2017-05-16 158 views
0

我想繪製圖並使用滑塊更改它們的屬性。但是matplotlib.widgets.Slider的標籤文字對我來說非常小。我瀏覽互聯網找到答案如何改變它,但我沒有運氣。更改matplotlib.slider標籤文本大小

代碼的關鍵部分是:

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 

我試圖讓文本標籤爲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) 

但它返回我一個錯誤:

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求助!

回答

0

首先創建滑塊,然後使用E0_slider.label.set_size()更新標籤文本大小。

您不需要先創建一個matplotlib.text.Text實例,只需使用該字符串作爲標籤即可。

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) 

從更改標籤尺寸:

enter image description here

enter image description here

0

不能文本提供給label說法。但之後您可以更改標籤尺寸。該標籤可作爲Slider.label使用,作爲matplotlib.text.Text實例,可以使用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