简体   繁体   English

matplotlib 轮廓 plot:得到虚假线

[英]matplotlib contour plot: getting spurious lines

I have the following code to plot the following 2D function:我有以下代码到 plot 以下二维 function:

$$ \frac{x^2 + y^2}{x*y+1} = 4 $$

The code is as follows:代码如下:

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') 

I get a spurious lines.我得到一条虚假的线路。

在此处输入图像描述

Any help regarding the issue is much appreciated.非常感谢有关该问题的任何帮助。

I think this is related to the relatively coarse resolution of the image your analyzing.我认为这与您分析的图像分辨率相对较粗有关。

If I crank the number of points in your calls to linspace , I get a better looking plot:如果我在您对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.

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