fu xue
fu xue

Reputation: 401

error met in plot two curves in one Figure (python-pandas-matplotlib)

I tried to plot two DataFrame curves in one figure using pandas and matplotlib in Python. Here is my code:

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()

The simple data structure of df['open'] and do is:

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

the df plotted is a curve and do is a point. If I removed the hold() line and the axes two line, it produces one figure, but the x-lim starts with day 2014-12-10 rather than 2014-10-18. I reset the x-lim of the do to meet the df, but the result becomes two figure. hold() line does not seems to work.

Upvotes: 1

Views: 249

Answers (1)

dermen
dermen

Reputation: 5372

try plotting from within a list

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()

This yields the figure enter image description here

Upvotes: 1

Related Questions