4

I want to make plots and change their properties with slider. But the label text of matplotlib.widgets.Slider is very small for my eyes. I surfed the internet to find the answer how to change it, but I had no luck.

The crucial part of code is:

E0_slider_ax = fig.add_axes([0.6, 0.2, 0.25, 0.03], axisbg=axis_color)
E0_slider = mw.Slider(E0_slider_ax, r'$\epsilon_0$', 1,  100, valinit = E0)
# I want to make r'$\epsilon_0$' bigger

I tried to make text for the label as folows:

t = matplotlib.text.Text(r'$\epsilon_0$', size = 22)
E0_slider = mw.Slider(E0_slider_ax, label = t, valmin = 1, valmax = 100, valinit = E0)

but it returns me an error:

Traceback (most recent call last):

  File "<ipython-input-53-2310b0749547>", line 1, in <module>
    runfile('C:/Users/Robert/Desktop/multidif_S.py', 
wdir='C:/Users/Robert/Desktop')

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-
packages\spyderlib\widgets\externalshell\sitecustomize.py", line 845, in 
runfile
    execfile(filename, namespace)

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-
packages\spyderlib\widgets\externalshell\sitecustomize.py", line 103, in 
execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Robert/Desktop/multidif_S.py", line 64, in <module>
    E0_slider = mw.Slider(E0_slider_ax, label = t, valmin = 1, valmax = 100, 
valinit = E0)

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-packages\matplotlib\widgets.py", line 376, in __init__
    horizontalalignment='right')

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-packages\matplotlib\axes\_axes.py", line 623, in text
    x=x, y=y, text=s)

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-packages\matplotlib\text.py", line 220, in __init__
    self.set_text(text)

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-packages\matplotlib\text.py", line 1206, in set_text
    self._text = '%s' % (s,)

  File "C:\Users\Robert\Desktop\WinPython-64bit-3.5.2.2\python-
3.5.2.amd64\lib\site-packages\matplotlib\text.py", line 186, in __str__
    return "Text(%g,%g,%s)" % (self._x, self._y, repr(self._text))

TypeError: a float is required

thx for help!

2 Answers 2

5

You cannot supply a text to the label argument. But you can change the label size afterwards. The label is available as Slider.label and, being a matplotlib.text.Text instance, can be changed in size using set_size.

import matplotlib.pyplot as plt
import matplotlib.widgets

fig = plt.figure()

E0_slider_ax = fig.add_axes([0.6, 0.2, 0.25, 0.03], facecolor="skyblue")
E0_slider =  matplotlib.widgets.Slider(E0_slider_ax, r'$\epsilon_0$', 1,  100, valinit = 50)
E0_slider.label.set_size(40)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Create the slider first, then update the label text size using E0_slider.label.set_size().

You don't need to create a matplotlib.text.Text instance first, but just use the string as the label.

E0_slider_ax = fig.add_axes([0.6, 0.2, 0.25, 0.03], axisbg=axis_color)
E0_slider = mw.Slider(E0_slider_ax, r'$\epsilon_0$', 1,  100, valinit = E0)

E0_slider.label.set_size(22)

Which changes the label size from:

enter image description here

Toenter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.