Home > front end >  How do I add a crosshair to all plots for plot(subplots=True)?
How do I add a crosshair to all plots for plot(subplots=True)?

Time:08-15

I have a function that calls 9 graphs.

It looks like this

df = pd.read_csv('Datasets\\CSV\\Data1_SetOI.csv')
features_considered = ['P','Tik','Delta_P','M','Pmin/max','V','D','OI','Sum']
features = df[features_considered]
features.index = df['Time']    
#features.head()
features.plot(subplots=True)
plt.show()

Can I add a crosshair to this graph invocation method?

I tried something like


cursor = MultiCursor(features.canvas,  color='r',
                     lw=1,  vertOn=True)

But it didn't work out for me

CodePudding user response:

You need to retrieve the Figure from the Axes array returned by plot:

axs = features.plot(subplots=True)
cursor = MultiCursor(axs[0].get_figure().canvas, axs)

Example:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor

df = pd.DataFrame(np.arange(1, 21).reshape(5,4))
axs = df.plot(subplots=True)

cursor = MultiCursor(axs[0].get_figure().canvas, axs, color='r', lw=1)

plt.show()

enter image description here

  • Related