I have a lot of data to create scatter plots for, with each individual plot needing to be saved. Each plot shares the same axis. Currently I have this which works:
for i in dct:
plt.figure()
plt.scatter(time_values, dct[i])
plt.title(i)
plt.xlabel("Time")
plt.ylabel("values")
plt.xticks(x_labels,rotation=90)
plt.savefig(os.path.join('some_file_path','image{}.png'.format(str(self.image_counter))))
plt.close('all')
However, it is very slow at actually creating the graphs. The answer here How could I save multiple plots in a folder using Python? does what I want, however only for a normal plot. Is there anyway I can implement something like this with a scatter plot? I have tried converting by data into a 2D array, however my x_axis values are a string and so it does not accept the array
Actually, you can plot scatter plots with plt.plot(x, y, 'o') and re-use that code by example.
Related
In some cases, especially in scientific papers, authors add two images in one figure on each other with different colormaps. you can see an example in the below images. Can anyone guide me on how to use matplotlib to create these kinds of images?
You can create one plot area and then simply add lines graphs, barcharts, etc. etc. to the same plot area. For example:
fig, ax = plt.subplots(figsize=(15,8))
ax.plot(df_1.index, df_1['Column_Name'], linewidth=5, color='blue', label='Some_Label')
ax.legend(loc='upper right')
ax.plot(df_2.index, df_2['Column_Name'], label='Some_Label')
ax.legend(loc='upper right')
You can create graphs using matplotlib, pandas or seaborn - it just depends what you are looking to create. What I did above is create 2 line graphs on the same plotting area - but you can do the same with other types of graphs.
I am new in python and I create pandas series in a for-loop. Each time I want to plot the series in a figure. I use ax = series.plot(title = str(i)+'.jpg') but all figures are plotted in same window. How can I plot them in different windows?
If you are using matplotlib, use
plt.figure()
for every new figure you want to plot.
You then show all figures with
plt.show()
also, you should check out what subplots does.
I am working on a project where I am generating hundreds of plots using the matplotlib module in Python. I want to put these plots in a pptx using the python-pptx module, let's say four plots on a slide without storing these plots on the local disk.
To overcome the storing problem I am using the BytesIO python module, which stores the plots inside the buffer, and then send these plots to the pptx.
The major issue that I am facing is the overlapping of the plots.
Question is how to send these plots serially to pptx so that we can avoid the overlapping?
Screenshot of pptx generated
I have added a screenshot of the pptx, where I am trying to add the two plots
Plot 1 (Age vs Name),
Plot 2 (Height vs Name),
but if you see the Plot 2 the data of plot 1 and plot 2 are getting overlapped. I want to avoid this overlapping.
You need to clear the axes between each plot, there are several ways to do that:
plt.clf(): clears the current figure
plt.cla(): clears the current axes
So instead of e.g.
plt.scatter(x, y1)
# save first plot
plt.scatter(x, y2)
# save second plot
You do
plt.scatter(x, y1)
# save first plot
plt.clf()
plt.scatter(x, y2)
# save second plot
And the two scatter plots will be drawn separately. This is probably the most 'blunt' way to approach this, but it should work fairly well. It is by no means the best way to do it for any specific case.
The figure is also cleared when plt.show() is called - but I expect that is undesirable in this case.
I have followed the post here in order to smooth a 3D scatter plot I have.
My original scatter plot is,
And I would like to get a smooth plot like the following one, that was made using Mathematica,
In the post I mentioned, they use the trisurf function to get a smoother plot. So I though I could use the same to get a similar plot. However, what I get is
As you can see, the triangulation did not work properly. And I don't know how to fix it.
Does anybody know a way to fix this problem? Or is there any other function I could use to smooth my scatter plot?
I think I should mention that my scatter plot is NOT a surface, it is a volume.
Thank you.
Just to clarify this, I post my codes for the original and the trisurf plot eventhough there isn't much to see.
Scatter plot:
S=pd.read_csv("SeparableStatesGrafica.csv",header=None,names=
['P0','P1','P2','P3','P4'])
G=plt.figure().gca(projection='3d')
G.scatter(S['P1'], S['P3'], S['P0'],color='red')
G.set_xlabel("P1")
G.set_ylabel("P3")
G.set_zlabel("P0")
G.view_init(40,40)
plt.show()
Trisurf plot:
S=pd.read_csv("SeparableStatesGrafica.csv",header=None,names=
['P0','P1','P2','P3','P4'])
p0=S['P0'].values
p1=S['P1'].values
p3=S['P3'].values
fig = pylab.figure(figsize=pyplot.figaspect(.96))
ax = Axes3D(fig)
ax.plot_trisurf(p1, p3, p0)
ax.set_xlabel("p1")
ax.set_ylabel("p3")
ax.set_zlabel("p0")
ax.view_init(40,40)
plt.show()
I am currently plotting two completely different datasets into one 3D surface plot. When I am plotting each one independently, everything works fine. However, as soon as I plot them in one, the visualization is strange. I do the plotting the following way:
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z, color=color, antialiased=True)
(get new X,Y, Z values)
ax.plot_surface(X,Y,Z, color=color, antialiased=True)
ax.view_init(30, 360)
The output is the following:
As you can see, the blue data is correct, but the green one is somehow in the backside and not correctly visualized. If I plot the green one alone, it works perfectly.
Changing the order of plotting (or playing around with zorder) does not change anything.
Hope someone can help!
Matplotlib is just a 2d plotting library. 3d plots are achieved by projecting the 3d surface onto the image plane.
If you have multiple 3d surfaces, it will turn each into a 2d shape, and then calculate a single height for each shape, and show then in that order.
As far as I'm aware, the zorder option doesn't work, and all it would is change the order of the surfaces anyway.
If you're really unlucky, the grey boxes that make up the axis grids can get plotted above your surface too. That's even more annoying.
Of you must use matplotlib, then i guess you could split up your surface into lots of smaller ones, but you're going to encounter a pretty big performance bit doing this, and you'll to override the values in the legend too.