[英]How to change the size of plot with equal aspect ratio?
It seems that I can't have both setting equal axes scales AND setting the size of the plot. 似乎不能同时设置相等的轴比例和设置图的大小。 What I'm doing is:
我正在做的是:
fig = pl.figure(figsize=(20,20))
ax = fig.add_subplot(111)
ax.set_aspect('equal')
If I remove the figsize
the plot seems to have equal scales, but with figsize
I have a bigger plot but the scales aren't equal anymore. 如果我删除
figsize
则绘图似乎具有相等的比例,但是使用figsize
,则绘图会具有较大的比例,但是比例不再相等。
Edit: The graph does not necessarily have to be square, just bigger.. please assume that I don't now the exact ratio of the axes 编辑:图形不一定必须是正方形,只是更大..请假设我现在没有确切的轴比例
Any solutions? 有什么办法吗?
Thanks 谢谢
If you want to change the data limits to make the axes square, add datalim
to your call to set_aspect
: 如果你想改变的数据限制,使轴广场,加
datalim
您的来电set_aspect
:
ax.set_aspect('equal', 'datalim')
If you want the scaling to change so that the limits are different but the axes look square, you can calculate the axis size ratio and set it explicitly: 如果要更改缩放比例以使限制不同但轴看起来是正方形,则可以计算轴尺寸比并显式设置:
ax.set_aspect(1./ax.get_data_ratio())
eg 例如
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)
x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect('equal', 'datalim')
plt.show()
or 要么
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)
x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect(1./ax.get_data_ratio())
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.