如何在等高线中创建交替的非重叠虚线

2024-09-27 07:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用交替且不重叠的红色和绿色虚线标记函数的一个特定轮廓。我的第一个想法是使用参数化的dash minilanguage,但这会产生一个错误:

import numpy as np
import matplotlib.pyplot as plt

# prepare the data
delta = 0.025
x = np.arange(-1.0, 1.0, delta)
y = np.arange(-1.0, 1.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2

# the actual plotting
plt.contour(X, Y, Z, levels=[1.1], linestyles=(0, (5, 5)), colors="red", alpha=0.5)
plt.contour(X, Y, Z, levels=[1.1], linestyles=(5, (5, 5)), colors="green", alpha=0.5)

plt.show()
ValueError: Do not know how to convert [0] to dashes

有什么提示吗


Tags: thetoimportalphaasnppltcontour
1条回答
网友
1楼 · 发布于 2024-09-27 07:22:11

缺少[]括号:

import numpy as np
import matplotlib.pyplot as plt

# prepare the data
delta = 0.025
x = np.arange(-1.0, 1.0, delta)
y = np.arange(-1.0, 1.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2

# the actual plotting
plt.contour(X, Y, Z, levels=[1.1], linestyles=[(0, (5, 5))], colors="red")
plt.contour(X, Y, Z, levels=[1.1], linestyles=[(5, (5, 5))], colors="green")

plt.show()

enter image description here

感谢@JohanC的回复

相关问题 更多 >

    热门问题