plot date ranges in matlab in python

Here are the steps to plot date ranges using matplotlib and pandas:

  1. Import required libraries:
main.py
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
82 chars
4 lines
  1. Create a dataframe with date range:
main.py
# Assuming date range from 1st to 7th of January 2021
date_rng = pd.date_range(start='1/1/2021', end='1/7/2021', freq='D')
df = pd.DataFrame(date_rng, columns=['date'])
169 chars
4 lines
  1. Convert date column into datetime object:
main.py
df['date'] = pd.to_datetime(df['date'])
40 chars
2 lines
  1. Plot date range on x-axis and any other variable on y-axis:
main.py
plt.plot_date(x=df['date'], y=[1, 2, 3, 4, 5, 6, 7])
53 chars
2 lines
  1. Customize the plot as per your requirement:
main.py
plt.title('Date Range Plot')
plt.xlabel('Date')
plt.ylabel('Values')
plt.show()
80 chars
5 lines

This will create a simple line graph with date range on x-axis and input values on y-axis. You can modify the code as per your requirement for different types of plots like scatterplot, bar graph, etc.

related categories

gistlibby LogSnag