微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与Matplotlib进行交互的可视化,试图将单选按钮/滑块/按钮集成在一起

如何解决与Matplotlib进行交互的可视化,试图将单选按钮/滑块/按钮集成在一起

我正在尝试使用以下参数构建交互式视觉效果

  1. 当值增加到最大值并根据所选类型(“正常”,“伽玛”,“指数”,“均匀”)和所选数据量(范围从100- 1000)
  2. 滑块的作用是设置所选值的最大数量
  3. “单选”按钮用于选择数据/直方图的类型
  4. 该按钮将启动该过程。

我的问题:

  1. 选择直方图类型(通过单选按钮触发)后,该过程就会开始
  2. 我正在尝试使直方图的“步长”移动,并且将滑块设为“ 50”,但我发现它是相当随机
  3. 不管滑块的选定值如何,迭代都会继续达到最大值。
  4. 第一次迭代结束后,程序将停止并且不会重置。我知道我没有添加重置功能,仅仅是因为我不知道怎么做。
  5. 按钮功能目前似乎没有用。
  6. 我无法让小部件一起工作

这是我第一次尝试进行交互式可视化,因此,感谢您的建设性反馈和支持

这是我的下面的代码

#starting with preparing the figure layout
import matplotlib.gridspec as gridspec

fig= plt.figure(figsize= (9,9))

gspec= gridspec.GridSpec(5,6,wspace= 0,hspace= 0.2
                    )

main_visual= plt.subplot(gspec[1:,:])
radio_ax= plt.subplot(gspec[0,0])


button_ax= plt.subplot(gspec[0,-1])
plt.subplots_adjust(left= 0.1,bottom= 0.2)
slider_ax= plt.axes([0.28,0.13,0.62,0.03])

 
for ax in [radio_ax,slider_ax,button_ax]:
    ax.tick_params(axis="both",which= "both",length=0,labelbottom= False,labelleft= False)

for spine in ["right","left","top","bottom"]:
    button_ax.spines[spine].set_visible(False)

main_visual.axis([-4,4,2])

from matplotlib.widgets import RadioButtons
from matplotlib.widgets import Slider
from matplotlib.widgets import Button
import matplotlib.animation as animation


x1 = np.random.normal(-2.5,1,10000)
x2 = np.random.gamma(2,10000)
x3 = np.random.exponential(2,10000)
x4 = np.random.uniform(-2.5,10000)


#Setting up the Radio buttons
radio= RadioButtons(radio_ax,("normal","Gamma","Exponential","Uniform"),active="blue",activecolor="#B2c891")
name = radio.value_selected


#setting up the Button

button= Button(button_ax,label= "Start Iteration",color= "blue",hovercolor= "#B2c891")

#Setting up the Slider Bar
valmax= 1000.0
slider= Slider(slider_ax,label= "Number of values",valmin= 100.0,valmax= valmax,valinit= 500.0,valfmt= "%1.0f",alpha= 0.6,)
slider.label.set_size(10)

curr= slider.val



#Setting the Animation


def update(curr):
    
   
    
    if curr == valmax:
        a.event_source.stop()
    
    
    main_visual.cla()
    
    #selecting the type of graph
    type_dict= {"normal": x1,"Gamma": x2,"Exponential": x3,"Uniform": x4 }
    data= type_dict[radio.value_selected]
    
    
    #drawing the histogram
    bins= np.arange(-4,0.2)
    main_visual.hist(data[:curr],bins= bins,alpha= 0.4,normed= True)

    main_visual.set_title("Sampling the {} distribution \n sample size= {}".format(radio.value_selected,curr),fontsize= 14,fontweight="bold")

    main_visual.set_ylabel("Frequency",fontsize=12,fontweight="semibold",style="oblique")   

button.on_clicked(update)
a= animation.FuncAnimation(fig,update,interval= 5)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。