Reputation: 11
So, I'm trying to plot some data I have and draw a confidence ellipse with a given mean and covariance on top of it. I'm very new to Python but I thought it would be way easier to visualize this than it would be in Java (where I calculated these values used).
The problem I have right now is that I simply can't rotate the ellipse I'm drawing without distorting it. I'm not entirely sure if that is actually the case, but I think it's because of the vastly different scales in the axes (x from 0 to 6, y from 40 to 100). Once again I'm guessing, but I would imagine that the rotation doesn't scale the height (which is set to fit the y axis from 40 to 100 obviously) so it's way too long for my short x axis.
My question: Is there a better way to do this or a way to scale the ellipse after the rotation so it fits again? Since I don't really know too much about Python, I just went through different tutorials to see what gets me a result quickly. Not sure if this is the "right" way to begin with. What I know, however, is that all of the examples I saw have similarly scaled axes so I could not find an adequate solution to my issue.
This is just a part of the code, the actual data is not included but it should still be copy-paste-able for the ellipse so you guys can see what I mean if you change the angle of rotation (45 in this example).
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
meanX = 2.0374
meanY = 54.4897
secondLongest = 0.0700
longest = 33.7679
ax = plt.subplot(111)
plt.axis([0, 6, 40, 100])
#plt.plot(duration, waitperiod, "rx")
plt.xlabel("Duration")
plt.ylabel("Time between eruptions")
#if rotation is set to 0, it works perfectly fine but I want a different angle
ellipse = Ellipse((meanX, meanY), 2*np.sqrt(5.991*secondLongest), 2*np.sqrt(5.991*longest), 45)
ax.add_artist(ellipse)
plt.show()
Upvotes: 1
Views: 566
Reputation: 15071
It works perfectly fine, the apparent distortion you see is indeed because of the different scaling of your X and Y axes in the plot. E.g. try with these axis limits to see that angle doesn't "distort" anymore:
plt.axis([-20, 20, 40, 80])
(and make sure your plot window is square too)
If this ellipse represents confidence, I doubt you want to rescale it to fit your arbitrary axis limits, instead you probably want to set your axis limits to fit your data and confidence ellipse...
Upvotes: -1