python - 在散点图中,我想自动填充轴

标签 python pandas matplotlib

我正在从单列文本文件加载数据。我想用这些数据绘制散点图,但我只有一个轴(加载的数据)。对于我的其他轴,我只想向上计数(即 0,1,2,3,4,5,...),直到加载的每个值都有一个匹配的值。

感谢您提前提供的任何帮助!

最佳答案

考虑示例文件txt

from io import StringIO
import pandas as pd

txt = """4
5
2
6
1
4
3
6
2
7"""

s = pd.read_csv(StringIO(txt), header=None, squeeze=True)

分配给系列s的默认索引将为0,系列长度减去1。您可以重置索引以将其放入数据框中并相应地进行绘制。

s.reset_index(name='x').plot.scatter('x', 'index')

enter image description here

关于python - 在散点图中,我想自动填充轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43179920/

相关文章:

python - 如何在没有 for 循环的情况下比较 pandas 中的两个数据帧?

matplotlib - 如何在 matplotlib 或 seaborn 中使用 Hershey 字体?

python - Linux 上的图形没有成熟的窗口管理器?

python - 无法在 NLTK 中导入 - Python

用于 Google 表格的 Python : Create new sheet (in the same workbook) automatically and write a new dataframe into it

python - 如何根据列子集中值的存在按行创建 pandas DataFrame 列?

python - 将 set_array 与 pyplot.pcolormesh 一起使用会破坏图形

python - Matplotlib savefig 带有图外的图例

python - 使用 Selenium 创建和执行 Javascript 函数

python - matplotlib 的 'webagg' 后端可以与我的 Django 站点一起使用吗?