Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理;“像素化”;Matplotlib中重叠曲线的定义_Python_Matplotlib_Plot_Curve - Fatal编程技术网

Python 如何处理;“像素化”;Matplotlib中重叠曲线的定义

Python 如何处理;“像素化”;Matplotlib中重叠曲线的定义,python,matplotlib,plot,curve,Python,Matplotlib,Plot,Curve,我有以下问题:当我绘制两条或多条曲线(线宽相同)时,它们在重叠的区域看起来“像素化”。 下面是一个例子: x = np.linspace(0, np.pi, 100) y1 = np.sin(x) y2 = np.zeros(x.shape) y2[:51] = np.sin(x[:51]) y2[51:] = 2 - np.sin(x[51:]) fig, ax = plt.subplots(1, 1, figsize=(3,3)) ax.plot(x, y1, lw=4, color='

我有以下问题:当我绘制两条或多条曲线(线宽相同)时,它们在重叠的区域看起来“像素化”。 下面是一个例子:

x = np.linspace(0, np.pi, 100)
y1 = np.sin(x)
y2 = np.zeros(x.shape) 
y2[:51] = np.sin(x[:51])
y2[51:] = 2 - np.sin(x[51:])

fig, ax = plt.subplots(1, 1, figsize=(3,3))
ax.plot(x, y1, lw=4, color='k')
ax.plot(x, y2, lw=4, color='yellow')
plt.show()

据我所知,之所以会出现这种情况,是因为曲线边缘的像素具有一定程度的透明度,这使它们看起来更平滑。但是,在重叠曲线的情况下会有这种副作用:第一条曲线的边缘像素通过第二条曲线的边缘像素可见


如何处理这种副作用?一个想法是增加最后一条曲线的宽度,但是这个解决方案对我来说并不理想,因为我希望曲线在不重叠的区域具有完全相同的宽度

您描述的效果称为抗锯齿。通常需要获得更平滑的图像。您可以根据每个艺术家禁用抗锯齿。(然而,艺术家们确实遵守每个后端的设置。)

对于
绘图
,这可能看起来像

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 100)
y1 = np.sin(x)
y2 = np.zeros(x.shape) 
y2[:51] = np.sin(x[:51])
y2[51:] = 2 - np.sin(x[51:])

fig, axes = plt.subplots(1,2, figsize=(6,3))
for aa, ax in zip([True, False], axes.flat):
    ax.plot(x, y1, lw=4, color='k', antialiased=aa)
    ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
plt.show()


因此,当禁用抗锯齿时,像素要么被线条的相应颜色占用,要么不被占用

谢谢,我不知道。但是,将“抗锯齿”设置为False会使曲线看起来是像素化的,因此这解决了一个问题,但会创建另一个问题。似乎最好的选择是增加第二条曲线的宽度并保持抗锯齿。