Matplotlib plots turn out to be empty
I am trying to plot circles in matplotlib but the result is always empty.
eg.
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")
This creates an empty plot, same thing when I try to add add_artist. Hopefully someone can point me where I am going wrong! Thanks you
source to share
Your problem here is not that the patches were not allocated. The reason your plot is empty is because matplotlib did not automatically adjust the axis according to the range of your patches.
It usually does automatic job setup with some basic schedule functions such as plt.plot(), plt.scatter() ...
. As far as I know, it is not designed for drawing geometric shapes like triangles, rectangles and circles. This may also explain why it is called patches
in matplotlib
.
So, you need to specify the axis range manully, using in your case something like this:
plt.axis([-50, 100, -50, 100])
source to share