There is a method to resize the entire Figure
plt.gcf().set_size_inches
Is it possible to similarly resize just a subplot of a Figure, i.e. Axis?
My xticks currently overlap and I would like to resize the plot accordingly, to:
len(ticks) * max(tick_lengths)
The problem with using set_size_inches is:
a) it resizes all subplots when a Figure contains more than a single Axis
b) the margins around the Axis and between them are also resized
You can change the spacing of the subplots with (amongst other things):
subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
For figures with multiple subplots, you might need to change wspace or hspace. See the documentation for details.
Alternatively, you could try calling plt.tight_layout() after plotting, usually this solves any spacing/overlapping/... problems.
Related
I use the matplotlib boxplot and plotted this graph. My question is how can I make the box larger or make the y-axis interval wider?
To resize the plot or to enlarge the resulting image, you could tweak the figsize parameter in matplotlib.pyplot.figure.
To reduce the size of the markers to improve the visibility of the dots/circles, you could do that using the linewidth parameter.
You could share a snippet of your code for more accurate answers.
I got a general issue with a plotting function using matplotlib.pyplot. I plot a set of stacked horizontal bar charts, the goal is to produce congruent figures, the embedded grid is suppose to fit, but the image gets resized, when I change the input data. However, if i plot the image without the legend, I receive the desired output. But I do want to include a legend of course !
The formatting lines I include are the following:
figure size
fig,ax = plt.subplots(1,figsize=(20,9))
grid definition
plt.grid(color="darkgrey")
legend defintion
ax.legend([handles[i] for i in order], [labels[i] for i in order],
bbox_to_anchor=((0.5, -0.15)), loc="upper center",fontsize="medium",ncol=5)
tight layout
fig.tight_layout()
store the image
fig.savefig(str(figname)+'.jpg',bbox_inches='tight',quality=95,dpi=300)
I am trying to make automated plots with matplotlib, with several different features plotted on top of one another (the background is a filled contour plot, one level above is a pcolormesh). The topmost feature that I'm trying to plot is several scatter plots, with different labels and icons.
I'm trying to add a legend to this plot, currently using the following commands:
leg = ax.legend(legplots,
legnames,
scatterpoints=1,
loc='upper center',
ncol=3,
fontsize=14,
bbox_to_anchor=(0.5, -0.14),
fancybox=True, shadow=True)
Assume that ax is the main axes, and that legplots and legnames are lists of scatter plots and their appropriate labels respectively.
Adding the legend works correctly, but as the number of legplots that I have (and their name lengths) vary, as you animate the plots, the legend grows and shrinks in size. How do I control the size of the legend box and the column widths inside the legend? Is this possible?
At the moment your bounding box has a size of 0, since you only specify its position ((0.5, -0.14)).
You can set the bounding box of the legend to be bigger than 0 and also big enough for the maximum size that it needs to have for the maximum number of elements to fit in. I think you will need to find that size by trial and error.
So using the full 4-tuple notation
bbox_to_anchor=(x0, y0, width, height)
in conjunction with an appropriate loc parameter and the keyword argument mode="expand" will allow you to make the legend big enough for your needs. For a more detailed explanation about the 4-tuple notation see this post and also look at the legend location guide.
I have a figure with multiple subplots, some being data graphs and some images. The image data will typically determine the size of the axes automatically, which is good. However, when I add titles to the different subplots, the titles are a different vertical positions. Here is a (reduced) example:
import pylab as plt
plt.subplot(121)
plt.title('A')
plt.subplot(122)
plt.imshow(plt.randn(10, 10))
plt.title('B')
plt.show()
See below for the output:
As you can see, the titles (A and B) are at different vertical positions. I am aware that I could manually set the pad parameter of the title function, but I was wondering if there is an automatic way to do so as well.
I don't have much experience with Matplotlib, but if you change the line
plt.subplot(122)
to
plt.subplot(222)
you should get the result I think you want.
I'm trying to draw an arrow into a loglog plot with matplotlib, which looks like this:
I know that it has been suggested to turn off the axis (Matplotlib: Draw a vertical arrow in a log-log plot), but I do need the axes. In addition, the suggestion did not seem to change anything (apart from turning the axes off, as expected):
plt.figure();plt.loglog([1,10,60],[1,0.1,0.005])
plt.axis('off')
plt.arrow(2,0.002,5,0.098,'k',head_length=0.3)
My work around so far has been to create an invisible inset (meaning: axes off) with a linear axes environment and plot the arrow in the inset, which works but is really a bit unpleasant. Is there a simpler way? Or do people recommend to add these type of additional features with eg. inkscape, after the main plot is done?
You can use plt.annotate rather than plt.arrow. This is noted in the documentation for plt.arrow:
The resulting arrow is affected by the axes aspect ratio and limits.
This may produce an arrow whose head is not square with its stem. To
create an arrow whose head is square with its stem, use annotate()
For example:
import matplotlib.pyplot as plt
plt.figure()
plt.loglog([1,10,60],[1,0.1,0.005])
plt.annotate('', xy=(5, 0.098), xytext=(2, 0.002),
arrowprops=dict(facecolor='black', shrink=0.),
)
plt.ylim(0.001, 10)
plt.show()
Note that you may need to adjust the axes limits to fit the arrow into the plot. Here I had to change ylim.