I am trying to plot some small circles in my plotting window, in addition to the curves I’m already plotting. If I don’t want to set my x- and y- axis scales equal to each other, a naive drawing of a circle results in an ellipse. To fix this problem I found some nice example code online here : http://stackoverflow.com/questions/9230389/why-is-matplotlib-plotting-my-circles-as-ovals, which solves the problem by basically plotting an ellipse, but an ellipse which will look like a circle in the display window.
That works all fine for me, but then, if I change my xlim or ylim using ax1.set_xlim((something1,something2)) then the solution no longer works, and I get an ellipse.
A minimal example showing the breaking behavior can be seen below.
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Circle
fig = plt.figure()
ax1 = fig.add_subplot(111)
uncomment the following line to see it break
calculate asymmetry of x and y axes:
x0, y0 = ax1.transAxes.transform((0, 0)) # lower left in pixels
x1, y1 = ax1.transAxes.transform((1, 1)) # upper right in pixes
dx = x1 - x0
dy = y1 - y0
maxd = max(dx, dy)
width = .15 * maxd / dx
height = .15 * maxd / dy
a circle you expect to be a circle, but it is not
ax1.add_artist(Circle((.5, .5), .15))
an ellipse you expect to be an ellipse, but it’s a circle
ax1.add_artist(Ellipse((.75, .75), width, height))
I suppose the problem is that ax1.transAxes.transform commands return the same numbers, regardless of whether I’ve changed the limits or not. Is there an easy and clean way to fix this (perhaps a different command for getting x0,y0,x1, and y1)?
Thanks for the help!