python - 如何在使用 ax.axis ('equal' 时强制执行 xlim 和 ylim )?

标签 python python-2.7 matplotlib plot

我想使用ax.axis('equal')强制 X 和 Y 上的间距均匀,但我还想指定 X 和 Y 轴的特定范围。如果边距也固定的话,问题就过约束了,结果如图1左侧所示。相反,如果允许边距自动增加以弥补闲置,则 xlimylim可以保持我设置的状态,同时仍然令人满意axis('equal') 。我所追求的示例如图 1 右侧所示。 如何让绘图边距“ float ”?

f,ax=plt.subplots(1) #open a figure
ax.axis('equal') #make the axes have equal spacing
ax.plot([0,20],[0,20]) #test data set

#change the plot axis limits
ax.set_xlim([2,18]) 
ax.set_ylim([5,15])

#read the plot axis limits
xlim2=array(ax.get_xlim())
ylim2=array(ax.get_ylim())

#define indices for drawing a rectangle with xlim2, ylim2
sqx=array([0,1,1,0,0])
sqy=array([0,0,1,1,0])

#plot a thick rectangle marking the xlim2, ylim2
ax.plot(xlim2[sqx],ylim2[sqy],lw=3) #this does not go all the way around the edge

actual and desired output 图 1:上述代码片段的输出。

最佳答案

ax.set_aspect('equal',adjustable='box')

enter image description here

关于python - 如何在使用 ax.axis ('equal' 时强制执行 xlim 和 ylim )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39775709/

相关文章:

python - 删除特定行 Pandas 数据框上方的所有行

python - 更改 Seaborn 图中的轴

python - 创建一个条形图,其条形是相对于最大值或最小值的填充进度百分比

python - 如何从 python 检查 OpenAI key 的有效性?

python - Setuptools entry_points/console_scripts 在 shebang 中有特定的 Python 版本

python - py2app bundle 中的 Twisted Core Foundation Reactor 和 CFNetwork

Python 对象缓存

python - 将字符串转换为二进制,然后异或二进制

python - 如何在列表理解中改变字典的值

python - 如何绘制多个图形并使用 [matplotlib] 中的导航按钮