简体   繁体   English

matplotlib.pyplot.imshow 中的神器

[英]Artifact in matplotlib.pyplot.imshow

I'm trying to make a colorplot of a function with matplotlob.pyplot.imshow.我正在尝试使用 matplotlob.pyplot.imshow 制作 function 的彩色图。 However, depending on the size of the plot, I get a vertical line as an artifact.但是,根据 plot 的大小,我会得到一条垂直线作为伪影。

The code to generate the plot is:生成 plot 的代码是:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
from matplotlib import cm

def double_vortex(X,Y):
    return np.angle((X + 25)+1j*Y) - np.angle((X - 25)+1j*Y)

X = np.arange(-50,50)
Y = np.arange(-50,50)

X, Y = np.meshgrid(X, Y)
phi0_vortex = double_vortex(X,Y)
 
fig = plt.figure(figsize=(16,8)) 
gs = gridspec.GridSpec(1, 3, width_ratios=[2.5, 1.5,1]) 
for i in range(3):
    ax = plt.subplot(gs[i])
    ax.imshow(phi0_vortex % (2*np.pi), cmap=cm.hsv, vmin=0, vmax=2*np.pi)

The resulting plot is this:生成的 plot 是这样的:在此处输入图像描述

You can see that the two smaller plots exhibit a vertical line as an artefact.您可以看到两个较小的图显示一条垂直线作为人工制品。 Is this a bug in matplotlib or somehow actually to be expected?这是 matplotlib 中的错误还是实际上可以预期的错误?

This is a consequence of matplotlib's downsampling algorithm, which happens in data space, and in your case a pair of pixels that has [359, 1] in them, get averaged to 180, and you get the cyan line.这是 matplotlib 的下采样算法的结果,该算法发生在数据空间中,在您的情况下,一对像素中包含 [359, 1] 的像素平均为 180,您会得到青色线。 This is https://github.com/matplotlib/matplotlib/issues/18735 for which we are working on a solution to allow RGB-space downsampling (as well).这是https://github.com/matplotlib/matplotlib/issues/18735 ,我们正在研究一种允许 RGB 空间下采样(以及)的解决方案。

What can you do about this until that is improved in Matplotlib?在 Matplotlib 改进之前,你能做些什么呢? Don't downsample in Matplotlib is the simple answer - make a big png, and then resample in post-processing software like imagemagick.不要在 Matplotlib 中下采样是简单的答案 - 制作一个大 png,然后在 imagemagick 等后处理软件中重新采样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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