- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I plot two different spaced time series on one same plot in Python Matplotlib?
To plot two different spaced time series on one same plot using Matplotlib, we can take the following steps
Steps
Set the figure size and adjust the padding between and around the subplots.
Create x1, y1 and x2 and y2 data points.
Create a figure and a set of subplots.
Plot the data that contains dates, with (x1, y1) and (x2, y2) data points.
Set the major formatter of the X-axis ticklabels.
Rotate xtick label by 45 degrees using tick_params() method.
To display the figure, use Show() method.
Example
import matplotlib.pyplot as plt from matplotlib.dates import date2num, DateFormatter import datetime as dt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x1 = [date2num(dt.datetime(2021, 1, 15, 1, 1, 1)), date2num(dt.datetime(2021, 1, 15, 9, 1, 1))] y1 = [6, 2] x2 = [ date2num(dt.datetime(2021, 1, 15, 2, 1, 1)), date2num(dt.datetime(2021, 1, 15, 4, 1, 1)), date2num(dt.datetime(2021, 1, 15, 6, 1, 1)), date2num(dt.datetime(2021, 1, 15, 8, 1, 1)), date2num(dt.datetime(2021, 1, 15, 10, 1, 1)) ] y2 = [3, 6, 5, 4, 1] fig, ax = plt.subplots() ax.plot_date(x1, y1, 'o--') ax.plot_date(x2, y2, 'o:') ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S')) ax.tick_params(rotation=45) plt.show()
Output
It will produce the following output −
- Related Articles
- How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?
- Annotate Time Series plot in Matplotlib
- How to plot two violin plot series on the same graph using Seaborn?
- How can I plot a single point in Matplotlib Python?
- How to plot a time series in Python?
- How can I place a table on a plot in Matplotlib?
- Plot multiple time-series DataFrames into a single plot using Pandas (Matplotlib)
- How to plot multiple histograms on same plot with Seaborn using Matplotlib?
- How can I plot hysteresis threshold in Matplotlib?
- How to plot events on time using Matplotlib?
- How to plot a time series array, with confidence intervals displayed in Python? (Matplotlib)
- How to name different lines in the same plot of Matplotlib?
- How do you plot a vertical line on a time series plot in Pandas?
- How can matplotlib be used to plot 3 different datasets on a single graph in Python?
- How can I plot a confusion matrix in matplotlib?

Advertisements