2017-04-26 74 views
0

繪製的分佈圖我有一個數據幀如下:從數據幀

Metal     Cost per m^3/$  volume/mm^3 
0 Cast Iron    5996.0    20088.253323 
1 Medium Carbon Steel  4301.0    12636.050310 
2 Alloy Steel    6490.6    9134.975311 
3 Stainless Steels  34621.0   29216.210066 
8 Titanium Alloys   76500.0   16303.954297 

我想繪製成本VS音量。 我如何繪製不同顏色的每個點,並使用金屬列作爲圖例。

+0

請考慮[接受](http://meta.stackexchange.com/a/5235)/ upvoting一個答案,如果它是有益的 - 這也將表明,你的問題已經有了答案 – MaxU

+1

請下一次詢問,提供一些示例數據框與代碼,花了我5分鐘重現數據框和2分鐘來寫答案。 – ImportanceOfBeingErnest

回答

2

您可以迭代數據幀的行並使用pyplot.scatter來繪製點。

import pandas as pd 
import matplotlib.pyplot as plt 

a = ["Cast Iron", "Medium Carbon Steel", "Alloy Steel", 
    "Stainless Steels", "Titanium Alloys"] 
b = [5996,4301, 6490,34621,76500] 
c = [ 20088.253323, 12636.050310, 9134.975311, 29216.210066,16303.954297] 

df = pd.DataFrame({"Metal":a, "cost":b, "volume":c}) 

for row in df.iterrows(): 
    plt.scatter(row[1]["cost"], row[1]["volume"], 
       c=plt.cm.jet(row[0]/float(len(df))), label=row[1]["Metal"]) 

plt.legend() 
plt.show() 

enter image description here