簡體   English   中英

是否可以更改 matplotlib 的 vlines 中單行的顏色?

[英]Is it possible to change the color of one individual line in matplotlib's vlines?

From the matplotlib documentation I know I can use a set of colors in the colors argument of vlines like colors=['green', 'yellow', 'red'] etc, but then the colors simply rotate ie: the first line will be綠色,第二個黃色,第三個紅色,第四個綠色,第五個黃色等等。

有沒有辦法單獨訪問一行並更改其顏色?

您可以單獨使用 plot 該特定行:

ax.vlines([1,2,4,5], 0, 1, colors='red')
ax.vlines(3, 0, 1, colors='blue')

或者,您只需創建一個 colors 列表,其中包含與行一樣多的 colors。

通過get_colors()你得到當前的 colors。 如果 colors 較少,則 colors 會重復。 要更改特定行的顏色,需要創建 colors 的完整數組,之后可以更改特定的 colors。

以下示例更改了第 11 行:

import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
import numpy as np

lines = plt.vlines(np.arange(16), np.random.rand(16), np.random.rand(16) + 5,
                   colors=['lime', 'gold', 'crimson'], lw=5)

# lines = plt.gca().collections[0]
colors = lines.get_colors()
num_colors = len(colors)
num_lines = len(lines.get_paths())
new_colors = np.tile(colors, ((num_lines + num_colors - 1) // num_colors, 1))
new_colors[10] = to_rgba('darkblue')
lines.set_colors(new_colors)

plt.show()

在 plt.vlines() 中改變一行的顏色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM