python colormap绘制,如何在python matplotlib colormap中增加颜色分辨率

在Python中使用matplotlib创建2D网格颜色映射时,发现即使指定较高的像素数,颜色分辨率仍然较低。文章指出,这可能是因为`contourf()`函数的参数设置问题。`N`参数实际上用于指定等高线数量而非RGB像素数。通过增加等高线条数可以提升颜色分辨率。示例代码展示了如何通过设置不同数量的等高线来比较颜色分辨率,并指出使用`imshow()`函数可以获得更高分辨率的颜色映射。
摘要由CSDN通过智能技术生成

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)

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

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?

The result is: eDLqy.png

But I want the result to be: ROzBU.png

Thank you!

解决方案

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

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.

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.

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()

bo4iW.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值