Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 colormap中提高颜色分辨率_Python_Matlab_Numpy_Matplotlib_Colormap - Fatal编程技术网

如何在python matplotlib colormap中提高颜色分辨率

如何在python matplotlib colormap中提高颜色分辨率,python,matlab,numpy,matplotlib,colormap,Python,Matlab,Numpy,Matplotlib,Colormap,我正在制作2D numpy网格的彩色贴图: X, Y = np.meshgrid(fields, frequencies) cs = ax.contourf(X, Y, fields_freqs_abs_grid, cmap="viridis", N=256) 按颜色绘制的字段\u freqs\u abs\u网格中的值已按对数比例缩放 python的matplotlib生成的颜色贴图比较粗糙——它可以缩放8种颜色,尽管我使用“N=256”表示RGB像素数。将N增加到2048并没有改变任何事情。

我正在制作2D numpy网格的彩色贴图:

X, Y = np.meshgrid(fields, frequencies)
cs = ax.contourf(X, Y, fields_freqs_abs_grid, cmap="viridis", N=256)
按颜色绘制的字段\u freqs\u abs\u网格中的值已按对数比例缩放

python的matplotlib生成的颜色贴图比较粗糙——它可以缩放8种颜色,尽管我使用“N=256”表示RGB像素数。将N增加到2048并没有改变任何事情。在相同的数据上使用MatLab语言绘制,可以生成具有明显更高颜色分辨率的彩色贴图。如何增加Python中映射的颜色数量

结果是:

但我希望结果是:

谢谢大家!

绝对有效,可以为您提供高分辨率图像。我在下面的例子中实现了他的想法

关于使用
contourf()
,我不确定这是否与版本有关,但在最新版本中, 没有用于
N
的kwarg

正如您在文档中看到的,您希望使用
N
作为参数(语法:
tourtf(X,Y,Z,N)
)来指定要打印的级别,而不是RGB像素数
contourf()
绘制填充轮廓,分辨率取决于要绘制的层数。您的
N=256
不会执行任何操作,并且
contourf()
将自动选择

以下代码是从官方版本中修改的,用于比较不同
N
的分辨率。如果存在版本问题,此代码将使用
python3.5.2给出以下绘图;matplotlib 1.5.3

import numpy as np
import matplotlib.pyplot as plt

delta = 0.025

x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.set_size_inches(8, 6)

# Your code sample
CS1 = ax1.contourf(X, Y, Z, cmap="viridis", N=256)
ax1.set_title('Your code sample')
ax1.set_xlabel('word length anomaly')
ax1.set_ylabel('sentence length anomaly')
cbar1 = fig.colorbar(CS1, ax=ax1)

# Contour up to N=7 automatically-chosen levels, 
# which should give the same as your code.
N = 7
CS2 = ax2.contourf(X, Y, Z, N, cmap="viridis")
ax2.set_title('N=7')
ax2.set_xlabel('word length anomaly')
ax2.set_ylabel('sentence length anomaly')
cbar2 = fig.colorbar(CS2, ax=ax2)

# Contour up to N=100 automatically-chosen levels.
# The resolution is still not as high as using imshow().
N = 100
CS3 = ax3.contourf(X, Y, Z, N, cmap="viridis")
ax3.set_title('N=100')
ax3.set_xlabel('word length anomaly')
ax3.set_ylabel('sentence length anomaly')
cbar3 = fig.colorbar(CS3, ax=ax3)

IM = ax4.imshow(Z, cmap="viridis", origin='lower', extent=(-3, 3, -3, 3))
ax4.set_title("Warren Weckesser's idea")
ax4.set_xlabel('word length anomaly')
ax4.set_ylabel('sentence length anomaly')
cbar4 = fig.colorbar(IM, ax=ax4)

fig.tight_layout()
plt.show()

尝试使用
imshow
而不是
contourf
。使用
extent
参数设置x轴和y轴范围。