对于相关图,我希望有一个光学正方形的图(x和y的长度相同,以像素为单位),但在x和y上也有一定的轴限制。我可以分别得到这两个图中的每一个,但不能同时得到:
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2)
x = [1 , 4 , 6]
y1 = [4, 7, 9]
y2 = [20, 89, 99]
ax1.plot(x, y1, 'o')
ax2.plot(x, y2, 'o')
myXlim = [0, 8]
ax1.set_xlim(myXlim)
ax2.set_xlim(myXlim)
ax1.axis('square')
ax2.axis('square')
# limit is gone here
ax1.set_xlim(myXlim)
ax2.set_xlim(myXlim)
# square is gone here
plt.show()
如果我只使用ax1.set_xlim(myXlim)
(而不是square
),那么我可以手动调整窗口大小以获得我想要的结果,但是我如何才能自动完成呢?在
获取正方形子图的一个选项是设置子图参数,以便生成的子图自动调整为方形。这有点牵扯,因为所有的边距和间距都需要考虑在内。在
这里的好处是能够自由缩放和自动缩放。缺点是一旦图形大小改变,子图大小就不再是正方形了。为了克服这个缺点,还可以在图形的大小更改时注册回调。在
^{pr2}$上面的解决方案通过限制子批次在其网格内的空间来工作。另一种相反的方法,即子块的大小以某种方式固定,将在Create equal aspect (square) plot with multiple axes when data limits are different?的答案中显示。在
没有像“square”这样的单字魔术,但是您可以在设置了限制之后使用set_aspect来玩玩(去掉影响轴值的方形线):
我只是想看看上面的值:
您可以通过将x范围除以y范围来计算这个值-
^{pr2}$ax1
的估计值是8/5,ax2
的估计值是8/85,但是您可以使用实际值来精确:相关问题 更多 >
编程相关推荐