简体   繁体   English

plot 条形图,带有使用日期作为 xticks 的子图

[英]plot a bar chart with subplots using dates as xticks

I was wondering what I am doing wrong when I am trying to plot a bar chart with plt.当我尝试 plot 带有 plt. subplots the series us_gdp.子图系列 us_gdp。 My final goal is to overlay it with a line chart represented by ism_pmi.我的最终目标是用 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'])

在此处输入图像描述

You can use pandas builtin matplotlib bindings DataFrame.plot.bar :您可以使用pandas内置 matplotlib 绑定DataFrame.plot.bar :

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

By not specifying the x parameter it will automatically use the index.通过不指定x参数,它将自动使用索引。

Thanks for the reply.谢谢回复。 I am aware of the plot.bar method however, when I try overlay it with a line graph for ism_pmi I got the below error.我知道 plot.bar 方法但是,当我尝试用 ism_pmi 的折线图覆盖它时,出现以下错误。 That is why I tried to use the ax.bar method because I believe it works with plt.subplots.这就是我尝试使用 ax.bar 方法的原因,因为我相信它适用于 plt.subplots。

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # set up the 2nd axis
ax1.plot(gdp_pmi['PMI'], color = 'green', label = 'Real GDP')
ax2.plot.bar(us_gdp['Value'],label = 'PMI', color='blue')

And I got the below error:我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-e6f717dd7f16> in <module>
      3 ax1.plot(gdp_pmi['PMI'], color = 'green', label = 'Real GDP')
      4 
----> 5 ax2.plot.bar(us_gdp['Value'],label = 'PMI', color='blue')

AttributeError: 'function' object has no attribute 'bar'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM