python - plot a circle with pyplot -


surprisingly didn't find straight-forward description on how draw circle matplotlib.pyplot (please no pylab) taking input center (x,y) , radius r. tried variants of this:

import matplotlib.pyplot plt circle=plt.circle((0,0),2) # here must circle.plot() or not? plt.show() 

... still didn't working.

you need add axes. circle subclass of artist, , axes has add_artist method.

here's example of doing this:

import matplotlib.pyplot plt  circle1 = plt.circle((0, 0), 0.2, color='r') circle2 = plt.circle((0.5, 0.5), 0.2, color='blue') circle3 = plt.circle((1, 1), 0.2, color='g', clip_on=false)  fig, ax = plt.subplots() # note must use plt.subplots, not plt.subplot # (or if have existing figure) # fig = plt.gcf() # ax = fig.gca()  ax.add_artist(circle1) ax.add_artist(circle2) ax.add_artist(circle3)  fig.savefig('plotcircles.png') 

this results in following figure:

the first circle @ origin, default clip_on true, circle clipped when ever extends beyond axes. third (green) circle shows happens when don't clip artist. extends beyond axes (but not beyond figure, ie figure size not automatically adjusted plot of artists).

the units x, y , radius correspond data units default. in case, didn't plot on axes (fig.gca() returns current axes), , since limits have never been set, defaults x , y range 0 1.

here's continuation of example, showing how units matter:

circle1 = plt.circle((0, 0), 2, color='r') # make circle no fill, hi-lighting key results circle2 = plt.circle((5, 5), 0.5, color='b', fill=false) circle3 = plt.circle((10, 10), 2, color='g', clip_on=false)  ax = plt.gca() ax.cla() # clear things fresh plot  # change default range new circles work ax.set_xlim((0, 10)) ax.set_ylim((0, 10)) # data ax.plot(range(11), 'o', color='black') # key data point encircling ax.plot((5), (5), 'o', color='y')  ax.add_artist(circle1) ax.add_artist(circle2) ax.add_artist(circle3) fig.savefig('plotcircles2.png') 

which results in:

you can see how set fill of 2nd circle false, useful encircling key results (like yellow data point).


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -