Reputation: 137
I'm playing with the slider demo (https://matplotlib.org/stable/gallery/widgets/slider_demo.html) and decided to add text to the plot with plt.text, as follows;
freqtext = plt.text(-5, 22, r'$\nu = %.2f$' % freq_slider.val)
I then changed the text value based on freq_slider.val as follows
freqtext.set_text(r'$\nu = %.2f$' % freq_slider.val)
If the value of freq_slider.val is greater than some value, say 10, I would like for the text to change color to red. I know I can set its value with
freqtext = plt.text(-5, 22, r'$\nu = %.2f$' % freq_slider.val, color = 'r')
However, what I am trying to do is dynamically change the color (or other font parameters) based on its value. The entire code is below. Note that the only changes are the two described above.
Thanks in advance.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
# The parametrized function to be plotted
def f(t, amplitude, frequency):
return amplitude * np.sin(2 * np.pi * frequency * t)
t = np.linspace(0, 1, 1000)
# Define initial parameters
init_amplitude = 5
init_frequency = 3
# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2)
ax.set_xlabel('Time [s]')
# adjust the main plot to make room for the sliders
plt.subplots_adjust(left=0.25, bottom=0.25)
# Make a horizontal slider to control the frequency.
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
freq_slider = Slider(
ax=axfreq,
label='Frequency [Hz]',
valmin=0.1,
valmax=30,
valinit=init_frequency,
)
# Make a vertically oriented slider to control the amplitude
axamp = plt.axes([0.1, 0.25, 0.0225, 0.63])
amp_slider = Slider(
ax=axamp,
label="Amplitude",
valmin=0,
valmax=10,
valinit=init_amplitude,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
line.set_ydata(f(t, amp_slider.val, freq_slider.val))
fig.canvas.draw_idle()
# make text red if freq_slider.val > 10
freqtext.set_text(r'$\nu = %.2f$' % freq_slider.val)
# register the update function with each slider
freq_slider.on_changed(update)
amp_slider.on_changed(update)
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
freqtext = plt.text(-5, 22, r'$\nu = %.2f$' % freq_slider.val, color='r')
def reset(event):
freq_slider.reset()
amp_slider.reset()
button.on_clicked(reset)
plt.show()
Upvotes: 2
Views: 1837
Reputation: 35135
I've rarely used matplotlib's event system, but I set the initial text setting as empty, and update the text and text color in the update function. At that time, I add a numeric judgment of 10 or more.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
%matplotlib widget
# The parametrized function to be plotted
def f(t, amplitude, frequency):
return amplitude * np.sin(2 * np.pi * frequency * t)
t = np.linspace(0, 1, 1000)
# Define initial parameters
init_amplitude = 5
init_frequency = 3
# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2)
ax.set_xlabel('Time [s]')
# freqtext = ax.text(-5, 22, []) # update
# adjust the main plot to make room for the sliders
plt.subplots_adjust(left=0.25, bottom=0.25)
# Make a horizontal slider to control the frequency.
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
freq_slider = Slider(
ax=axfreq,
label='Frequency [Hz]',
valmin=0.1,
valmax=30,
valinit=init_frequency,
)
# Make a vertically oriented slider to control the amplitude
axamp = plt.axes([0.1, 0.25, 0.0225, 0.63])
amp_slider = Slider(
ax=axamp,
label="Amplitude",
valmin=0,
valmax=10,
valinit=init_amplitude,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
line.set_ydata(f(t, amp_slider.val, freq_slider.val))
fig.canvas.draw_idle()
# make text red if freq_slider.val > 10
c = 'r' if freq_slider.val > 10 else 'k'
freqtext.set_text(r'$\nu = %.2f$' % freq_slider.val)
freqtext.set_color(c)
# register the update function with each slider
freq_slider.on_changed(update)
amp_slider.on_changed(update)
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
#freqtext = plt.text(-5, 22, r'$\nu = %.2f$' % freq_slider.val)
freqtext = plt.text(-5, 22, '')
def reset(event):
freq_slider.reset()
amp_slider.reset()
button.on_clicked(reset)
plt.show()
Upvotes: 1