在一个图中绘制两条曲线时遇到的错误(python-pandas-matplotlib)

时间:2015-07-30 03:12:44

标签: python pandas matplotlib plot

我尝试使用DataFrame中的pandasmatplotlib在一个图中绘制两条Python曲线。这是我的代码:

import numpy as np
import pandas as pd
from pandas import DataFrame
from matplotlib import pyplot as plt
plt.figure()
plt.hold()
df['open'].plot(color="green", linewidth=1.0, linestyle="-",label='open')
do = DataFrame(df[df['jiangjz']==True]['open'])
do.plot(marker='o',linestyle=".") 
axes = plt.gca()
axes.set_xticklabels(df.index)
plt.show()

df['open']do的简单数据结构是:

df:              open       do(one data):        open
     index                         index   
     2014-10-18   11               2014-12-10    12
     2014-12-10   12
     2015-12-10   12

df绘制的是曲线,do是一个点。如果我删除了hold()行和axes两行,则会产生一个数字,但x-lim从第2014-12-10开始,而不是2014-10-18。我重置x-lim的{​​{1}}以符合do,但结果变为两位数。 df行似乎不起作用。

1 个答案:

答案 0 :(得分:1)

尝试从列表中进行绘图

from itertools import izip

import pylab as plt
import pandas

d1 = pandas.DataFrame( {'open': pandas.np.random.random(100)} )
d2 = pandas.DataFrame( {'open': pandas.np.random.random(100)} )
my_dfs = [d1, d2] # or in your case [ df,do]
my_opts = [ {"color":"green", "linewidth":1.0, "linestyle":"-","label":"open"},
            {"marker":"o","linestyle":"."} ]
for d,opt in izip(my_dfs, my_opts):
    d['open'].plot( **opt)
plt.legend()

这产生了这个数字 enter image description here