How to create a dot plot (not scatter plot) from this data? - python

I need help creating a dot plot in Python like the one from the image.
The exercise consists on graphing the following data 74.001 , 74.003, 74.015, 74.000, 74.005, 74.004. I'm having some trouble with doing the dot plot because I can't find how to do it.

Here you go:
import matplotlib.pyplot as plt
y =[74.001 , 74.003, 74.015, 74.000, 74.005, 74.004]
fig = plt.plot(y,'o', fillstyle='none')
Next time you post a question, include a MRE (Minimum Reproducible Example) showing what you have done.

Using plotly and also defining x which was not provided.
import plotly.express as px
y =[74.001 , 74.003, 74.015, 74.000, 74.005, 74.004]
x =[12.4,12.5,12.5,12.6,12.7, 12.8]
px.scatter(x=x, y=y).update_traces(marker_symbol="circle-open", marker_line_width=3)

Related

Plotting each Cluster value percentage individually

So I have been working on this problem for a bit and seem to be stuck..so I am asking for some guidance here.
This is my code
from clusteval import clusteval
from sklearn.datasets import make_blobs
import pandas as pd
X, labels = make_blobs(n_samples=50, centers=2, n_features=5, cluster_std=1)
X = abs(X)
X = pd.DataFrame(X, columns=['Feature_1','Feature_2','Feature_3','Feature_4','Feature_5'])
ce = clusteval('kmeans', metric='euclidean', linkage='complete')
results = ce.fit(X)
X['Cluster_labels'] = results['labx']
X.groupby('Cluster_labels').Feature_1.value_counts(normalize=True).plot(kind='bar')
plt.tight_layout()
plt.show()
This produces this image:
This image is really close to what I want but notice that both clusters show up in the same graph. I would like to produce the same graph represents only one cluster. essentially for every cluster I have I want a graph like this. So if I had 10 clusters, I would have 10 graphs that showed the percentage of each value within that cluster and that cluster only.
Any guidance or help is appreciated. Thanks.
I can suggest two alternative plots. Both would benefit from visual refinement (label all axes, clean up underscores, pick nicer font sizes, etc.) but hopefully are useful starting points.
Using pandas:
axes = X.hist('Feature_1', by='Cluster_labels')
for ax in axes:
ax.set_title('Cluster_labels = ' + ax.get_title())
Using seaborn:
import seaborn as sns
sns.displot(X,
x='Feature_1',
col='Cluster_labels',
binwidth=0.5)

plot overlaps using matplotlib

I am learning matplotlib.
I am trying to plot two below plots in a single plot using matplotlib.
But it overlaps.
Here is my code.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
train_error = [0.26462888486225206, 0.26462383329393313, 0.2628674962680674, 0.2553700231555298, 0.17473717177688022, 0.14773444580059242, 0.1468299949185866, 0.1468235689407127, 0.1439370366766204]
test_error = [0.8438224756653776, 0.8442034650577578, 1.018608707726192, 4.853704454584892, 123.69312582226338, 798.4569874115062, 3205.5264038946007, 9972.587330411312, 10787335.618580218]
plt.plot(train_error)
plt.plot(test_error)
plt.show()
Where am i doing wrong ? Can anyone please guide / help ?
Use the subplot
Go check https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
plt.subplot(1,2,1)
plt.plot(train_error)
plt.subplot(1,2,2)
plt.plot(test_error)
in plt.subplot(a,b,x) you have a,b that represents the number of (row and column) you want vertically and horizontally and x the index of the subplot selected counting from left to right and top to bottom.

Step drawstyle plotly dash

I created some cool graphs with matplotlib and “drawstyle steps” time on x, categorical data on y and steps betwenn these points like picture below. Is this possible in plotly? I have only found gannt, but thats not what I need, in a kind of a waterfall graph would be nice but I have same category multiple on timeline (x), must not be exact the same but something which I can see how long was the time (in my case timedelta) from one datadot to another
You can use line_shape (set to vh or hv) and line_dash like this.
import plotly.express as px
fig = px.line(x=[0,1,2,3,4,5], y=[0,1,0,2,0,1])
fig.update_traces(mode="markers+lines", line_shape="vh", line_dash="dash")
fig.show()

Plotting vertical profile of wind barbs with matplotlib

I am trying to plot the two horizontal wind components u and v in a vertical profile using matplotlib. Next to that, I would like to plot wind barbs at the same heights, so that the two plots share the y-axis. I would like to use the sharey-option so that i can control the ylimits more easily using values of z.
So far I did this:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
u = np.array([1,2,3,5,7,7,7,7])
v = np.array([-1,-1,-1,1,3,3,3,3])
z = np.array([2,10,50,100,200,300,400,500])
fig,ax = plt.subplots(figsize=[15,8],ncols=2,sharey=True)
ax[0].plot(u,z,label='U-component')
ax[0].plot(v,z,label='V-component')
ax[0].axvline(0,color='k')
ax[0].legend(loc=4)
Xq,Yq = np.meshgrid(1,np.arange(0,u.shape[0]))
ax[1].barbs(Xq,Yq,u,v)
plt.show()
which gives me the following plot:
image with sharey-option
As you can see, the barb-function plots the wind barbs against the index of the array, but i would like to plot it against my z-array (just like I did with the u and v arrays). It should look something like this:
without sharey-option
Here I simply switched off the sharey-option to show what I would like to have. But as I mentioned, with this option I cannot set the ylimits of the barb-plot using z-values.
Can anyone help me to get this done?
I hope I made myself clear, if not, please help me to improve my question. The code should work as a minimal working example.
thanks in advance
philipp
Not sure I understood correctly, but if I did then you should try changing the meshgrid line to:
Xq,Yq = np.meshgrid(1, z)
The point being setting the barbs origin to be at (1, z) coordinate.

creating data for matplotlib

I wanted to create graphs for some math functions and tried to do this using matplotlib.I need to plot graphs of several functions in one plot sothat these can be compared against each other.
say these are the math functions
2**(math.log(x,2))
2**(2**(math.log(x,2)))
I tried this
from matplotlib.pyplot import plot as plt
x=arange(1000,1010)
y1=[2**(math.log(t,2)) for t in x ]
y2=[2**(2**(math.log(t,2))) for t in x ]
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()
this only shows one graph..that of (x,y2)
how do I make all the graphs show in one plot?
edit:
using plt.plot(x,y1,x,y2) produces this graph
I don't think they are going to be visible on the same scale. The first one is essentially y = x, y is about 1000. The second one is y = 2**x, and x starts at 1000...
However, plotting with log scale can help:
matplotlib.pyplot.yscale('log')

Categories