ero*_*oar 4 python plot matplotlib
我正在尝试在matplotlib中绘制圆,但是结果始终是空图。
例如
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")
Run Code Online (Sandbox Code Playgroud)
这将产生一个空图,与尝试add_artist时相同。希望有人可以指出我要去哪里了!谢谢
你的问题不是没有绘制补丁。您的绘图为空白的原因是matplotlib 没有根据您的补丁范围自动调整轴。
通常,它会使用一些主要绘图功能来完成自动调整工作,例如plt.plot(), plt.scatter() .... 就我而言,它不是为绘制三角形、矩形和圆形等几何图形而设计的。这也可以解释为什么这会被patches调用matplotlib。
因此,您需要做的是在您的情况下使用类似以下内容手动指定轴的范围:
plt.axis([-50, 100, -50, 100])
Run Code Online (Sandbox Code Playgroud)