我有問題,我想在 python 中繪制多線顏色圖,我不知道我該怎么做,我的資料框是這樣的:
frame | name | color | max_val
0 | sadness | c | 0.07
24 | sadness | c | 0.054
48 | neutral | k | 0.8
等等..
我的腳本是:
enomy = {
"anger":"r",
"disgust":"g",
"fear":"m",
"happiness":"y",
"sadness":"c",
"surprise":"b",
"neutral":"k",
}
x = df["Max_value"]
y = df["frame"]
z = []
for i in range(len(x)):
z.append(enomy[list(df["name"])[i]])
fig, ax = plt.subplots()
for j in range(len(x)):
plt.plot(y, x, color=z[j])
plt.show()
這個情節是這樣的:

我如何解決這個問題并繪制多線圖?
uj5u.com熱心網友回復:
問題似乎是您每次都在里面用新顏色繪制整組線條for j。一個簡單的解決方案是每次繪制整個曲線的一部分,例如:
for j in range(len(x)-1):
plt.plot(y[j:j 2], x[j:j 2], color=z[j])
(我不確定您是否希望每條線的顏色與起點或終點相關聯。上面的代碼根據起始幀為每條線著色。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/390059.html
標籤:Python 熊猫 matplotlib
