Colorful Ed
Colorful Ed

Reputation: 143

Plotting multiple sets of dataframes on the same graph with multiple figures

I am attempting to plot two dataframes on the same graph. I want to do this five times in order to produce five different figures so that they can be saved as separate files in order to be used later. However, I have not been able to get these two sets of data on the same graph. I know how to plot two sets of data on the same graph when I have multiple subplots in grid as well as how to plot two sets of data when not using dataframes on the same graph. However, this seeems to perplex me.

tableau20=xcolorpallet()
f0,axes00=plt.subplots()
axes01=axes00.twinx()
df00=df00.drop('elev', 1)
ax00min=float(df00.min())
ax00max=1.05*float(df00.max())
ax00step=int((ax00max-ax00min)/10)
ax10min=float(df10.min())
ax10max=1.05*float(df10.max())
ax10step=int((ax10max-ax10min)/10)
df00.plot(linewidth=2, figsize=(9,6),color=tableau20[6])
axes00.set_ylabel(v00,fontsize=14, rotation=90)
axes00.set_ylim(ax00min,ax00max)
axes00.set_yticks(np.arange(ax00min,ax00max,ax00step))
df10.plot(fig=f0,ax=axes01,secondary_y=v10,linewidth=2,color=tableau20[14])
f0.set_title(v10,fontsize=18)
axes01.set_ylabel(v00,fontsize=14, rotation=90)
axes01.set_ylim(ax00min,ax00max)
axes01.set_yticks(np.arange(ax00min,ax00max,ax00step))
plt.show()

Currently, it is producing two separate graphs with my data on it, which is nice, but not what I am looking for. Any suggestions?

Here is some sample data to get an idea of what I am trying to graph:

df00:

time                Temp
2014-08-16 12:02:40 68.0
2014-08-16 12:17:40 69.0
2014-08-16 12:32:40 68.0
2014-08-16 12:47:40 68.0
2014-08-16 13:02:40 68.0
2014-08-16 13:17:40 68.0
2014-08-16 13:32:40 68.0
2014-08-16 13:47:40 68.0
2014-08-16 14:02:40 68.0
2014-08-16 14:17:40 68.0
2014-08-16 14:32:39 66.0
2014-08-16 14:32:40 67.0
2014-08-16 14:47:39 66.0
2014-08-16 14:47:40 66.0
2014-08-16 15:02:40 66.0
2014-08-16 15:17:39 64.0
2014-08-16 15:17:40 65.0
...

df10:

date_time       Temperature
2014-08-16 12:00:00 17.3997
2014-08-16 13:00:00 16.9094
2014-08-16 14:00:00 16.4693
2014-08-16 15:00:00 15.9627
2014-08-16 16:00:00 15.5795
2014-08-16 17:00:00 15.5492
2014-08-16 18:00:00 15.2729
2014-08-16 19:00:00 15.2119
2014-08-16 20:00:00 15.3572
2014-08-16 21:00:00 15.497
2014-08-16 22:00:00 15.349
2014-08-16 23:00:00 15.3398
2014-08-17 00:00:00 15.5546
2014-08-17 01:00:00 14.9101
2014-08-17 02:00:00 15.279
2014-08-17 03:00:00 15.2961
2014-08-17 04:00:00 15.003
2014-08-17 05:00:00 15.4753
...

My apologies for not originally including it. I thought this was a graphing issue, but it seems has largely turned into a compatibility issue between dataframes.

Upvotes: 2

Views: 2310

Answers (1)

Stefan
Stefan

Reputation: 42905

To plot on the same chart, you would be using the same ax object with .plot():

Using the sample data - first df1:

DatetimeIndex: 17 entries, 2014-08-16 12:02:40 to 2014-08-16 15:17:40
Data columns (total 1 columns):
Temp    17 non-null int64
dtypes: int64(1)
memory usage: 272.0 bytes
None
                     Temp
time                     
2014-08-16 12:02:40    68
2014-08-16 12:17:40    69
2014-08-16 12:32:40    68
2014-08-16 12:47:40    68
2014-08-16 13:02:40    68
2014-08-16 13:17:40    68
2014-08-16 13:32:40    68
2014-08-16 13:47:40    68
2014-08-16 14:02:40    68
2014-08-16 14:17:40    68
2014-08-16 14:32:39    66
2014-08-16 14:32:40    67
2014-08-16 14:47:39    66
2014-08-16 14:47:40    66
2014-08-16 15:02:40    66
2014-08-16 15:17:39    64
2014-08-16 15:17:40    65

and df2:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 18 entries, 2014-08-16 12:00:00 to 2014-08-17 05:00:00
Data columns (total 1 columns):
Temperature    18 non-null float64
dtypes: float64(1)
memory usage: 288.0 bytes
None
                     Temperature
date_time                       
2014-08-16 12:00:00      17.3997
2014-08-16 13:00:00      16.9094
2014-08-16 14:00:00      16.4693
2014-08-16 15:00:00      15.9627
2014-08-16 16:00:00      15.5795
2014-08-16 17:00:00      15.5492
2014-08-16 18:00:00      15.2729
2014-08-16 19:00:00      15.2119
2014-08-16 20:00:00      15.3572
2014-08-16 21:00:00      15.4970
2014-08-16 22:00:00      15.3490
2014-08-16 23:00:00      15.3398
2014-08-17 00:00:00      15.5546
2014-08-17 01:00:00      14.9101
2014-08-17 02:00:00      15.2790
2014-08-17 03:00:00      15.2961
2014-08-17 04:00:00      15.0030
2014-08-17 05:00:00      15.4753

this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

ax = df1.plot()
df2.plot(ax=ax)
plt.show()

results in a shorter line for the DataFrame where the data end some 12 hours earlier, but otherwise as expected:

enter image description here

Upvotes: 1

Related Questions