nas
nas

Reputation: 2417

How can I hide a custom button when saving a matplotlib figure?

I am very new to pyplot. I need to add a custom button in my plot, which I did by following code lines:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

dataX = np.array([1,2,3,4,5,6,7,8,9,10])
dataY = np.array([1193,1225,1125,1644,1255,13676,2007,2008,12359,1210])

ax = plt.subplot(111)
def on_click(event):
    if event.dblclick:
       ax.plot((event.xdata, event.xdata), (mean-standardDeviation, mean+standardDeviation), 'r-')
       plt.show()

def _yes(event):
    print("yolo")

mean = np.mean(dataY)
standardDeviation = np.std(dataY)

ax.plot(dataX, dataY, linewidth=0.5)
plt.connect('button_press_event', on_click)

axcut = plt.axes([0.9, 0.0, 0.1, 0.075])
bcut = Button(axcut, 'YES', color='red', hovercolor='green')
bcut.on_clicked(_yes)

plt.show()

This line of code successfully gave me the solution I need. But my question is when I try to save the figure by clicking at the button in the toolbar, is it possible not to show that custom button?

Upvotes: 4

Views: 3645

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

A strategy might be to hide the button and only show it when it's needed, i.e. when you want to click on it. Several possibilities come into mind, like pressing a key to show/hide the button or double click somewhere in the figure. I guess one easy one might be to just show the button when the mouse is over the axes, where it lives in. (This might be a bad user experience for first time users but if it's the same person using the application, he or she should know where to expect the button and put the mouse.)

Here is a working example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

dataX = np.array([1,2,3,4,5,6,7,8,9,10])
dataY = np.array([1193,1225,1125,1644,1255,13676,2007,2008,12359,1210])

ax = plt.subplot(111)
def on_click(event):
    if event.dblclick:
       ax.plot((event.xdata, event.xdata), (mean-standardDeviation, mean+standardDeviation), 'r-')
       plt.show()

def on_enter(event):
    axcut.set_visible(True)
def on_leave(event):
    axcut.set_visible(False)
def _yes(event):
    print("yolo")

mean = np.mean(dataY)
standardDeviation = np.std(dataY)

ax.plot(dataX, dataY, linewidth=0.5)
plt.connect('button_press_event', on_click)
plt.connect("axes_enter_event", on_enter)
plt.connect("axes_leave_event", on_leave)


axcut = plt.axes([0.9, 0.0, 0.1, 0.075])
bcut = Button(axcut, 'YES', color='red', hovercolor='green')
axcut.set_visible(False)
bcut.on_clicked(_yes)

plt.show()

Upvotes: 3

J. P. Petersen
J. P. Petersen

Reputation: 5031

The Button class doesn't seem to have any nice way to hide it. But you can hide the buttons individual parts like this:

bcut.ax.patch.set_visible(False)
bcut.label.set_visible(False)
bcut.ax.axis('off')
plt.gcf().canvas.draw()

If you want to show it again, just enable all the parts again.

Upvotes: 2

Related Questions