如何格式化滑块

时间:2014-09-11 15:52:39

标签: matplotlib

我有一个滑块:

time_ax = fig.add_axes([0.1, 0.05, 0.8, 0.03])
var_time = Slider(time_ax, 'Time', 0, 100, valinit=10, valfmt='%0.0f')
var_time.on_changed(update)

我想自定义此滑块的外观:

screenshot

我可以将axisbg参数添加到add_axes函数,这会将默认的白色背景更改为指定的颜色,但这是我现在看到的所有内容。

那么,如何更改其他滑块组件:

  • silder border(默认值:黑色)
  • 默认值指示器(默认值:红色)
  • 滑块进度(默认值:蓝色)

2 个答案:

答案 0 :(得分:0)

滑块边框只是Axes实例的刺。可以直接访问进度条以在构造函数中进行基本自定义,初始状态指示符是滑块的属性。我能够改变所有这些:

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig = plt.figure()
time_ax = fig.add_axes([0.1, 0.05, 0.8, 0.03])

# Facecolor and edgecolor control the slider itself
var_time = Slider(time_ax, 'Time', 0, 100, valinit=10, valfmt='%0.0f',
                  facecolor='c', edgecolor='r')

# The vline attribute controls the initial value line
var_time.vline.set_color('blue')

# The spines of the axis control the borders
time_ax.spines['left'].set_color('magenta')
time_ax.spines['right'].set_color('magenta')
time_ax.spines['bottom'].set_color('magenta')
time_ax.spines['top'].set_color('magenta')

答案 1 :(得分:0)

定义“ax”框的轴时可以更改的框的颜色:

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 = 3
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.03, 0.25, 0.03, 0.65], axisbg=axcolor)
axamp = plt.axes([0.08, 0.25, 0.03, 0.65], axisbg=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
# The vline attribute controls the initial value line
samp.vline.set_color('green')