6

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()
asked Dec 10, 2012 at 11:35

1 Answer 1

7

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()
answered Dec 10, 2012 at 11:49

3 Comments

Thanks the changes have made it work, I am using Matplotlib v 1.2.0, compiled from source. Just a question what does the ax.set_xlim method limit? Again thanks a lot
The 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 interval
For some reason this throws a syntax error for me: SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.