How to plot arbitrary markers on a Pandas data series using Matplotlib?

MatplotlibPythonData Visualization

To plot arbitrary markers on a Pandas data series, we can use pyplot.plot() with markers.

Steps

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a Pandas data series with axis labels (including timeseries).
  • Plot the series index using plot() method with linestyle="dotted".
  • Use tick_params() method to rotate overlapping labels.
  • To display the figure, use show() method.

Example

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

ts = pd.Series(np.random.randn(10),
index=pd.date_range('2021-04-10', periods=10))

plt.plot(ts.index, ts, '*', ls='dotted', color='red')

plt.tick_params(rotation=45)

plt.show()

Output

raja
Published on 15-Jun-2021 11:46:37
Advertisements