【问题标题】:Pandas Plot: scatter plot with index [duplicate]熊猫图:带索引的散点图[重复]
【发布时间】:2019-03-15 02:34:03
【问题描述】:

我正在尝试从 pandas 数据框创建散点图,但我不想使用 matplotlib plt。以下是脚本

df:
group people value
   1    5    100
   2    2    90
   1    10   80
   2    20   40
   1    7    10

我想在 x 轴上创建一个带有索引的散点图,只使用 pandas datframe

df.plot.scatter(x = df.index, y = df.value)

它给了我一个错误

Int64Index([0, 1, 2, 3, 4], dtype='int64') not in index

我不想用

plt.scatter(x = df.index, y = df.value)

如何使用 pandas 数据框执行此图

【问题讨论】:

  • 试试 df.plot.scatter(x = df.index.tolist(), y = df.value)
  • 它不适合我

标签: python pandas matplotlib plot


【解决方案1】:

您可以尝试使用:

df.reset_index().plot.scatter(x = 'index', y = 'value')

【讨论】:

    【解决方案2】:

    您正在混合两种样式,matplotlibpandas 接口。要么像their answer 中建议的@anky_91 那样做,要么直接使用matplotlib

    import matplotlib.pyplot as plt
    
    plt.scatter(df.index, df.value)
    plt.xlabel("index")
    plt.ylabel("value")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 2020-11-16
      • 1970-01-01
      • 2019-07-12
      • 2015-02-12
      • 1970-01-01
      • 2019-01-23
      • 2017-05-07
      相关资源
      最近更新 更多