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.
Related
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.
I really think my google skills and matplotlib documentation readings skills are failing me...but I do not seem to be able to find an answer.
I am applying some formatting to some plots I am doing:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
ax.grid(True, 'major', 'y', color='#D3D3D3')
ax.tick_params(axis='both', which='major', labelsize=12, labelcolor='#545454')
I would like this formatting to be applied to any future plots I do as well. Currently, as soon as I do a new plot, even of the same chart type and using the same data, the default formatting comes back.
I realise I can plot multiple charts in one figure, and I am doing this at times, but sometimes I only want to plot one chart at a time.
Is there a way to do this? Or is copy and paste my only solution?
Thanks for your time,
Ian.
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.
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.
I want to make a figure which has similar axis to the example below. I know I could use loglog plot. But in this example, the step-size (x-axis) decreases when you go farther to the right.
How could I do this in python (using matplotlib)
Possibly the invert_xaxis call is what you are looking for. As follows:
fig = plt.figure()
ax = fig.add_axes()
ax.invert_xaxis()
Link: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes