簡體   English   中英

繪制不同顏色的點

[英]Plot points in different colors

我有一個使用創建的數據框

results = df2[(df2['R'] > 100)].sort(columns='ZR', ascending=False)

我想要做

plt.plot(results['ZR'], marker='o')

除了我希望這些點是results ['I'] == foo為紅色,而這些點是results ['I']!= foo為藍色。

我試過了

firstset = results.ZR[results.I.str.contains('foo')]
secondset = results.ZR[~results.I.str.contains('foo)]
plt.plot(firstset, marker='o', color='red')
plt.plot(secondset, marker='o', color='blue')

但這繪制了從x軸0開始的兩半,這不是我所需要的。

在此處輸入圖片說明

相反,我將只喜歡原始圖形,但其中一些點用紅色表示,而另一些用藍色表示。 那不是新點,也不是位置改變的點。 這是原始圖。 在此處輸入圖片說明

首先,在沒有任何X參數的情況下, plt.show假定它是從1開始的連續整數。為了使用邏輯運算找到正確的索引,請執行以下操作:

firstIndex = results.index[results.I.str.contains('foo')]
secondIndex = results.index[~results.I.str.contains('foo)]

如果原始索引很復雜,請為新索引創建一個虛擬的熊貓DataFrame。

newDf = pd.DataFrame(range(len(results)))
firstIndex = newDf.index[results.I.str.contains('foo')]
secondIndex = newDf.index[~results.I.str.contains('foo')]

這樣可以確保創建兩個索引,它們是1:230子集。

接下來,將字符串qulifier傳遞到plt.plot以指定顏色和標記類型。

plt.plot(results['ZR'])
plt.plot(firstIndex, firstset, "bo")
plt.plot(secondIndex, secondset, "ro")

這將使用線繪制整個帶下划線的數據,而沒有標記點。 然后,它將用各自的顏色覆蓋兩個設定點。

暫無
暫無

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

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