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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With