How can I build two different time slots on the same plot in Python

I have two different time intervals that I want to build on the same chart.

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

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:

 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 tried to build both rows in the same section:

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

and it works as follows:

enter image description here

Obviously, the blue lines in the first graph disappear at about 12:55:05, because series A has only half x points B and the graph () only arranges the graph based on the order of the x axis, and not the time interval.

It will be very clear if I make a series of only series A:

enter image description here

I want the two series to be shown on the same chart and ordered based on the true time interval. Ideally, the plot should be similar:

enter image description here

I hope I clearly said. If anything is confusing, please let me know.

+6
source share
1 answer

This creates the data directly, rather than converting it to strings; you may need matplotlib.dates.datestr2num instead, depending on your original format. Then they are converted to matplotlib date and time representation. This seems like a hassle, but means the interval will be correct for the time.

 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

+5
source

Source: https://habr.com/ru/post/989215/


All Articles