create different sliders by clicking different buttons (matplotlib widgets) - matplotlib

Okay, here we go: This code first defines 3 different sliders (f010,f015,f022) that are set invisible and 3 different buttons (WR10,WR15,WR22). Tje sliders are all at the same xy position. Then each button should call a specific slider, e.g. Button WR10 --> slider f010. The problem is, that if one button is clicked, all 3 sliders are called and do overwrite each other. Can someone help me?
from __future__ import print_function
from numpy import pi, sin
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.patches import Ellipse
from scipy.optimize import fsolve
#Plot
axis_color = 'lightgoldenrodyellow'
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.35)
#Define Sliders and set all invisible
f010_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f010_slider = Slider(f010_slider_ax, 'f0_10 [GHz]', 750.0, 1100.0, valinit=750.0)
f010_slider_ax.set_visible(False)
f015_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f015_slider = Slider(f015_slider_ax, 'f0_15 [GHz]', 500.0, 750.0, valinit=500.0)
f015_slider_ax.set_visible(False)
f022_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f022_slider = Slider(f022_slider_ax, 'f0_22 [GHz]', 340.0, 500.0, valinit=340.0)
f022_slider_ax.set_visible(False)
#Define WR buttons
WR10_button_ax = fig.add_axes([0.025, 0.9, 0.05, 0.05])
WR10_button = Button(WR10_button_ax, 'WR1.0:', color=axis_color, hovercolor='0.975')
WR15_button_ax = fig.add_axes([0.025, 0.8, 0.05, 0.05])
WR15_button = Button(WR15_button_ax, 'WR1.5:', color=axis_color, hovercolor='0.975')
WR22_button_ax = fig.add_axes([0.025, 0.7, 0.05, 0.05])
WR22_button = Button(WR22_button_ax, 'WR2.2:', color=axis_color, hovercolor='0.975')
#Define Event on Buttons
def WR10_button_on_clicked(mouse_event):
f010_slider_ax.set_visible(True)
WR10_button.on_clicked(WR10_button_on_clicked)
def WR15_button_on_clicked(mouse_event):
f015_slider_ax.set_visible(True)
WR15_button.on_clicked(WR15_button_on_clicked)
def WR22_button_on_clicked(mouse_event):
f022_slider_ax.set_visible(True)
WR22_button.on_clicked(WR22_button_on_clicked)
plt.show()

Related

Condition callback function in matplotlib

I created a bar chart using the following code :
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
import numpy as np
import scipy.stats
from matplotlib.widgets import Slider, Button, RadioButtons
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(32000,200000,3650),
np.random.normal(43000,100000,3650),
np.random.normal(43500,140000,3650),
np.random.normal(48000,70000,3650)],
index=[1992,1993,1994,1995])
x = df.T.agg(lambda z : z.mean())
def mean_IC(data, confidence=0.95):
n = len(data)
m, se = data.mean(), scipy.stats.sem(data)
h = se * scipy.stats.t.ppf((1 + confidence) / 2., n-1)
return m, m-h, m+h
plt.subplots_adjust(right=0.8)
yerr1 = np.array([[mean_IC(df.iloc[i])[0] - mean_IC(df.iloc[i])[1] for i in range(len(x.index))],
[mean_IC(df.iloc[i])[2] - mean_IC(df.iloc[i])[0] for i in range(len(x.index))]])
ax = plt.bar(range(len(x.index)), x, align='center', yerr=yerr1, width=1, edgecolor='black')
_=plt.xticks(range(len(x.index)), x.index)
#plt.bar(x.index.astype(str), x, align='center')
ax = plt.gca()
def step1(event):
plt.sca(ax)
plt.cla()
plt.bar(range(len(x.index)), x, align='center', yerr=yerr1, width=1, edgecolor='black')
plt.xticks(range(len(x.index)), x.index)
plt.axhline(y=event.ydata, color='black', linestyle='--', linewidth=1)
# tell mpl_connect we want to pass a 'button_press_event' into step1 when the event is detected
plt.gcf().canvas.mpl_connect('button_press_event', step1)
def step2(event) :
axcolor = 'lightgoldenrodyellow'
plt.axes([0.83, 0.7, 0.15, 0.25], facecolor=axcolor)
ax1 = plt.gca()
ax1.cla()
plt.axes([0.83, 0.7, 0.15, 0.25], facecolor=axcolor)
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
plt.text(0.5, 0.5,'Currently\nchoosing\n y=\n{:.4f}'.format(event.ydata), family='serif', ha='center', wrap=True)
button_ax = plt.axes([0.855, 0.73, 0.1, 0.055])
button = Button(button_ax, 'Continue', color='grey')
plt.gcf().canvas.mpl_connect('button_press_event', step2)
What I'm trying to do is to draw a horizontal line in the bar chart depending on where the user clicks, and then have the y value printed in the new axes object that I added. The problem is I want this to happen only when the user clicks in the actual figure of the bar chart. Instead, what happens is that when you click outside the figure or in the axes object on the right, it also takes into account this as an event. So how can I make the callback function only work under a condition ? I tried many things but nothing seems to work ..

Matplotlib - Setting a tick label's background colour

I have a subplot and its tick labels overlap with the data. I would like to set the x-tick labels to have a background colour (e.g. white). Currently I have only been able to find how to change the label's colour, but not the background. I know how to get the effect using a text object as shown below. (NB - I don't want the whole subplot's margin to be coloured, but just the tick label).
MWE
import matplotlib as mpl
rc_fonts = {
"text.usetex": True,
'text.latex.preview': True,
"font.size": 50,
'mathtext.default': 'regular',
'axes.titlesize': 55,
"axes.labelsize": 55,
"legend.fontsize": 50,
"xtick.labelsize": 50,
"ytick.labelsize": 50,
'figure.titlesize': 55,
'figure.figsize': (10, 6.5), # 15, 9.3
'text.latex.preamble': [
r"""\usepackage{lmodern,amsmath,amssymb,bm,physics,mathtools,nicefrac,letltxmacro,fixcmex}
"""],
"font.family": "serif",
"font.serif": "computer modern roman",
}
mpl.rcParams.update(rc_fonts)
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, InsetPosition, mark_inset
from numpy import linspace, sin
x = linspace(0, 1, 100)
plt.clf()
ax1 = plt.gca()
ax2 = plt.axes([0, 0, 1, 1], label=str(2))
ip = InsetPosition(ax1, [0.08, 0.63, 0.45, 0.3])
ax2.set_axes_locator(ip)
ax1.plot(x, x)
ax1.plot(x, x + 0.3)
ax1.set_xlim(0, 1)
ax1.set_ylim(0, 1)
ax2.xaxis.set_tick_params(labelcolor='r')
ax1.text(0.3, 0.3, '$1$', transform=ax1.transAxes, horizontalalignment='center', verticalalignment='center', color='black', backgroundcolor='white')
To set a label's background color you may use the same property as for a text, essentially because a label is a text.
plt.setp(ax2.get_xticklabels(), backgroundcolor="limegreen")
For more sophisticated backgrounds, you could also use the bbox property.
bbox = dict(boxstyle="round", ec="limegreen", fc="limegreen", alpha=0.5)
plt.setp(ax2.get_xticklabels(), bbox=bbox)
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(np.linspace(0, 1, 5), np.random.rand(5))
# set xticklabels
xtl = []
for x in ax.get_xticks():
xtl += ['lbl: {:.1f}'.format(x)]
ax.set_xticklabels(xtl)
# modify labels
for tl in ax.get_xticklabels():
txt = tl.get_text()
if txt == 'lbl: 1.0':
txt += ' (!)'
tl.set_backgroundcolor('C3')
tl.set_text(txt)

How to set the updated value of Slider always in the centre?

Following the Slider Demo of Matplotlib https://matplotlib.org/gallery/widgets/slider_demo.html, I would like to update the Slider ranges, so that every time I change the slider values, those are re-centred in the Slider.
I have tried to define the Sliders as
sfreq = Slider(axfreq, 'Freq', freq-10, freq+10, valinit=freq)
samp = Slider(axamp, 'Amp', amp-5, amp+5, valinit=amp)
but since the update() function does not return anything, that does not work. I also tried making these variables global inside the function, which also did not work. I finally tried defining the Sliders inside the update function,
def update(val):
amp = samp.val
freq = sfreq.val
l.set_ydata(amp*np.sin(2*np.pi*freq*t))
fig.canvas.draw_idle()
Slider(axfreq, 'Freq', freq-10, freq+10, valinit=freq)
Slider(axamp, 'Amp', amp-5, amp+5, valinit=amp)
but that overlays more and more Sliders as I change the values. Any suggestions?
So I just decided to make the range of the slider cover several orders of magnitude of the parameter, and display the values in a logarithmic scale. In case anyone wonders, and following the matplotlib demo:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 10
delta_f = 5.0
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
sfreq = Slider(axfreq, 'Freq', np.log(1), np.log10(1000), valinit=np.log10(f0), valfmt='%4.2E')
samp = Slider(axamp, 'Amp', a0-5, a0+5, valinit=a0)
def update(val):
amp = samp.val
freq = sfreq.val
sfreq.valtext.set_text('{:4.2E}'.format(10**freq))
l.set_ydata(amp*np.sin(2*np.pi*10**freq*t))
fig.canvas.draw_idle()
sfreq.on_changed(update)
samp.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04] )
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sfreq.reset()
samp.reset()
button.on_clicked(reset)
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(colorfunc)
plt.show()

How to make dependent sliders in matplotlib

Similar to Matplotlib dependent sliders, I want to make two sliders whose sum make 10. To do that, i want that when i move one slider, the other one moves to compensate. At the moment, the code is the following :
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 5
delta_f = 5.0
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.1, 10.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
def update_sfreq(val):
samp.val = 10-sfreq.val
l.set_ydata(samp.val*np.sin(2*np.pi*sfreq.val*t))
fig.canvas.draw_idle()
sfreq.on_changed(update_sfreq)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sfreq.reset()
samp.reset()
button.on_clicked(reset)
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(colorfunc)
plt.show()
This is one of matplotlib examples that I modified to suit my needs. At the moment, I only implemented s_freq.on_changed(). I want that when I move the freq slider, the graph changes (This part is working), and at the same time, the amp slider moves too (This part is not).
Any thoughts on how to modify my function update_sfreq to correctly update samp?
Note : I do realize that if both my sliders update each other, I might end up in an infinite loop. I have already thought of this and of a solution. The part that is not working is really the part where moving one slider makes the other slider move.
Well, after some digging in matplotlib source code, I managed to find an answer to my question. You need to change
samp.val = ...
to
samp.set_val(...)
This will update the bar correctly.

matlibplot graphing inside a button

I'm wondering why my matlibplot is currently graphing inside a button as shown below:
Code is shown below
from gui export Index
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
fig, ax = plt.subplots()
plt.subplots_adjust(buttom-0.2)
pos_trim = plt.axes([0.20, 0.05, .1, 0.075])
b_trim = Button(pos_trim, 'Trim', hoverclor='0.25')
callback = Index(c)
b_trim.on_clicked(callback.autoTrim) # from gui.index
freqs = np.arange(2,20,3)
t = np.arange(0.0, 1.0, 0.05)
s = np.sin(2*np.pi*freqs[0]*t)
l, = plt.plot(t,s,lw=2)
plt.show()
Index class:
class Index:
def __init__(self, chart):
self.__chart = chart
def autoTrim(self, event):
print "Autotrim"
#self.__chart.autoTrim()
plt.draw()
Try changing your line where you plot the data to:
l, = ax.plot(t,s,lw=2)
That is, plot to the axes you created for the plot (ax). Otherwise, you're plotting to the last axes you created, which in this case, is the button.