Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot circle on unequal axes with pyplot

I would like to plot a circle on an auto-scaled pyplot-generated graphic. When I run

ax.get_aspect()

hoping for a value with which I could manipulate the axes of a ellipse, pyplot returns:

auto

which is less than useful. What methods would you suggest for plotting a circle on a pyplot plot with unequal axes?

like image 747
benkay Avatar asked Aug 16 '12 20:08

benkay


People also ask

How to plot circles in Matplotlib?

How to Plot Circles in Matplotlib (With Examples) You can quickly add circles to a plot in Matplotlib by using the Circle () function, which uses the following syntax: matplotlib.patches.Circle (xy, radius=5)

How do you draw a circle on a scatter plot?

A scatter plot is a graphical representation that makes use of dots to represent values of the two numeric values. Each dot on the xy axis indicates value for an individual data point. Here, we have used the scatter plot to draw the circle. The xlim () and the ylim () methods are used to set the x limits and the y limits of the axes respectively.

What does the third (green) circle mean in a plot?

The third (green) circle shows what happens when you don't clip the Artist. It extends beyond the axes (but not beyond the figure, ie the figure size is not automatically adjusted to plot all of your artists). The units for x, y and radius correspond to data units by default.

What is the aspect ratio of the circle in the scatter plot?

For this example, we took the radius of the circle as 0.4 and set the aspect ratio as 1. A scatter plot is a graphical representation that makes use of dots to represent values of the two numeric values. Each dot on the xy axis indicates value for an individual data point.


2 Answers

It really does depend what you want it for.

The problem with defining a circle in data coordinates when aspect ratio is auto, is that you will be able to resize the figure (or its window), and the data scales will stretch nicely. Unfortunately, this would also mean that your circle is no longer a circle, but an ellipse.

There are several ways of addressing this. Firstly, and most simply, you could fix your aspect ratio and then put a circle on the plot in data coordinates:

import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure()
ax = plt.axes()

ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(np.sin(theta), np.cos(theta))

plt.show()

data coords circle

With this, you will be able to zoom and pan around as per usual, but the shape will always be a circle.

If you just want to put a circle on a figure, independent of the data coordinates, such that panning and zooming of an axes did not effect the position and zoom on the circle, then you could do something like:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure()
ax = plt.axes()
patch = mpatches.Circle((325, 245), 180, alpha=0.5, transform=None)
fig.artists.append(patch)


plt.show()

device coordinates circle

This is fairly advanced mpl, but even so, I think it is fairly readable.

HTH,

like image 93
pelson Avatar answered Oct 06 '22 00:10

pelson


This question is more than one year old, but I too just had this question. I needed to add circles to a matplotlib plot and I wanted to be able to specify the circle's location in the plot using data coordinates, and I didn't want the circle radius to change with panning/zooming (or worse the circle turning into an ellipse).

The best and most simple solution that I've found is simply plot a curve with a single point and include a circle marker:

ax.plot(center_x,center_y,'bo',fillstyle='none',markersize=5)

which gives a nice, fixed-size blue circle with no fill!

like image 45
user3208430 Avatar answered Oct 06 '22 00:10

user3208430