【问题标题】:How to get data from columns without index如何从没有索引的列中获取数据
【发布时间】:2019-12-31 18:41:33
【问题描述】:

我是 Python 的初学者,我正在使用 Matplotlib 和 Pandas 创建堆积面积图(例如 https://python-graph-gallery.com/251-stacked-area-chart-with-seaborn-style/)。不幸的是,它不起作用。我有以下包含数据的文件:

现在我需要存储每一列的值,为此我使用以下命令:

    ts_1 = wind_data.Building_1;
    ts_2 = wind_data.Building_2;
    ts_3 = wind_data.Building_3;
    ts_4 = wind_data.Building_4;
    ts_5 = wind_data.Building_5
y= [ts_1, ts_2, ts_3, ts_4, ts_5];

如果我现在在上面示例链接的命令中使用它:

# library
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Data
x=range(1,6)


# Plot
plt.stackplot(x,y, labels=['A','B','C'])
plt.legend(loc='upper left')
plt.show()

我收到一条错误消息。我认为问题在于,当我读取变量 ts_1 中的单个列时,我并没有单独获得值,我还获得了索引,正如您在这张图片中看到的那样:

如果我只能将值存储在没有索引的列的变量中,我希望我可以创建堆栈图。我该怎么办?

编辑:jupyter 尝试合并 YOLO 的 cmets 的屏幕截图:

Exel 的屏幕截图应该是这样的:

这里是样本数据集:

Building_1  Building_2  Building_3  Building_4  Building_5
7.04    7.04    7.04    7.04    7.04
6.36    6.36    6.36    6.36    6.36
6.4     6.4     6.4     6.4     6.4
6.1     6.1     6.1     6.1     6.1
5.88    5.88    5.88    5.88    5.88
6.18    6.18    6.18    6.18    6.18
6.16    6.16    6.16    6.16    6.16
5.82    5.82    5.82    5.82    5.82
5.28    5.28    5.28    5.28    5.28
4.82    4.82    4.82    4.82    4.82
4.18    4.18    4.18    4.18    4.18
4.02    4.02    4.02    4.02    4.02
4.08    4.08    4.08    4.08    4.08
4.24    4.24    4.24    4.24    4.24
6.24    6.24    6.24    6.24    6.24
8.44    8.44    8.44    8.44    8.44
8.72    8.72    8.72    8.72    8.72
8.06    8.06    8.06    8.06    8.06
7.16    7.16    7.16    7.16    7.16
6.52    6.52    6.52    6.52    6.52
7.16    7.16    7.16    7.16    7.16
7.88    7.88    7.88    7.88    7.88
8.44    8.44    8.44    8.44    8.44
8.56    8.56    8.56    8.56    8.56

考虑到 YOLOS 代码的输出的新屏幕截图:

【问题讨论】:

  • 首先,看起来y 是一个对象列表,而不是整数-其次,我相信您需要编辑数据以制作面积图-您在使用什么@ 987654336@ 值?
  • 感谢您的回答 Datanovice。如何将 y 更改为整数列表?数据本身适用于面积图。我可以用 Excel 轻松做到这一点。我仍然认为问题在于,数据变量 ts_1, ts_2 ...additinally 包含索引

标签: python pandas matplotlib seaborn


【解决方案1】:

这是一个简单的方法:

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# sample data, in your case this is wind_data
df = pd.DataFrame({'A': [1,2,3], 'B':[2,3,4], 'C': [5,6,7], 'D': [6,7,8]})

# from here, replace df with wind_data 
x = range(df.shape[1])
y = df.values.tolist()

plt.stackplot(x,y, labels=df.columns)
plt.legend(loc='upper left')
plt.show()

或者,您也可以这样做:

wind_data.plot.area() # or you can also do df.plot.area()

【讨论】:

  • 感谢您的回答 YOLO。不幸的是,我不知道如何将此示例转移到我的数据中。我试过了,但情节看起来真的很糟糕。我刚刚复制了您的示例并将括号 [] 中的值替换为我的数据变量 ts_1, ts_2 ...: df = pd.DataFrame({'A': ts_1, 'B':ts_2, 'C': ts_3, 'D': ts_4, 'E': ts_5}) x = range(df.shape[1]) y = df.values.tolist()
  • 基本上,只需将示例中的 df 替换为您拥有的 wind_data 数据框即可。它应该工作。检查编辑。
  • 再次感谢 YOLO 的 cmets。我试图采纳你的建议。但是,它看起来仍然不正确。我在第一篇文章中包含了一个屏幕截图,显示了 jupyter 中的代码和输出。我尝试在上图中按照您的建议将 df 替换为 wind_data 。在下图中,我还通过将时间序列变量 ts_1 .. ts_5 分配给它们,将数据变量 A 更改为 E。不幸的是,这两种方式都会导致错误的输出。
  • @PeterBe 可能是你错过了%matplotlib inline,检查编辑。
  • 你可以做类似ts_1.values.tolist()
猜你喜欢
  • 2017-02-20
  • 2020-11-12
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 2012-05-31
  • 2021-04-17
相关资源
最近更新 更多