8

I have two different spaced time series that I want to plot on one same graph.

Both of them are series between 12:30:00~1:25:00 but their time sequence are different: one is 5 seconds and the other is about 10.3 seconds. The type of both series is "pandas.core.series.Series". The type of the time index is string and made from strftime. For example, Series A would be:

12:30:05    0.176786
12:30:15    0.176786
12:30:26    0.176786
...
13:22:26    0.002395
13:22:37    0.002395
13:22:47    0.001574

and Series B would be:

12:30:05    0.140277
12:30:10    0.140277
12:30:15    0.140277
...
13:24:20    0.000642
13:24:25    0.000642
13:24:30    0.000454

I have tried to plot both of the series on one same plot by:

import matplotlib.pyplot as plt
A.plot()
B.plot()
plt.gcf().autofmt_xdate()
plt.show()

and it works like this:

enter image description here

It is obvious that the blue lines in the first graph vanishes around 12:55:05 because series A has only half x points of B's and plot() only arrange the plot based on the order of x-axis, not the time interval.

It will be quite clear if I plot series A alone:

enter image description here

What I want is to make the two series shown in one same plot and arranged based on the true time interval. Ideally, the plot should be similarly as:

enter image description here

I hope I've made my point clear. If anything confusing, please let me know.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3284048
  • 103
  • 1
  • 1
  • 5
  • what are `a` and `x` in your code? how are you storing the two series? Could you make this into a [MCVE](http://http://stackoverflow.com/help/mcve)? – tmdavison Jun 16 '15 at 13:26
  • Sorry for my mistake. I have corrected it. In the original post, a stands for series A and x stands for series B. I do not know what is the good way to make the data online for you to verify. Maybe just use the data I posted (6 data points for each series) would be fine. – user3284048 Jun 16 '15 at 13:31
  • 1
    What is the type of `A` and `B`? Some Pandas dataframe? If so, you should mention it ... – Bas Swinckels Jun 16 '15 at 16:34
  • 1
    Convert your x-values, whatever they are, into timestamps, and plot against time explicitly. E.g., http://stackoverflow.com/questions/24223378/autoscaling-in-matplotlib-plotting-different-time-series-in-same-chart?rq=1 – cphlewis Jun 16 '15 at 23:08

1 Answers1

8

This is creating datetimes directly, not converting them to strings; you might want matplotlib.dates.datestr2num instead, depending on your original format. Then they get converted to matplotlib's datetime representation. This seems like a hassle but means the spacing will be correct for times.

import matplotlib.pyplot as plt
from matplotlib.dates import date2num , DateFormatter
import datetime as dt

 # different times from the same timespan
TA = map(lambda x: date2num(dt.datetime(2015, 6, 15, 12, 1, x)),
         range(1, 20, 5))
TB = map(lambda x: date2num(dt.datetime(2015, 6, 15, 12, 1, x)),
         range(1, 20, 3))
A = [1.2, 1.1, 0.8, 0.66]
B = [1.3, 1.2, 0.7, 0.5, 0.45, 0.4, 0.3]

fig, ax = plt.subplots()
ax.plot_date(TA, A, 'b--')
ax.plot_date(TB, B, 'g:')
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
plt.show()

enter image description here

cphlewis
  • 15,759
  • 4
  • 46
  • 55