2017-06-29 203 views
2

我想提出一个2D的颜色映射numpy的meshgrid:如何提高颜色分辨率在Python matplotlib颜色表

X, Y = np.meshgrid(fields, frequencies) 
cs = ax.contourf(X, Y, fields_freqs_abs_grid, cmap="viridis", N=256) 

在fields_freqs_abs_grid的值,这是由颜色绘制,已对数缩放。

由python的matplotlib生成的颜色映射非常粗糙 - 即使对于RGB像素的数量使用“N = 256”,它也可以缩放超过8种颜色。将N增加到2048并没有改变任何东西。在相同数据上使用MatLab语言的绘图会生成色彩分辨率显着更高的色彩映射。如何增加在Python中映射的颜色数量?

结果为:enter image description here

但我希望得到的结果是:enter image description here

谢谢!

+2

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

回答

1

​​绝对有效,可以给你一个高分辨率的图像。我在下面的例子中实现了他的想法。

在关于使用contourf(),我不知道这是否是一个版本相关的问题,但在最近的版本, contourf()没有kwarg为N

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

下面的代码从官方的example修改,比较分辨率与不同的N。如果有一个版本的问题,这个代码给出了下面的情节与python 3.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() 

enter image description here