首页 > 解决方案 > 使用日期作为 xticks 绘制带有子图的条形图

问题描述

当我试图用 plt 绘制条形图时,我想知道我做错了什么。子图系列 us_gdp。我的最终目标是用 ism_pmi 表示的折线图覆盖它。

import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib inline 
import quandl

ism_pmi = quandl.get("ISM/MAN_PMI", authtoken="-kzUSP1X-aPZE-NCS7xw")
us_gdp = quandl.get("FRED/A191RL1Q225SBEA", authtoken="-kzUSP1X-aPZE-NCS7xw")

fig, ax = plt.subplots(figsize=(12,8))
ax.bar(us_gdp.index,height=us_gdp['Value'])

在此处输入图像描述

标签: pythonmatplotlib

解决方案


您可以使用pandas内置 matplotlib 绑定DataFrame.plot.bar​​:

us_gdp.plot.bar(y="Value")

通过不指定x参数,它将自动使用索引。


推荐阅读