在使用python的matplotlib库方面,我几乎是个初学者。我使用matplotlib.pyplot.plot赋值在同一图中绘制了10个图。我想从护林员中选择比默认颜色“短”的颜色,例如“在红色周围”(从红紫色到橙色)。有可能吗?我怎么用python来做呢?
我也找到了这个https://matplotlib.org/examples/color/colormaps_reference.html,但它对我来说似乎很没用。
感谢您的回复
编辑:为了更好地阐明我需要什么,我正在寻找一种方法来与"plt.plot“函数通信,我希望它在一组不同的颜色上迭代
发布于 2020-04-10 04:59:18
如果要创建色带,可以执行以下操作。使用https://matplotlib.org/3.2.1/tutorials/colors/colormap-manipulation.html作为参考:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
def plot_examples(colormaps):
"""
Helper function to plot data with associated colormap.
"""
np.random.seed(19680801)
data = np.random.randn(30, 30)
n = len(colormaps)
fig, axs = plt.subplots(1, n, figsize=(n * 2 + 2, 3),
constrained_layout=True, squeeze=False)
for [ax, cmap] in zip(axs.flat, colormaps):
psm = ax.pcolormesh(data, cmap=cmap, rasterized=True, vmin=-4, vmax=4)
fig.colorbar(psm, ax=ax)
plt.show()
colors = ["purple", "red"]
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)
plot_examples([cmap1])

您还可以使用色彩映射表来获取法线图的值:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Creating the colar map
colors = ["purple", "red"]
cmap1 = LinearSegmentedColormap.from_list("mycmap", colors)
# Data used in plot
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
plt.plot(t, s, color=cmap1(0.1))
plt.show()在这里,您可以更改倒数第二行中的0.1,以选择要在色彩映射表上查询的位置(0-255)。

https://stackoverflow.com/questions/61129897
复制相似问题