2

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.

Stefan
  • 41,759
  • 13
  • 76
  • 81
Colorful Ed
  • 143
  • 2
  • 12
  • Do you want to plot multiple line charts on the same `ax` object and then save, or plot five times using the same formatting and save each separately? – Stefan Jun 20 '16 at 16:38
  • I want to plot two lines on the same chart, then save that chart. Then, I want to do this five more times. Here is only one time. I have the other five sets of data and because I may not always have the variable being plotted in that dataframe, I may not always want to plot one of the graphs, and so am using if statements to check if dataframes exist before plotting. I could show you what the whole thing looks like, but it's over a hundred lines of code. – Colorful Ed Jun 20 '16 at 17:26
  • Are you using `twinx` because you want two charts? To plot on the same chart, you would rather be using the same `ax` object twice. – Stefan Jun 20 '16 at 17:33
  • but when I do that, I am getting this error: `ValueError: ordinal must be >= 1` – Colorful Ed Jun 20 '16 at 18:27

1 Answers1

1

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

Stefan
  • 41,759
  • 13
  • 76
  • 81
  • I'm still getting `ValueError: Ordinal must be >=1`. Now, these are two different dataframes that are plotting on the same graph. Does that matter? – Colorful Ed Jun 21 '16 at 12:49
  • 1
    That's possible, this error comes up when for instance the indexes are inconsistent. Could you combine the two `Series` in a single `DataFrame`, or do `.reset_index(drop=True)` on both? Another reason I've seen is a `DateTimeIndex`. – Stefan Jun 21 '16 at 12:53
  • I know that they are on different datetimes. One is from observations and has an off-hour timestep of 15 minutes, while the other is modeled behavior with a timestep of every hour on hours. So, if I combine the two dataframes, I will lose all of the data of one dataframe. How can I overlay the data on one graph, and use the same x-axis, while having different x-steps, as that seems to be the problem. – Colorful Ed Jun 21 '16 at 13:00
  • You can have different x-steps, but should in principle be able to merge the two series on the `index` using e.g. `pd.concat()`. If not, the `indexes` are inconsistent so `pandas` will have a hard time plotting them on the same chart using the same x-axis. On the other hand, you can go back to creating separate axis with `ax2=ax1.twinx` as described here: http://stackoverflow.com/questions/24183101/pandas-bar-plot-with-two-bars-and-two-y-axis – Stefan Jun 21 '16 at 13:08
  • Even when I do that, (which I tried before), I am getting `ValueError: ordinal >=1`. Unless, I do it where I have the larger timestep is graphed first. Then, I only get the larger time step dataframe to appear on the graph, even though the legend says that both dataframes are graphed. – Colorful Ed Jun 21 '16 at 13:25
  • You may want to post sample data, otherwise recommendations will be a bit of guess work. – Stefan Jun 21 '16 at 13:26
  • My apologies. It is now in the original question. – Colorful Ed Jun 21 '16 at 13:33
  • See updated. Looks as expected - note both `DataFrame` have a `DateTimeIndex`. – Stefan Jun 21 '16 at 13:47
  • Now you stated that they both have DateTimeIndex, is that where I am going wrong? – Colorful Ed Jun 21 '16 at 14:34
  • I created the `DateTimeIndex` because my understanding was that you want the time on the `x-axis`. Seems to work fine in this case. – Stefan Jun 21 '16 at 14:35
  • Right, what I more meant is that one of my dataframes has an index labeled `time`. Could that be a source of error as I still cannot reproduce a graph from all of the data I have. – Colorful Ed Jun 21 '16 at 14:52
  • The `name` of the `index` does not matter, as long as the `dtype` is `datetime`. Does `df.info()` show a `DateTimeIndex` on all `df`? – Stefan Jun 21 '16 at 15:00
  • Ah, no, one of them says that it is not a datetimeindex. Is there a way to easily convert or do I need to redefine the index in a converter? – Colorful Ed Jun 21 '16 at 15:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115215/discussion-between-colorful-ed-and-stefan). – Colorful Ed Jun 21 '16 at 15:09