Matplotlib square boxplot

牧云@^-^@ 提交于 2019-11-30 17:40:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!