如何在python matplotlib colormap中增加颜色分辨率 [英] How to increase color resolution in python matplotlib colormap

查看:209
本文介绍了如何在python matplotlib colormap中增加颜色分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作2D numpy meshgrid的颜色图:

I am making a colormap of a 2D numpy meshgrid:

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

fields_freqs_abs_grid中按颜色绘制的值已经按对数比例缩放.

The values in fields_freqs_abs_grid, which are plotted by color, have already been logarithmically scaled.

由python matplotlib生成的颜色图是粗糙的-即使我将"N = 256"用作RGB像素数,它也可以缩放8种颜色.将N增加到2048并没有任何改变.在相同的数据上使用MatLab语言绘制的图将产生具有明显更高的颜色分辨率的色图.如何增加在Python中映射的颜色数量?

The colormap produced by python's matplotlib is coarse -- it scales over 8 colors even though I use "N=256" for the number of RGB pixels. Increasing N to 2048 did not change anything. A plot using the MatLab language on the same data produces a colormap with significantly higher color resolution. How do I increase the number of colors mapped in Python?

结果是:

但是我希望结果是:

谢谢!

推荐答案

Warren Weckesser's comments definitely works and can give you a high resolution image. I implemented his idea in the example below.

在使用contourf()时,我不确定这是否是版本相关的问题,但是在最新版本中, contourf() .

In regarding to use contourf(), I'm not sure if this is a version dependent issue, but in the most recent version, contourf() doesn't have a kwarg for N.

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

As you can see in the document, you want to use N as an arg (in syntax: contourf(X,Y,Z,N)) to specify how many levels you want to plot rather than the number of RGB pixels. contourf() draws filled contours and the resolution depends on the number of levels to draw. Your N=256 won't do anything and contourf() will automatically choose 7 levels.

以下代码是从官方示例修改而来,比较了具有不同N.如果存在版本问题,此代码将使用python 3.5.2; matplotlib 1.5.3给出以下图表:

The following code is modified from the official example, comparing resolutions with different N. In case there is a version issue, this code gives the following plot with 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()

这篇关于如何在python matplotlib colormap中增加颜色分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆