Skip to content

Instantly share code, notes, and snippets.

@ymkim92
Created December 13, 2019 05:39
Show Gist options
  • Save ymkim92/aae763bc83e23926247897eb79ed9cfe to your computer and use it in GitHub Desktop.
Save ymkim92/aae763bc83e23926247897eb79ed9cfe to your computer and use it in GitHub Desktop.
matplotlib ploting with button to print stats and tooltip on hover
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import mplcursors
df = pd.DataFrame()
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
df['time'] = np.arange(0.0, 1.0, 0.001)
df['data'] = np.sin(2*np.pi*df['time'])
df.plot.scatter(x='time', y='data', s=1, ax=ax)
crs = mplcursors.cursor(ax,hover=True)
# crs.connect("add", lambda sel: sel.annotation.set_text(
# 'Point {},{}'.format(sel.target[0], sel.target[1])))
def print_stats(event):
start, end = ax.get_xlim()
cond1 = df['time'] > start
cond2 = df['time'] < end
df_tmp = df[cond1 & cond2]
print(df_tmp.describe())
b_loc_size = plt.axes([0.81, 0.05, 0.1, 0.075])
b = Button(b_loc_size, 'Stats', hovercolor='green')
b.on_clicked(print_stats)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment