gpt4 book ai didi

python - 带有自定义颜色图的 pyplot contourf 重复颜色而不是改变

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:46 26 4
gpt4 key购买 nike

我想用对数颜色代码绘制一些数据,其中十进制限制由白色/黑色界面表示。灰度级用于显示十年的一些分割。我的问题是,即使彩色 map 的条目数量正确(至少我认为),每个十年都有两个白色相邻区域。有人可以帮忙吗?

同时我做了一些测试,我发现它是没有使用的重复图案的第二种颜色(灰色(0.25)),但我仍然不知道为什么。

这是代码的简短版本:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

# generate data
x = y = np.linspace(-3, 3, 200)
im = 1800*np.exp(-(np.outer(x,x) + y**2))
im = im / im.max() # normalize

# set logarithic levels (with small steps)
levS = np.array([1e-3,2e-3,4e-3,6e-3,8e-3,
1e-2,2e-2,4e-2,6e-2,8e-2,
0.1,0.2,0.4,0.6,0.8,
1])
# (5 intervals in one decade )

# avoid white patches by filling up to lowest level
im[ im < levS.min() ] = levS.min()

# make a list of 5 colors to create a colormap from
mapColS = [plt.cm.gray(0),plt.cm.gray(0.25),plt.cm.gray(0.50),
plt.cm.gray(0.7),plt.cm.gray(0.99)]
# repeat 3 times for the three decades
mapColS = mapColS + mapColS + mapColS
MyCmap=colors.ListedColormap(mapColS) # make color map

fig13g = plt.figure(1000) #create figure
ax13g = fig13g.add_subplot(111)

# plot lines
cax = plt.contour(im, levS, linewidths = 0.5,
norm=colors.LogNorm(), colors = 'k')
# fill with colors
cax = plt.contourf(im, levS, norm=colors.LogNorm(),
cmap=MyCmap) # plt.cm.jet OR MyCmap

# show log color bar
cbar = fig13g.colorbar(cax, orientation='vertical',
spacing='regular',ticks= levS)

结果如下:

The plot with the problem

对比一下,使用'jet'没有问题:
using 'jet' there is no problem

最佳答案

问题是您使用 mapColS = mapColS + mapColS + mapColS 重复相同的颜色级别 mapColS 3 次。直接的解决方案是通过线性划分 plt.cm.gray(0)plt.cm.gray(0.99 ) 分成 15 个相等的级别作为

mapColS = [plt.cm.gray(i) for i in np.linspace(0, 0.99, 15)]

MyCmap=colors.ListedColormap(mapColS) # make color map

输出

enter image description here

关于python - 带有自定义颜色图的 pyplot contourf 重复颜色而不是改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52498858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com