[英]Piecewise impclit functions in matplotlib (Python)
I would like to plot the function x^3 - 3xy + y^3 = 0
(Folium of Descartes) but with different colors in quadrant 2 and 4, and at the peak of the leaf I want the colors to change.我想要 plot function
x^3 - 3xy + y^3 = 0
(笛卡尔叶)但在第 2 象限和第 4 象限中有不同的 colors,并且在叶的峰值处我希望 colors 改变。 I was thinking about converting to a piecewise parametric equation to make the plotting in different colors easier, but I don't know how to convert into a parametric form.我正在考虑转换为分段参数方程以使不同 colors 中的绘图更容易,但我不知道如何转换为参数形式。
I was originally using a contour to plot the function only in one color, but I have no idea how to get the color changes.我最初只使用一种颜色的轮廓到 plot function,但我不知道如何改变颜色。
Below is what I have for the single color, but I would like to have separate colors for each of the segments listed above.下面是我所拥有的单一颜色,但我想为上面列出的每个部分分别设置 colors。
import matplotlib.pyplot as plt
import numpy as np
xrange = np.arange(-5, 5, .025)
yrange = np.arange(-5, 5, .025)
X, Y = np.meshgrid(xrange, yrange)
plt.contour(X, Y, X**3 - 3*X*Y + Y**3, levels=[0], colors=['#000000'])
plt.show()
This website gives parametric Cartesian and polar equations of the curve. 该网站给出了曲线的参数笛卡尔和极坐标方程。 The parametric equation isn't symmetric in x and y, which is annoying for drawing nice curves.
参数方程在 x 和 y 上不对称,这对于绘制漂亮的曲线来说很烦人。
The polar equation gives a radius rho for a given angle theta.极坐标方程给出了给定角度 theta 的半径 rho。 Some experimenting shows that the tails are formed by theta negative, or larger than pi/2.
一些实验表明,尾巴是由 theta negative 或大于 pi/2 形成的。 The cusp is at pi/4.
尖点位于 pi/4。
To visualize the polar curve, x=rho*cos(theta)
and y=rho*sin(theta)
can be used.要可视化极坐标曲线,可以使用
x=rho*cos(theta)
和y=rho*sin(theta)
。 Coloring can happen depending on theta.着色可以根据 theta 发生。
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
import numpy as np
a = 1
tail = 0.5
theta = np.linspace(-tail, np.pi / 2 + tail, 500)
rho = 3 * a * np.sin(theta) * np.cos(theta) / (np.cos(theta) ** 3 + np.sin(theta) ** 3)
x = rho * np.cos(theta)
y = rho * np.sin(theta)
cmap = LinearSegmentedColormap.from_list('', ['red', 'gold', 'blue'])
# cmap = ListedColormap(['red', 'blue'])
# cmap = 'Spectral'
plt.scatter(x, y, c=theta, cmap=cmap, s=1)
plt.axis('equal')
plt.axis('off')
plt.show()
Copying the leave 4 times, and filling them in decreasing order creates the following plot.复制 leave 4 次,并按降序填充它们,创建以下 plot。
import matplotlib.pyplot as plt
import numpy as np
for a in np.linspace(1, 0.001, 50):
theta = np.linspace(0, np.pi / 2, 500)
rho = 3 * a * np.sin(theta) * np.cos(theta) / (np.cos(theta) ** 3 + np.sin(theta) ** 3)
x = rho * np.cos(theta)
y = rho * np.sin(theta)
for i in [-1, 1]:
for j in [-1, 1]:
plt.fill(i * x, j * y, c=plt.cm.summer(1 - a))
plt.axis('equal')
plt.axis('off')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.