用不同的颜色绘制熊猫点

时间:2017-05-10 16:24:33

标签: pandas matplotlib

我有一个两列['frequency','color']的pandas数据框,它看起来像这样:

 name  frequency  color
0   351   r
1   122   r
2   30    g
3   85    r
4   195   r
5   88    g
6   130   r
7   85    r
8   41    r
9   9     g

我想根据颜色绘制排序的“频率”。我试过这个:

plt.scatter(y=np.sort(data['frequency']),x=range(len(data['frequency'])),c=np.sort(data['color']))

我收到以下错误:

  

ValueError:to_rgba:无效的rgba arg“['r']”to_rgb:无效的rgb arg   “('r',)”序列长度为1;必须是3或4

代码有什么问题?

1 个答案:

答案 0 :(得分:4)

下图
enter image description here

由此代码生成:

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

frequency = [351,122,30,85,195,88,130,85,41,9]
color = ["r","r","g","r","r","g","r","r","r","g"]
df = pd.DataFrame( {"frequency" : frequency, "color" : color})
df.sort_values("frequency", inplace=True)

plt.scatter(x=range(len(df)), y= df["frequency"], c = df["color"])
plt.show()