【问题标题】:Pandas: Plot X-axis values from dataframe columns and Y-axis values from dataframe rowsPandas:绘制数据框列的 X 轴值和数据框行的 Y 轴值
【发布时间】:2017-07-18 01:07:40
【问题描述】:

我有这个数据集,其中的列是跨年份的值,而行的值是英镑:

         total_2013  total2014  total_2015  total_2016
0         1569000    1614000     1644000     1659000
1          330000     423000      474000      540000
2         2100000    2080000     2093000     2135000
3         2161000    2238000     2221000     2200000
4         1865000    1975000     2046000     2152000
5         1903000    1972000     1970000     2034000
6         2087000    2091000     1963000     1956000
7         1237000    1231000     1199000     1188000
8         1043000    1072000     1076000     1059000
9          569000     610000      564000      592000
10        2207000    2287000     2191000     2274000
11        1908000    1908000     1917000     1908000

我需要在 X 轴上绘制 total_2013、total_2014、total_2015、total_2016 列

Y 轴上,我需要为每一行的单个值画一个点,并在 2013-2016 年的这些值之间画一条线

我无法通过xticks 将列中的值作为 X 轴上的不同时间点。不确定这是否是正确的做法。

到目前为止,我已经绘制了这个:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
matplotlib.style.use('ggplot')
ax = data_merged_years.plot(kind='line', title ="UK Schools budget per year 2013-2016", figsize=(15, 10), legend=True, fontsize=12)
ax.set_xlabel("Year", fontsize=12)
ax.set_ylabel("Pounds", fontsize=12)
plt.show()

我的尝试:X 和 Y 不正确的绘图:

【问题讨论】:

  • 我不确定我是否理解您的数据集。行的内容是什么?据我所知,一维太多了。

标签: python-2.7 pandas matplotlib plot


【解决方案1】:

使用您的脚本,我可以产生的输出是这样的:

如果您希望将列作为 x 轴而不是不同的线,您可以在绘图前transpose DataFrame:

In [13]: import matplotlib.pyplot as plt
    ...: import pandas as pd
    ...: matplotlib.style.use('ggplot')
    ...: ax = data_merged_years.transpose().plot(kind='line', title ="UK Schools budget per year 2013-2016", figsize=(15, 10), legend=True, fontsize=12)
    ...: ax.set_xlabel("Year", fontsize=12)
    ...: ax.set_ylabel("Pounds", fontsize=12)
    ...: plt.show()
    ...: 

从您的原始情节看来,一个情节中将有超过一万行。你确定这是你想要的吗?

【讨论】:

  • transpose 运行良好。我简化为data_merged_years.transpose().plot() plt.show()并很好地展示了标签。无法绘制我拥有的 20,000 行(错误:太大),但最初的目的是将其应用于较小的数据子集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 2019-12-15
  • 2019-01-17
  • 2013-02-22
相关资源
最近更新 更多