问题
I have a plot of two boxplots in the same figure. For style reasons, the axis should have the same length, so that the graphic box is square. I tried to use the set_aspect method, but the axes are too different because of their range
and the result is terrible.
Is it possible to have 1:1 axes even if they do not have the same number of points?
回答1:
You can use Axes.set_aspect to do this if you set the aspect to the ratio of axes limits. Here's an example:
from matplotlib.pyplot import figure, show
fig = figure()
ax0 = fig.add_subplot(1,2,1)
ax0.set_xlim(10., 10.5)
ax0.set_ylim(0, 100.)
ax0.set_aspect(.5/100)
ax1 = fig.add_subplot(1,2,2)
ax1.set_xlim(0., 1007)
ax1.set_ylim(0, 12.)
x0, x1 = ax1.get_xlim()
y0, y1 = ax1.get_ylim()
ax1.set_aspect((x1-x0)/(y1-y0))
show()
There may be an easier way, but I don't know it.
回答2:
Try axis('equal'). It's been a while since I worked with matplotlib, but I seem to remember typing that command a lot.
回答3:
For loglog plots ( loglog() ) don't forget to use
ax1.set_aspect(log10(xmax/xmin)/log10(ymax/ymin))
来源:https://stackoverflow.com/questions/1506647/matplotlib-square-boxplot