Matplotlib将不会显示默认情况下居中的多边形图? [英] Matplotlib will not show a polygon plot centered by default?

查看:38
本文介绍了Matplotlib将不会显示默认情况下居中的多边形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于到目前为止我所见过的所有类型图,当没有给出 xlim(),ylim()值时, matplotlib 会自动将它们居中.示例:

For all types plots I've seen so far, matplotlib will automatically center them when no xlim(), ylim() values are given. Example:

import matplotlib.pyplot as plt
A_pts = [(162.5, 137.5), (211.0, 158.3), (89.6, 133.7)]
ax = plt.subplot(111)
ax.scatter(*A_pts)
plt.show()

但是当我绘制一个 Polygon

ax = plt.subplot(111)
triangle = plt.Polygon(A_pts, fill=None, edgecolor='r')
ax.add_patch(triangle)
plt.show()

绘图窗口显示为两个轴的限制 [0, 1],这导致多边形不可见.我必须明确地传递适当的限制,以使其显示在绘图窗口中

the plot window is shown with limits [0, 1] for both axis, which results in the polygon not being visible. I have to explicitly pass proper limits so that it will show in the plot window

ax.set_xlim(80, 250)
ax.set_ylim(120, 170)

这是有意为之还是我遗漏了什么?

Is this by design or am I missing something?

推荐答案

添加补丁时,坐标轴的数据限制发生变化,打印ax.dataLim.bounds即可看到.但是, add_patch 不会调用automlimits函数,而大多数其他绘图命令都可以.

When adding a patch, the data limits of the axes are changed, which you can see by printing ax.dataLim.bounds. However, add_patch does not call the automlimits function, while most other plotting commands do.

这意味着您可以手动设置绘图的限制(如问题所示),也可以调用 ax.autoscale_view() 来调整限制.后者当然具有以下优点:您无需事先确定限制,并且可以保留边距.

This means you can either set the limits of the plot manually (as in the question) or you can just call ax.autoscale_view() to adjust the limits. The latter has of course the advantage that you don't need to determine the limits beforehands and that the margins are preserved.

import matplotlib.pyplot as plt
pts = [(162, 137), (211, 158), (89, 133)]
ax = plt.subplot(111)
triangle = plt.Polygon(pts, fill=None, edgecolor='r')
ax.add_patch(triangle)
print ax.dataLim.bounds

ax.autoscale_view()
plt.show() 

一旦您添加了一些其他自动缩放限制的图,就无需再调用 autoscale_view().

Once you would add some other plot which does automatically scale the limits, there is no need to call autoscale_view() any more.

import matplotlib.pyplot as plt
pts = [(162, 137), (211, 158), (89, 133)]
ax = plt.subplot(111)
triangle = plt.Polygon(pts, fill=None, edgecolor='r')
ax.add_patch(triangle)

ax.plot([100,151,200,100], [124,135,128,124])

plt.show()

这篇关于Matplotlib将不会显示默认情况下居中的多边形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆