我不得不画一个椭圆,并使用了下面的程序,它给了我预期的结果.

在最后一部分,我使用了ax.set_aspect('equal'),我注意到我还必须使用plt.axis('equal'):如果我取消最后一个指令,我得到的只是一个盒子,但没有任何椭圆.

我以为只用ax.set_aspect('equal')就足够了,但事实并非如此.我不明白为什么.我可能在plot计划中有一些不一致的地方,但我不知道在哪里.如果你能帮忙,我将不胜感激.

# Imports
import numpy as np
sqrt = np.sqrt
atan = np.arctan
degrees = np.degrees
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Ellipse general equation's coefficients
A = 1 ; B = -1.25 ; C = 1.625 ; D = 1.875 ; E = 1.5 ; F = -2

# Previous personal calculations
det_M2 = (4*A*C - B**2) / 4
det_M3 = (B*D*E - (A*(E**2) + C*(D**2)) - (B**2 - 4*A*C)*F) / 4
xc = (B*E - 2*C*D) / (4*det_M2)
yc = (B*D - 2*A*E) / (4*det_M2)
center = [xc, yc]
theta = atan(B/(A-C)) / 2
lambda_plus = (A+C)/2 + sqrt(((A+C)/2)**2 - det_M2)
lambda_minus = (A+C)/2 - sqrt(((A+C)/2)**2 - det_M2)
a = sqrt(2)/2 * sqrt(-lambda_plus * det_M3) / det_M2
b = sqrt(2)/2 * sqrt(-lambda_minus * det_M3) / det_M2

# Plot
ellipse = Ellipse(xy=center, 
                  width=2 * a, 
                  height=2 * b, 
                  angle=degrees(theta), 
                  edgecolor='red', 
                  linestyle='-', linewidth=2, fill=False)

fig, ax = plt.subplots(figsize=(8, 4))
ax.set_aspect('equal', adjustable='box')

ax.add_patch(ellipse)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Ellipse')
ax.legend()
ax.grid(False)
             
plt.axis('equal')
plt.show()

推荐答案

ax.set_aspect('equal', adjustable='box')将绘图的纵横比设置为相等,这意味着在轴的边界框("box")内x轴上的一个单位等于y轴上的一个单位,而plt.axis('equal')确保绘图区域(轴界限)被调整,使得x轴上的刻度单位的比率与y轴上的刻度单位的比率相同.但是,实际上你可以通过自动zoom 来实现这一点:

sqrt = np.sqrt
atan = np.arctan
degrees = np.degrees
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

A = 1 ; B = -1.25 ; C = 1.625 ; D = 1.875 ; E = 1.5 ; F = -2

det_M2 = (4*A*C - B**2) / 4
det_M3 = (B*D*E - (A*(E**2) + C*(D**2)) - (B**2 - 4*A*C)*F) / 4
xc = (B*E - 2*C*D) / (4*det_M2)
yc = (B*D - 2*A*E) / (4*det_M2)
center = [xc, yc]
theta = atan(B/(A-C)) / 2
lambda_plus = (A+C)/2 + sqrt(((A+C)/2)**2 - det_M2)
lambda_minus = (A+C)/2 - sqrt(((A+C)/2)**2 - det_M2)
a = sqrt(2)/2 * sqrt(-lambda_plus * det_M3) / det_M2
b = sqrt(2)/2 * sqrt(-lambda_minus * det_M3) / det_M2

ellipse = Ellipse(xy=center, 
                  width=2 * a, 
                  height=2 * b, 
                  angle=degrees(theta), 
                  edgecolor='red', 
                  linestyle='-', linewidth=2, fill=False)

fig, ax = plt.subplots()
ellipse = Ellipse(xy=center, width=2*a, height=2*b, angle=np.degrees(theta),
                  edgecolor='red', fill=False, linewidth=2, linestyle='-')

ax.add_patch(ellipse)
ax.set_aspect('equal')
ax.relim()  
ax.autoscale_view()  
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Ellipse')

plt.show()

它给你

enter image description here

Python-3.x相关问答推荐

Paramiko SFTPClient get()和put()函数的通过/失败结果?

按小时和日期对Pandas 数据帧进行分组

如何在matplotlib中显示次要刻度标签

无法使用Python发送带有参数和标头的POST请求

调用 Clear 时 Airflow 会加载新代码吗

通过在不重新索引的情况下采用最高概率的百分比,有效地转换 0/1 列表中的概率列表

Python base64.b32hexencode 未创建预期结果

无法使用 Python 和 Selenium 检索 href 属性

Python - 使用 OpenCV 将字节图像转换为 NumPy 数组

在 Python 3.5 中使用 aiohttp 获取多个 url

Python:在 map 对象上调用列表两次

混合全局/参数和名为top的函数的奇怪python行为

Python 的 unittest 和 unittest2 模块有什么区别?

计数大于Pandas groupby 中的值的项目

使用 urllib3 忽略证书验证

带有自定义标头的 urllib.urlretrieve

为什么异步库比这个 I/O 绑定操作的线程慢?

将 Python 字节转换为无符号 8 位整数

如何在 Python 3.4 中使用 pip 3?

在 PostgreSQL 上使用 SQLAlchemy 创建全文搜索索引