2

I am trying to add tool tip to the graph, so whenever we hover around the graph it will give the info. How do i add one and make it an interactive one?

import matplotlib.pyplot as plt
import pandas as pd
import pandas as pd
from numpy import nan
from matplotlib import dates as mpl_dates


df = dataset

df["Date"] = pd.to_datetime(df["Date"]).dt.strftime('%m/%d/%Y')
#df["Date"] = pd.to_datetime(df["Date"]).apply(lambda x: x.strftime('%B-%Y'))

df.loc[df['Actuals'] == 0, ['Actuals']] = nan
df.loc[df['Actuals'] > 0, ['Predicted_Lower']] = nan
df.loc[df['Actuals'] > 0, ['Predicted_Upper']] = nan
# gca stands for 'get current axis'
ax = plt.gca()
y1 = df['Predicted_Lower']
y2 = df['Predicted_Upper']
x = df['Date']


ax.fill_between(x,y1, y2, facecolor="blue", alpha=0.7)
df.plot(kind='line',x='Date',y='Predicted', color='black', ax=ax)
df.plot(kind='line',x='Date',y='Actuals', color='green', ax=ax)
df.plot(kind='line',x='Date',y='Predicted_Lower',color='white',ax=ax)
df.plot(kind='line',x='Date',y='Predicted_Upper',color='white', ax=ax)

date_format = mpl_dates.DateFormatter('%Y-%m-%d')
plt.gca().xaxis.set_major_formatter(date_format)

locs, labels = plt.xticks()
plt.xticks(locs[::3], labels[::3], rotation=45)
plt.show()
plt.xticks(rotation=45)
plt.legend(['Predicted','Actuals'])
plt.xlabel('Date')
df.head(30)
plt.show()

using pandas, matplotlib, I am getting the data from sql server that is connected to Power BI and writing pyscripts to display graphs.

enter image description here

Ashita Ramteke
  • 119
  • 1
  • 7
  • 20

1 Answers1

1

It's possible using matplotlib as discussed here.

However, you might want to look into other plotting packages such as plotly where it is builtin, default behavior.

import plotly.express as px

df = pd.DataFrame(np.arange(20), columns=['x'])
df['y'] = df['x']**2
px.line(df, x='x', y='y')

enter image description here

In your example, you could try something like

px.line(df, x='Date', y=Predicted, ...)
busybear
  • 10,194
  • 1
  • 25
  • 42