1

I am trying to plot a circle on a grid. The code that I have written is as follows:

import pyplot as plt
from pyplot import Figure, subplot

fig=plt.figure(1)
plt.axis([0,400,0,400])
ax=fig.add_subplot(1,1,1)
circ=plt.Circle((200,200), radius=10, color='g', fill=False)
ax.add_patch(circ)
plt.show()

Now, I want the center of the circle to be the center of the graph, that is, (200,200) in this example. In case of other cases I want it to automatically choose the centre depending on the size that us set. Can this be in some way?

To make it clearer I want to get the x-axis and the y-axis range so as to find the mid point of the grid. How do I proceed?

2 Answers 2

4

Your x-axis and y-axis ranges are in your code right here:

plt.axis([0,400,0,400])

So all you would need is leverage on this a bit like so:

x_min = 0
x_max = 400
y_min = 0
y_max = 400

circle_x = (x_max-x_min)/2.
circle_y = (y_max-y_min)/2.

circ=plt.Circle((circle_x,circle_y), radius=10, color='g', fill=False)

If you want to catch x_min etc. from the command prompt then read out sys.argv.

Sign up to request clarification or add additional context in comments.

Comments

0

What you want may be ax.transAxes, here's the tutorial for coordinates transformation.

ax.transAxes means the coordinate system of the Axes; (0,0) is bottom left of the axes, and (1,1) is top right of the axes.

fig=plt.figure(1)
plt.axis([0,400,0,400])
ax=fig.add_subplot(1,1,1)
circ=plt.Circle((0.5,0.5), radius=0.2, color='g', fill=False,transform=ax.transAxes)
ax.add_patch(circ)
plt.show()

Note that the radius is also transformed into Axes coordinate. If you specify a radius larger than sqrt(2)/2 (about 0.7) you will see nothing in the plot.

If you want to plot a set of circles, it would be much simpler if you use the function circles here. For this problem,

fig=plt.figure(1)
plt.axis([0,400,0,400])
ax=fig.add_subplot(1,1,1)
circles(0.5, 0.5, 0.2, c='g', ax=ax, facecolor='none', transform=ax.transAxes)
plt.show()

A bit more, if you want see a real circle (instead of an ellipse) in your figure, you should use

ax=fig.add_subplot(1,1,1, aspect='equal')

or something like that.

Comments

Your Answer

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.