python - Pandas 图 : scatter plot with index

标签 python pandas matplotlib plot

这个问题在这里已经有了答案:





Scatter plot form dataframe with index on x-axis

(2 个回答)


2年前关闭。




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

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

我想在 x 轴上创建一个带有索引的散点图,只使用 Pandas 数据框
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.reset_index().plot.scatter(x = 'index', y = 'value')

enter image description here

关于python - Pandas 图 : scatter plot with index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55169540/

相关文章:

python - 在企业防火墙后面验证谷歌云

python - 以不寻常的方式对 Pandas 数据框进行分组

python - 如何联合更新两个数据帧?

python - 从使用 matplotlib 绘制的数据进行推断

matplotlib - 是否可以使用 Matplotlib imshow() 将二维数组显示为极坐标图?

python - Pandas 数据帧操作中不受支持的操作数

python - 在 addConstraint 中使用变量会产生错误的答案,但不使用变量则可以正常工作

python - 未使用 .xs() 和 .get_level_values() 在切片上设置值

python - 带日期的 Matplotlib - 更改每月数据的标签和刻度

python - Python 无法识别 Windows 上包含回车符的文件名