在图旁边显示(离散)颜色条,作为(自动选择的)线条颜色的图例

用户名

我试图绘制一个显示多条线的图,但是很难区分它们。它们具有不同的颜色,但是我想使显示哪条线更容易。一个普通的图例并不能很好地工作,因为我的行数超过10条。

这些行遵循逻辑顺序。我想(1)从颜色表中自动选择它们的颜色(最好是具有平滑顺序的颜色,例如翠绿或彩虹色)。然后,我想(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...。
我现在拥有的示例图。现在很难区分。

认真的重要性

通过获取一个颜色图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()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章