gpt4 book ai didi

python - Python中颜色条的自定义(截断并添加更多颜色)

转载 作者:行者123 更新时间:2023-12-04 09:16:20 27 4
gpt4 key购买 nike

我是 Python 新手。你能帮我对颜色条进行适当的自定义吗?我已经尝试了网络上的几种变体,但它们无法正常工作。我有以下带有颜色条的图表:
sample graph with colorbar
这是代码:

set_cmap('rainbow')
contourf(0.5*(data1+data2))
xlabel (r'across track $\rm\,[pixels]$')
ylabel (r'along track $\rm\,[pixels]$')

cbar = plt.colorbar()
cbar.set_label('radiance', rotation=270)
title('1.6 mue (avg: 5992-6012 cm^(-1))')
# Show the major grid lines
plt.grid(b=True, which='major', color='#666666', linestyle='-')
# Show the minor grid lines
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#666666', linestyle='-', alpha=0.4)`fig=plt.figure()
我需要将颜色条截断为 0 到 9 之间的值,即 0,1,2,3,4,5,6,7,8,9 并为每个值创建一个新颜色。
附言像 cbar = plt.clim(0, 9) 这样的命令不起作用。
提前致谢!

最佳答案

levels=你可以告诉contourf您想要一个新级别的值。在这种情况下,案例选择 levels=0,1,2,...,9似乎达到了你想要的。
请注意,颜色是在调用 contourf 时决定的。 .函数colorbar只是尝试可视化使用的颜色。
下面的代码首先创建了一些 0 到 20 之间的随机数据,异常值值为 50。默认调用 contourf被比作一个是 levels是明确设置的。

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np

N, M = 100, 300
x = np.random.uniform(-0.1, 0.1, (N, M)).cumsum(axis=1).cumsum(axis=0)
x -= x.min()
x *= 20 / x.max()
x[-1,-1] = 50
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12,4))
cont1 = ax1.contourf(x, cmap='rainbow')
plt.colorbar(cont1, ax=ax1)
cont2 = ax2.contourf(x, levels=np.arange(10), cmap='rainbow')
plt.colorbar(cont2, ax=ax2)
plt.show()
example plot

关于python - Python中颜色条的自定义(截断并添加更多颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63194453/

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