2

I have 4 dataframes in 4 csv. I need to plot timeseries ( Date , mean ) in the same plot.

This is my script :

cc = Series.from_csv('D:/python/means2000_2001.csv' , header=0)

fig = plt.figure()
plt.plot(cc , color='red')
fig.suptitle('test title', fontsize=20)
plt.xlabel('Date', fontsize=15)
plt.ylabel('MEANS ', fontsize=15)
plt.xticks(rotation=90)

The 4 dataframes are like this ( x=Date and y=mean )

 Out[307]: 
 Date
07-28     0.17
08-13     0.18
08-29     0.17
09-14     0.19
09-30     0.19
10-16     0.20
11-01     0.18
11-17     0.22
12-03     0.21
12-19     0.82
01-02     0.59
01-18     0.52
02-03     0.54
02-19     0.53
03-07     0.33
03-23     0.32
04-08     0.31
04-24     0.39
05-10     0.40
05-26     0.40
06-11     0.37
06-27     0.33
07-13     0.29
Name: mean, dtype: float64

when I plot the timeseries i have this graph : enter image description here

how can i plot all dataframes in the same plot with different colors?

I need something like this :

enter image description here

0

2 Answers 2

1

You can do both:

  • plot all curves with one singel command, see: plt.plot()
  • adress each singel curve to plot, see for-loop with plt.fill_between()
  • if you have 2 DataFrames, say df1 and df2, then use plt.plot() twice: plt.plot(t,df1); plt.plot(t,df2); plt.show()

enter image description here

import numpy as np
import pandas as pd
import matplotlib.pylab as plt

#--- generate data and DataFrame --
nt = 100
t= np.linspace(0,1,nt)*3*np.pi
y1 = np.sin(t); y2 = np.cos(t); y3 = y1*y2
df = pd.DataFrame({'y1':y1,'y2':y2,'y3':y3 })

#--- graphics ---
plt.style.use('fast')  
fig, ax0 = plt.subplots(figsize=(20,4))
plt.plot(t,df, lw=4, alpha=0.6);           # plot all curves with 1 command

for j in range(len(df.columns)):           # add on: fill_between for each curve
    plt.fill_between(t,df.values[:,j],label=df.columns[j],alpha=0.2)
plt.legend(prop={'size':15});plt.grid(axis='y');plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

The answer

You can plot multiple dataframes on a single graph by capturing the Axes object that df.plot returns and then reusing it. Here's an example with two dataframes, df1 and df2:

ax = df1.plot(x='dates', y='vals', label='val 1')
df2.plot(x='dates', y='vals', label='val 2', ax=ax)
plt.show()

Output:

enter image description here

Details

Here's the code I used to generate random example values for df1 and df2:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def random_dates(start, end, n=10):
    if isinstance(start, str): start = pd.to_datetime(start)
    if isinstance(end, str): end = pd.to_datetime(end)

    start_u = start.value//10**9
    end_u = end.value//10**9
    return pd.to_datetime(np.random.randint(start_u, end_u, n), unit='s')

# generate two random dfs
df1 = pd.DataFrame({'dates': random_dates('2016-01-01', '2016-12-31'), 'vals': np.random.rand(10)})
df2 = pd.DataFrame({'dates': random_dates('2016-01-01', '2016-12-31'), 'vals': np.random.rand(10)})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.