【发布时间】:2017-05-19 18:24:48
【问题描述】:
我正在尝试标记我从 matplotlib 创建的散点图/气泡图,其中包含来自 pandas 数据框中的列的条目。我看过很多相关的例子和问题(参见例如here 和here)。因此,我试图相应地注释情节。这是我的工作:
import matplotlib.pyplot as plt
import pandas as pd
#example data frame
x = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
y = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600]
s = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
users =['mark', 'mark', 'mark', 'rachel', 'rachel', 'rachel', 'jeff', 'jeff', 'jeff', 'lauren', 'lauren', 'lauren']
df = pd.DataFrame(dict(x=x, y=y, users=users)
#my attempt to plot things
plt.scatter(x_axis, y_axis, s=area, alpha=0.5)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.annotate(df.users, xy=(x,y))
plt.show()
我使用了 pandas 数据帧,但不知何故得到了 KeyError- 所以我猜应该是 dict() 对象?有没有其他方法可以使用 pandas 数据框中的条目来标记数据?
【问题讨论】:
标签: python-3.x pandas matplotlib scatter-plot