我想在matplotlib cmap 中将黄色更改为goldenrod
RdYlBu
。目前,RdYlBu
中心的黄色太亮,很难看清。我尝试了各种方法,但没有找到解决这个问题的方法。
没有其他人发现
RdYlBu
cmap 中心太亮吗?
以下是我通过 bing AI 找到的一些答案。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Get the RdYlBu colormap
rdylbu = plt.cm.get_cmap('RdYlBu')
# Get the colors from the colormap
colors = rdylbu(np.linspace(0, 1, rdylbu.N))
# Define the range of colors to change (for example, all shades of yellow)
color_range = range(rdylbu.N//2 - 10, rdylbu.N//2 + 10)
# Create a gradient from yellow to goldenrod
yellow = np.array([1, 1, 0, 1])
goldenrod = np.array(plt.cm.get_cmap('Set2')(5))
for i in color_range:
t = (i - color_range.start) / len(color_range) # Normalized value in [0, 1]
colors[i] = yellow * (1 - t) + goldenrod * t # Linear interpolation
# Create a new colormap from the modified colors
new_cmap = LinearSegmentedColormap.from_list("RdYlBu_mod", colors)
xs = list(range(25))
ys = list(range(25))
sc = plt.scatter(xs, ys, c=ys, s=10, cmap=new_cmap)
cbar = plt.colorbar(sc)
# Use the new colormap
# plt.imshow(, cmap=new_cmap)
plt.show()
但是,这不是一个完整的解决方案,因为它仅用新颜色替换颜色图的中间部分。新插入的goldenrod颜色不会与现有的cmap以平滑的渐变方式连接。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Get the RdYlBu colormap
rdylbu = plt.cm.get_cmap('RdYlBu')
# Get the colors from the colormap
colors = rdylbu(np.linspace(0, 1, rdylbu.N))
# Define the range of colors to change (for example, all shades of yellow)
color_range = range(rdylbu.N//2 - 10, rdylbu.N//2 + 10)
# Create a gradient from yellow to goldenrod
yellow = np.array([1, 1, 0, 1])
goldenrod = np.array(plt.cm.get_cmap('Set2')(5))
for i in color_range:
t = (i - color_range.start) / len(color_range) # Normalized value in [0, 1]
colors[i] = yellow * (1 - t) + goldenrod * t # Linear interpolation
# Create a new colormap from the modified colors
new_cmap = LinearSegmentedColormap.from_list("RdYlBu_mod", colors)
xs = list(range(25))
ys = list(range(25))
sc = plt.scatter(xs, ys, c=ys, s=10, cmap=new_cmap)
cbar = plt.colorbar(sc)
plt.show()
此代码创建一个新的颜色图,其中黄色被替换为从黄色到金黄色的渐变。这应该会使颜色图的中心不那么明亮并且更容易看到。让我知道这是否有帮助! 😊