简体   繁体   English

在iPython / pandas中绘制多行产生多个图

[英]Plotting Multiple Lines in iPython/pandas Produces Multiple Plots

I am trying to get my head around matplotlib's state machine model, but I am running into an error when trying to plot multiple lines on a single plot. 我试图了解matplotlib的状态机模型,但是在尝试在单个绘图上绘制多条线时遇到错误。 From what I understand, the following code should produce a single plot with two lines: 根据我的理解,下面的代码应该生成一个包含两行的图:

import pandas as pd
import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL', '1/1/2005')

# extract adjusted close
adj_close = aapl.loc[:, ['Adj Close']]

# 2 lines on one plot
hold(False)
adj_close.resample('M', how='min').plot()
adj_close.resample('M', how='max').plot()

In fact, I get three figures: first a blank one, and then two with one line each. 事实上,我得到三个数字:首先是空白数字,然后是两个数字,每个数字一行。

空白情节第一个情节第二个情节

Any idea what I am doing wrong or what setting on my system might be misconfigured? 知道我做错了什么或我的系统上的设置可能配置错误了吗?

You can pre-create an axis object using matplotlibs pyplot package and then append the plots to this axis object: 您可以使用matplotlibs pyplot包预先创建轴对象,然后将图附加到此轴对象:

import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt

aapl = web.get_data_yahoo('AAPL', '1/1/2005')

# extract adjusted close
adj_close = aapl.loc[:, ['Adj Close']]

# 2 lines on one plot
#hold(False)

fig, ax = plt.subplots(1, 1)
adj_close.resample('M', how='min').plot(ax=ax)
adj_close.resample('M', how='max').plot(ax=ax)

情节示例

Alternatively, you could concat the two series into amx 2 DataFrame and plot the dataframe instead: 或者,您可以将两个系列连接到amx 2 DataFrame并绘制数据框:

s1 = adj_close.resample('M', how='min')
s2 = adj_close.resample('M', how='max')
df = pd.concat([s1, s2], axis=1)
df.plot()

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

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