Matplotlib图表变成空白

时间:2017-04-20 00:16:37

标签: python matplotlib plot

我试图在matplotlib中绘制圆圈,但结果总是一个空图。

E.g。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.collections as mcollections

fig = plt.figure()
fig.set_size_inches(18.5, 10.5, forward=True)
ax = fig.add_subplot(111, aspect='equal')

x = np.array([17., 29., 41.,  3., 15.])
y = np.array([21., 41., 30., 19., 5.])
r = np.array([22.8035085, 46.04345773, 46.61544808, 16.,  12.16552506])

patches = [mpatches.Circle((xx, yy), rr) for xx, yy, rr in zip(x, y, r)]
collection = mcollections.PatchCollection(patches)
ax.add_collection(collection) 

fig.savefig("test.png")

这会产生一个空图,当我尝试使用add_artist时也是如此。希望有人可以指出我哪里出错了!谢谢

2 个答案:

答案 0 :(得分:4)

除了提供的其他答案外,您还可以在保存图表之前使用ax.autoscale()。这将导致

enter image description here

答案 1 :(得分:1)

这里你的问题不是没有得到庇护。你的情节是空白的原因是 matplotlib没有根据补丁的范围自动调整轴。

通常,它会使用一些主要的绘图功能(例如plt.plot(), plt.scatter() ...)执行自动调整作业。就我而言,它并非设计用于绘制几何图形,如三角形,矩形和圆形。这也可以解释为什么在patches中调用matplotlib

所以你需要做的是在你的情况下使用类似的东西来手动指定轴的范围:

plt.axis([-50, 100, -50, 100])