I have just started experimenting with matplotlib, since I often come across instances where I need to plot some data, for which matplotlib seems an excellent tool. I attempted to adapt the ellipse example in the main site, so as to draw two circles instead, how ever after running the code, I find that none of the patches are displayed, I am not able to figure out what exactly is wrong... here is the code. Thanks in Advance.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
plt.axis([-3,3,-3,3])
ax = plt.axes([-3,3,-3,3])
# add a circle
art = mpatches.Circle([0,0], radius = 1, color = 'r', axes = ax)
ax.add_artist(art)
#add another circle
art = mpatches.Circle([0,0], radius = 0.1, color = 'b', axes = ax)
ax.add_artist(art)
print ax.patches
plt.show()
1 Answer 1
Which version of matplotlib are you using? I'm not able to replicate your results, I can see the two ellipsis quite well. I'm going by a long shot, but i guess you mean to do something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
# create the figure and the axis in one shot
fig, ax = plt.subplots(1,figsize=(6,6))
art = mpatches.Circle([0,0], radius = 1, color = 'r')
#use add_patch instead, it's more clear what you are doing
ax.add_patch(art)
art = mpatches.Circle([0,0], radius = 0.1, color = 'b')
ax.add_patch(art)
print ax.patches
#set the limit of the axes to -3,3 both on x and y
ax.set_xlim(-3,3)
ax.set_ylim(-3,3)
plt.show()
3 Comments
set_xlim
method impose the limit of your plot in terms of data coordinates. So putting -3,3 as parameters tell to matplotlib to plot only those objects that are included in that intervalSyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?