在iPython/pandas中绘制多条线会产生多个图 [英] Plotting Multiple Lines in iPython/pandas Produces Multiple Plots

查看:248
本文介绍了在iPython/pandas中绘制多条线会产生多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解matplotlib的状态机模型,但是尝试在单个图上绘制多条线时遇到错误.据我了解,以下代码应生成包含两行的单个图:

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. 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?

推荐答案

您可以使用matplotlibs pyplot包预先创建轴对象,然后将绘图附加到该轴对象:

You can pre-create an axis object using matplotlibs pyplot package and then append the plots to this axis object:

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)

或者,您可以将两个系列合并为一个m x 2的DataFrame并绘制该数据帧:

Alternatively, you could concat the two series into a m x 2 DataFrame and plot the dataframe instead:

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

这篇关于在iPython/pandas中绘制多条线会产生多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆