May be this is a silly question. I am exporting an eps plot from matplotlib (installed through Anaconda in Ubuntu 18.04 LTS). When opening the .eps file an annoying square appears at the right top corner, this seems to be something from the fig viewer. I still could not manage to find a solution.
EDIT: I am adding the code, after the accepted answer.
plt.figure(num=2, figsize=(5,3))
p1, = plt.plot(ffaa[0], ffaa[1], 'k-', linewidth=0.5)
plt.xlabel('Time (s)', fontsize=8)
plt.ylabel('Voltage (V)',fontsize=8)
plt.legend(handles=[p1])
plt.xlim([0,100])
plt.show()
mu.figexp('Figure 03 - Frequency spectrum', plt)
The "annoying square" is a legend. So at some place in your code you create a legend for your graph, presumably via plt.legend() or ax.legend(). However your plot does not have any label associated with it. Hence the legend stays empty.
Solutions:
Remove the line that created the legend from your code.
Add a label to your plot, e.g. via plt.plot(...., label="my label"), which would then be shown inside the box.
Related
I have a plot that has a scatter. I was using previous answers that allowed me to click individual points of the scatter and display a label that I connected to each point (Found here and here). It worked well, but I now want to display a separate line on the same plot. I'm able to show the line, but now when I click on points near it, it displays a box about the line and not the scatter. I used zorder to have the scatter displayed on top but that doesn't change anything about which plot is selected when I click. Is there a way to specify which plot is on top in the mplcursors? I would think there would be a way if I wasn't using the loop to connect the scatter plot points with the labels, but I'm not sure if there's another way to do that. Alternatively, is there a way I could tell mplcursors to just ignore the line altogether?
Code snippet:
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(1,1,1)
ax.plot(x_axis_2, y_axis_2, zorder=0) #This is the line
for i in self.full_dict['full_results']: #This is for the scatter plot
ax.scatter(i[1],i[2],label='$[1D,2D]: {}$'.format(i[0]),zorder=5)
canvas = FigureCanvasTkAgg(fig, master=self.root)
canvas.draw()
canvas.get_tk_widget().grid(row=14,column=0,ipadx=40,ipady=10,columnspan=6)
datacursor(formatter='{label}'.format)
I am trying to make a single scatterplot for my online course using matplotlib, but sometimes multiple plots are showing up on the screen.
Here is the code:
plt.figure()
plt.scatter(x[:2], y[:2], s=100, c='red', label='Tall students')
plt.scatter(x[2:], y[2:], s=100, c='blue', label='Short students')
plt.show()
My problem is that if I run the code twice I get two images like this:
If I run it again, I get only a single plot.
Is there any way to make sure I get only 1 plot here?
plt.clf() will clear the current figure. plt.cla() will clear all subplots.
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.
Some code gives me the following matplotlib figure:
Unfortunately, the figure size is fixed and hence on the top right, the legend and the lines overlap. Is there any way to have the legend not stack on top of the lines?
I am aware that legend allows ax2.legend(loc=0), where 0 will put it into the "best" location. However, with two y axis as here, this will stack both legends on top of each other - not really the best allocation.
My next best try would be to "scale up" the figure, as manually done with an interactive graph, where I have only scaled up both axis:
Doing this with the "real" figure scale requires iterated "trying numbers and checking how far it goes" procedure - which may need to be redone if the graph changes. Is there any way of having matplotlib compute the scale "intelligently"?
If the best location plt.legend(loc='best') fails, try putting the legend outside of the plot:
plt.legend(loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0)
You can scale only legend, not the whole plot. Link here
More on legends here and also here.
I'm running a script remotely on a cluster to generate a scatter plot. I wish to save the plot, but I don't want the plot to be display or a window to come up (as when you execute plt.show() ).
My saved plots are always empty. This is the code that I'm using (below). Any tips would be very helpful. Thanks!
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([-1,maxX+1])
ax.set_ylim([0,maxY+1])
ax.set_xlabel('Comparison number (n)', fontsize=18, fontweight='bold')
ax.set_ylabel('Normalized cross correlation score', fontsize=18, fontweight='bold')
ax.scatter(xaxis,yaxis)
plt.savefig('testfig.png')
In order to use avoid showing plot windows (i.e. to do off-screen rendering) you probably want to use a different matplotlib backend.
Before any matplotlib import statements, add
import matplotlib
matplotlib.use('Agg')
and subsequent calls to matplotlib will not show any plot windows.
If your plot file shows an empty axis, then the problem lies in the plotting arguments as calling plot with empty arguments creates an empty axis.