问题描述:

I am working with matplotlib patches and am trying to plot an ellipse at a specific angle. According to the code below, I think the line I have plotted should be at the exact same angle as the major axis of the ellipse (angle in degrees going anti-clockwise where 0 degrees is horizontal).

Unfortunately my reputation isn't good enough to post images, but if you run the code, you can see from the plot that they do not seem to align. (if you change the size of the plot you can make it look like they just about do, but surely they should still align no matter how you stretch the axis?)

Could anyone help to explain why this is and how I can remedy it?

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.patches as mpatches

import math

m = -1.34756097561

c = 337.518292683

angle_rad = np.arctan(m)

angle_deg = angle_rad*(180/math.pi)

major_ax = 5.0

minor_ax = 3.5

center = np.array([133.26666667, 157.93333333])

patch = mpatches.Ellipse(center, major_ax, minor_ax, angle_deg, fc='none', ls='solid', ec='g', lw='3.')

x = np.arange(131, 137)

fig, ax = plt.subplots()

line1 = plt.plot(x, m*x + c)

ax.add_patch(patch)

ax.set_xlim(130.5, 135.5)

ax.set_ylim(155.5, 160.5)

plt.plot(center[0], center[1], 'go')

plt.show()

网友答案:

When your code is run (almost, there is a typing mistake in lw='3.' which should be lw=3.), I get:

I just made a small change (changed the aspect ratio to be 1:1) to show the the line intersects the ellipse in 90° angles.

However on-screen the center dot may not look to be in the right place. The image above is taken with savefig, but for comparison a screen capture of exactly the same plot:

The center dot is not in the right place. This is a feature (bug) of the backend. (In my case the backend is MacOSX, and I do not know if this bugs other backends.)

相关阅读:
Top