如何設置matplotlib中的線條顏色以及運行時使用顏色貼圖提供的標量值(如jet
)?我在這裏嘗試了幾種不同的方法,我想我很難過。 values[]
是一個標量數組。曲線是一組一維數組,標籤是一組文本字符串。每個陣列具有相同的長度。使用Colormaps設置matplotlib中的線條顏色
fig = plt.figure()
ax = fig.add_subplot(111)
jet = colors.Colormap('jet')
cNorm = colors.Normalize(vmin=0, vmax=values[-1])
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
lines = []
for idx in range(len(curves)):
line = curves[idx]
colorVal = scalarMap.to_rgba(values[idx])
retLine, = ax.plot(line, color=colorVal)
#retLine.set_color()
lines.append(retLine)
ax.legend(lines, labels, loc='upper right')
ax.grid()
plt.show()
這隻適用於你的'stop'是1 – Eric
當然1是最好的價值。如果你想要更大範圍的顏色,你只需要增加'number_of_lines'。如果您只想要樂隊中的一部分顏色,可以根據需要減少「停止」並增加「開始」。 – Parousia
一個簡單的問題:如何將顏色條而不是圖例添加到您的情節? –