Skip to content

Instantly share code, notes, and snippets.

@travis23
Last active June 2, 2019 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travis23/fa25cc442a05b5117655eb677384324d to your computer and use it in GitHub Desktop.
Save travis23/fa25cc442a05b5117655eb677384324d to your computer and use it in GitHub Desktop.
[matplotlib mplcursors demo] mplcursors provides a very easy way to add hover text capability to matplotlib #matplotlib #mplcursors #window_title #GridSpec #layout #tooltip #hover_text #annotation #tick_labels
import matplotlib.pyplot as plt
from matplotlib import gridspec
import mplcursors
annotations0 = ['cat', 'dog', 'hare']
x = [1, 2, 3]
y = [0, 2.5, 3]
fontsize=20
suptitle = 'Title above plot'
window_title = 'window title'
fig = plt.figure(figsize=(20,20))
fig.suptitle(suptitle, fontsize=fontsize)
fig.canvas.set_window_title(window_title)
gs = gridspec.GridSpec(1, 1)
ax0 = fig.add_subplot(gs[0])
plot0 = ax0.plot(x, y, marker='o', color='k', linestyle='None', markersize='8')
cursor_plot0 = mplcursors.cursor(plot0)
cursor_plot0.connect('add', lambda sel: sel.annotation.set_text(annotations0[sel.target.index]))
ax0.grid(True)
ax0.set_xlabel('X', fontsize=fontsize)
ax0.set_ylabel('Y', fontsize=fontsize)
plt.setp(ax0.get_xticklabels(), visible=True, fontsize=fontsize) # Modify tick label properties.
plt.setp(ax0.get_yticklabels(), visible=True, fontsize=fontsize)
ax0.set_aspect('equal', 'box') # Gives the x-axis and the y-axis the same scaling.
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment