2017-03-08 58 views
1

我目前正在繪製基於兩列數據的散點圖。但是,我想根據第三列中的類標籤給數據點着色。顏色散點圖基於第三列中的值嗎?

我的第三列中的標籤是1,2或3.我如何根據第三列中的值對散​​點圖着色?

plt.scatter(waterUsage['duration'],waterUsage['water_amount']) 
plt.xlabel('Duration (seconds)') 
plt.ylabel('Water (gallons)') 

回答

4

scatter函數愉快地獲取表示顏色的數字列表。您可以用顏色表玩也一樣,如果你想要的(但你不必):

plt.scatter(waterUsage['duration'], waterUsage['water_amount'],\ 
      c=waterUsage['third_column'], cmap=plt.cm.autumn) 
+0

用你的方法,我怎麼知道哪個顏色對應於我的'third_column'中的哪個數字? – Gary

+0

添加一個顏色條:'plt.colorbar()'。 – DyZ

0

另一條目添加到字典「色」

def addcolor(b): 

    a=b 
    for x in range(len(a['third_column'])): 
     if a['third_column'][x]==1: a['color'][x]='rosybrown' 
     elif a['third_column'][x]==2: a['color'][x]='papayawhip' 
     elif a['third_column'][x]==3: a['color'][x]='chartreuse' 
    return a 

waterUsage = addcolor(waterUsage) 

plt.scatter(waterUsage['duration'], 
      waterUsage['water_amount'], 
      c=waterUsage['color']) 

matplotlib接受灰度,RGB ,十六進制和HTML顏色:

http://matplotlib.org/api/colors_api.html

HTML顏色列表,按組:

https://www.w3schools.com/colors/colors_groups.asp

相關問題