pandas 熊猫图:带索引的散点图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55169540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:20:58  来源:igfitidea点击:

Pandas Plot: scatter plot with index

pythonpandasmatplotlibplot

提问by Manu Sharma

I am trying to create a scatter plot from pandas dataframe, and I dont want to use matplotlib plt for it. Following is the script

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

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

I want to create a scatter plot with index on x axis, only using pandas datframe

我想在 x 轴上创建一个带有索引的散点图,只使用 Pandas 数据框

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

it gives me an error

它给了我一个错误

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

I dont want to use

我不想使用

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

how to perfom this plot with pandas dataframe

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

回答by anky

You can try and use:

您可以尝试使用:

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

enter image description here

在此处输入图片说明

回答by Graipher

You are mixing two styles, matplotliband the pandasinterface to it. Either do it like @anky_91suggested in their answer, or use matplotlibdirectly:

您正在混合两种风格,matplotlib以及pandas它的界面。要么像他们的回答中建议的@anky_91那样做,要么直接使用:matplotlib

import matplotlib.pyplot as plt

plt.scatter(df.index, df.value)
plt.xlabel("index")
plt.ylabel("value")
plt.show()

enter image description here

在此处输入图片说明