简体   繁体   English

如何从 Python 中椭圆的一般方程绘制椭圆

[英]How to plot an ellipse from the general equation of an ellipse in Python

I know matplotlib could plot an ellipse according to its center, semi-major axis length,semi-minor axis length and the angle between x-axis and major axis.我知道 matplotlib 可以根据其中心、半长轴长度、半短轴长度以及 x 轴和长轴之间的角度绘制椭圆。 But is there any easy way to plot an ellipse according to its general equation just like Matlab:但是有没有什么简单的方法可以像 Matlab 一样根据椭圆的一般方程来绘制椭圆:

ezplot('3*x^2+2*x*y+4*y^2 = 5')

I find a way to calculate the center, semi-major axis length,semi-minor axis length and the angle between x-axis and major axis from the general formula.我找到了一种方法来计算中心、半长轴长度、半短轴长度以及从通用公式中 x 轴和长轴之间的角度。 This is the website: link .这是网站: 链接 I use this method and write a function to calculate parameters.我使用这种方法并编写了一个函数来计算参数。 Because I'm dealing with the data by drawing the ellipse.The experimental data give me the general equation of the ellipse.I would loop many times (for example, 500) and plot 500 ellipses on a single graph.If you do this every time before plotting, it slows the program down.So I'm looking for whether python provides a way to draw an ellipse directly from the general equation of the ellipse instead of calculating the parameters every time.因为我是通过绘制椭圆来处理数据的。实验数据给了我椭圆的一般方程。我会循环多次(例如,500)并在单个图形上绘制 500 个椭圆。如果你每次都这样做绘图前的时间,它减慢了程序的速度。所以我在寻找python是否提供了一种直接从椭圆的一般方程中绘制椭圆的方法,而不是每次都计算参数。

Thanks!谢谢!

With sympy , you just do:使用sympy ,您只需执行以下操作:

from sympy import plot_implicit, Eq
from sympy.abc import x, y

plot_implicit (Eq(3*x**2+2*x*y+4*y**2, 5))

Note that Python needs ** for the power function, as ^ is reserved to bitwise xor .请注意,Python 需要**作为幂函数,因为^保留给按位 xor The expression can either be written as 3*x**2+2*x*y+4*y**2 - 5 or using the equation operator Eq(3*x**2+2*x*y+4*y**2, 5) .表达式可以写成3*x**2+2*x*y+4*y**2 - 5或使用方程运算符Eq(3*x**2+2*x*y+4*y**2, 5)

Extra parameters to plot_implicit can set the ranges for x and y as in plot_implicit (3*x**2+2*x*y+4*y**2 - 5, (x, -2, 2), (y, -2, 2)) . plot_implicit额外参数可以设置 x 和 y 的范围,如plot_implicit (3*x**2+2*x*y+4*y**2 - 5, (x, -2, 2), (y, -2, 2))

Alternatively, to get something more fancy, matplotlibs imshow can draw a complete area in x,y colored by a z-value.或者,为了获得更奇特的效果,matplotlibs imshow可以在 x,y 中绘制一个由 z 值着色的完整区域。 Choosing a diverging colormap , the ellipse will show up at the central z-value indicated by a diverging norm .选择发散色图,椭圆将显示在发散范数指示的中心 z 值处。

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.colors as mcolors

xmin, xmax = -2, 2
ymin, ymax = -2, 2
x, y = np.meshgrid(np.linspace(xmin, xmax, 500), np.linspace(ymin, ymax, 500))
z = 3*x**2+2*x*y+4*y**2

divnorm = mcolors.DivergingNorm(vmin=z.min(), vcenter=5, vmax=z.max())

#plt.contourf(x, y, z, levels=15, norm=divnorm, cmap='seismic')
plt.imshow(z, interpolation='bilinear', norm=divnorm, cmap='seismic', origin='lower', extent=[xmin, xmax, ymin, ymax])

plt.colorbar()
plt.show()

示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM