2014-04-15 46 views
-1

色調散點圖我有一個數據集的在matplotlib

enter image description here

我想創建X和Y之間的散點圖,由國家matplotlib有色形式。 請幫忙。

回答

0
c_map = {'US': 'b', 'UK': 'r', ...} 
fig, ax = plt.subplots(1, 1) 
ax.scatter(x, y, c=[c_map[_] for _ in country]) 
+0

基本上我的數據集是在一個數據幀大熊貓爲c_map變量的形式,我使用c_map = P [「國家」]。to_dict然後使用的建議的其餘部分。但有一個錯誤'instancemethod'對象沒有屬性'__getitem__' – user40465

+0

我試過plt.plot(df ['x'],df ['y'],c = df ['country']),沒有錯誤但圖表不會彈出 – user40465

0

我試過這段代碼,它似乎解決了這個問題。

fig, ax = plt.subplots() 
    for dd,daata in P.groupby('Country'): 
    ax.plot(daata['X'],daata['Y'],'o') 
-1

在Altair解決這個問題。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

cat = ["US"] *5 + ["UK"]*4 
x = np.append(np.arange(1,6), np.arange(2.5,4.1,0.5)) 
y = np.random.randint(12,24, size=len(cat)) 
df = pd.DataFrame({"cat":cat, "x":x, "y":y}) 

from altair import * 
Chart(df).mark_point().encode(x='x', y='y', color='cat').configure_cell(width=200, height=150) 

enter image description here