【发布时间】:2021-06-27 06:45:22
【问题描述】:
我正在尝试根据数据框的一列在散点图中添加一条水平线 - 我收到以下错误:ValueError: DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
x_line = datLong.groupby('ctr1').agg({'maxx': ['mean']})
for country in datLong.ctr1.unique():
temp_df = plt.figure(country)
temp_df = datLong[datLong.ctr1 == country]
ax1 = temp_df.plot(kind='scatter', x='x', y='Price', color='#d95f0e', label = 'xx', linewidth =3, alpha = 0.7, figsize=(7,4))
plt.title(country)
plt.axvline(x=x_line) ### this is the line that is causing this error
plt.show()
print (ax1)
问题似乎与数据框有关。但我能弄清楚它是什么?谁能帮帮我
【问题讨论】:
-
plt.axvline要求您传递一个数字,但x_line是一个数据框。 -
谢谢,@Swier,我试图转换为 .numeric 但它不起作用(错误:在“float”和“function”实例之间不支持 >')。在这种情况下,我需要有几个值(每个值对应一个国家),想法是为每个国家地块添加线。
标签: python pandas matplotlib axvline