我试图制作一个显示许多线条的图,但很难区分它们。它们有不同的颜色,但我想让它很容易显示哪条线是哪条线。普通的图例并不能很好地工作,因为我有超过 10 行。
这些行遵循逻辑顺序。我想(1)从颜色图中自动选择它们的颜色(最好是具有平滑排序的颜色,例如 viridis 或彩虹)。然后我希望(2)在颜色条旁边有刻度线以对应于索引i
对于每一行(或者更好的是来自字符串数组的文本标签textlabels[i]
).
这是一小段代码(有一些空白,我不确定要使用什么)。我希望这能说明我正在尝试的事情。
import numpy as np
import matplotlib.pyplot as plt
# Genereate some values to plot on the x-axis
x = np.linspace(0,1,1000)
# Some code to select a (discrete version of) a rainbow/viridis color map
...
# Loop over lines that should appear in the plot
for i in range(0,9):
# Plot something (using straight lines with different slope as example)
plt.plot(i*x)
# Some code to plot a discrete color bar next
# to the plot with ticks showing the value of i
...
我目前有这个。我希望颜色条上的刻度值为i
,即 0, 1, 2, ... 旁边作为刻度线。
我现在拥有的示例图。现在很难区分这些线条。 https://i.stack.imgur.com/E2oxm.png
一个人通过获取颜色图plt.get_cmap("name of cmap", number_of_colors)
。
该颜色图可用于计算绘图的颜色。它还可用于生成颜色条。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
n = 10 # how many lines to draw or number of discrete color levels
x = np.linspace(0,1,17)
cmap = plt.get_cmap("viridis", n)
for i in range(0,n):
plt.plot(i*x, color=cmap(i))
norm= matplotlib.colors.BoundaryNorm(np.arange(0,n+1)-0.5, n)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
plt.colorbar(sm, ticks=np.arange(0,n))
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)