使用 matplotlib 添加垂直滑块 [英] Add a vertical slider with matplotlib

查看:43
本文介绍了使用 matplotlib 添加垂直滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 matplotlib 创建一个垂直滑块小部件而不是水平滑块.

I would like to create a vertical slider widget instead horizontal slider with matplotlib.

我在matplotlib网页 http://matplotlib.org/examples/中找到了一个很好的例子widgets/slider_demo.html 但我不知道如何在 Y 轴上移动滑块并更改滑块标签.我可以改变轴的位置,但不能改变滑块的移动.示例在这里:

I have found a good example in the matplotlib webpage http://matplotlib.org/examples/widgets/slider_demo.html but I do not know how to move the slider in the Y axis and change the slider labels. I can change the position of the axis but not the movement of the slider. The example is here:

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 = 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)

def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*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.5, 0.025, 0.15, 0.15], axisbg=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()

推荐答案

我知道这篇文章已经 5 年了,但是在尝试实现我自己的垂直滑块后,我发现从 matplotlib 3.1 这个功能存在原生.

I know this post is 5 years old, but after trying to implement my own vertical slider I then found that as of matplotlib 3.1 this feature exists natively.

我有 matplotlib 3.1.2 参数之一是

I have matplotlib 3.1.2 and one of the parameters is

orientation:str,水平"或垂直",默认值:水平"滑块的方向.

orientation : str, 'horizontal' or 'vertical', default: 'horizontal' The orientation of the slider.

在此处查看 https://matplotlib.org/3.1.1/api/widgets_api.html

这篇关于使用 matplotlib 添加垂直滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆