【问题标题】:scatter plot pandas one variable on x axis and y axis is dataframe index散点图 pandas x 轴和 y 轴上的一个变量是数据帧索引
【发布时间】:2017-05-28 19:34:16
【问题描述】:

我想在 pandas 中制作散点图,x 轴是平均值,y 轴应该是数据框的索引,但我无法继续这是我的代码,我遇到了很多错误。

y=list(range(len(df.index)))    
df.plot(kind='scatter', x='meandf', y  )    
error : SyntaxError: positional argument follows keyword argument

【问题讨论】:

  • 该错误本质上是说在您使用关键字 args 之后它不知道如何处理变量 y。请尝试以下操作:df.plot(kind='scatter', x='meandf', y=y )
  • 谢谢,现在我又遇到一个错误:IndexError: indices are out-of-bounds
  • 我在下面添加了答案。

标签: pandas scatter-plot


【解决方案1】:

尝试以下方法:

y=list(range(len(df.index)))

df.meandf.plot(x='meandf', y=y)

或者更简洁,因为您正在绘制Series

df.meandf.plot(y=y)

如果您需要维护kind = 'scatter',您需要传递一个数据框:

df['y'] = y # create y column for the list you created
df.plot(x='a', y='y', kind='scatter', marker='.')
df.drop('y', axis=1, inplace=True)

【讨论】:

  • 谢谢,它运作良好,另一个问题,如果我想为每个点添加一个点,我必须输入 kind=' ' ?
  • @sunny 还添加了注意事项,意识到您故意使用kind='scatter'。立即查看修改。
  • 谢谢!很好。
最近更新 更多