[英]matplotlib contour plot: getting spurious lines
我有以下代码到 plot 以下二维 function:
$$ \frac{x^2 + y^2}{x*y+1} = 4 $$
代码如下:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
xlist = np.linspace(-5.0, 5.0, 100) # Create 1-D arrays for x,y dimensions
ylist = np.linspace(-5.0, 5.0, 100)
X,Y = np.meshgrid(xlist, ylist) # Create 2-D grid xlist,ylist values
F = (X**2 + Y**2)/(X*Y+1)
CS = ax.contour(X, Y, F, [4.0],linewidths=0.5, colors='k')
ax.clabel(CS, inline=True, fontsize=10)
ax.grid(b=True, which='major', axis='both')
我得到一条虚假的线路。
非常感谢有关该问题的任何帮助。
我认为这与您分析的图像分辨率相对较粗有关。
如果我在您对linspace
的调用中增加点数,我会得到一个更好看的 plot:
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(8, 8))
for n, ax in zip([50, 100, 500, 1000], axes.flat):
xlist = np.linspace(-5.0, 5.0, n) # Create 1-D arrays for x,y dimensions
ylist = np.linspace(-5.0, 5.0, n)
X,Y = np.meshgrid(xlist, ylist) # Create 2-D grid xlist,ylist values
F = (X**2 + Y**2)/(X*Y+1)
CS = ax.contour(X, Y, F, [4.0],linewidths=0.5, colors='k')
ax.clabel(CS, inline=True, fontsize=10)
ax.grid(b=True, which='major', axis='both')
ax.set_title(f"N ={n}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.