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
Pandas Plot: scatter plot with index
提问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 Graipher
You are mixing two styles, matplotlib
and the pandas
interface to it. Either do it like @anky_91suggested in their answer, or use matplotlib
directly:
您正在混合两种风格,matplotlib
以及pandas
它的界面。要么像他们的回答中建议的@anky_91那样做,要么直接使用:matplotlib
import matplotlib.pyplot as plt
plt.scatter(df.index, df.value)
plt.xlabel("index")
plt.ylabel("value")
plt.show()