简体   繁体   English

如何使用 slider 同时更新更多线图?

[英]How to use the slider to update more line plots at the same time?

this is my first question code related, so please bear with me I'm a complete beginner,I've developed a small desktop app using three libraries (tkiner, pandas and matplotlib).这是我的第一个问题代码相关,所以请耐心等待我是一个完整的初学者,我使用三个库(tkiner、pandas 和 matplotlib)开发了一个小型桌面应用程序。 this app plots several graphs from a csv.One of these graphs is dynamically updated using a slider that changes a factor by which the x value is multiplied.这个应用程序从 csv 中绘制了几个图表。其中一个图表使用 slider 动态更新,该 slider 改变了 x 值乘以的因子。

My question is: how can I change also the legend at the same time as well as the limit of my x-axis?我的问题是:如何同时更改图例以及 x 轴的限制?

I was trying to access the labels of ax2 and assign them the updated value, but I couldn't find a way to do that.我试图访问 ax2 的标签并为其分配更新的值,但我找不到这样做的方法。 Is there something like l.set_label(label=name +" Nspt x f1={}".format(f1)) ?有类似l.set_label(label=name +" Nspt x f1={}".format(f1))东西吗?

I attach a small code section that shows the problem.我附上了一个显示问题的小代码部分。 Also an image of the plot.还有一张 plot 的图片。 thanks in advace to who will spend his time helping me.提前感谢谁会花时间帮助我。

initial_f1=5

axcolor ='darkgray'
axf1 = plt.axes([0.6,0.01, 0.3, 0.01], facecolor=axcolor)
sf1=Slider(axf1,"f1",4.0,6.5,valinit=initial_f1,valstep=0.5)

for name, group in bh_groups:
    ax1.plot(group["Nspt"],group["ztest"],marker="X",linestyle="",ms=8, label=name)
    ax2.plot(group["cu"],group["ztest"],marker="o",linestyle="",ms=8,label=name +" Triaxial tests")
    l,=ax2.plot(group["Nspt"]*initial_f1,group["ztest"],marker="X",linestyle="",ms=8,label=name +" Nspt x f1={}".format(initial_f1))
 

    def update(val):

        f1 = sf1.val
        l.set_xdata(f1*group["Nspt"])                
        fig1.canvas.draw_idle()
        
    sf1.on_changed(update)



ax1.set_xlabel('Nspt',fontweight="bold")    
ax1.xaxis.set_label_position('top')
ax1.xaxis.tick_top()
ax1.set_ylabel("z [mOD]")

ax2.set_xlabel('Undrained Shear Strength, su [kPa]',fontweight="bold")    
ax2.xaxis.set_label_position('top')
ax2.xaxis.tick_top()
ax2.set_ylabel("z [mOD]")

Output: Output:

在此处输入图像描述

EDITED: I managed to updated the legend as well, now what if I want the slider to act on more lines at the same time?编辑:我也设法更新了图例,现在如果我希望 slider 同时作用于更多行怎么办? the lines are described by a fuction and depend on the same variable f1.这些行由一个函数描述并依赖于相同的变量 f1。 I haven't found a solution for this, any idea?我还没有找到解决方案,知道吗?

fig1, (ax1,ax2) = plt.subplots(nrows=1,ncols=2)
fig1.canvas.set_window_title('Data by borehole')

initial_f1=5

axcolor ='darkgray'
axf1 = plt.axes([0.6,0.01, 0.3, 0.01], facecolor=axcolor)
sf1=Slider(axf1,"f1",4.0,6.5,valinit=initial_f1,valstep=0.5)


   
for name, group in bh_groups:
    ax1.plot(group["Nspt"],group["ztest"],marker="X",linestyle="",ms=8, label=name)
    ax2.plot(group["cu"],group["ztest"],marker="o",linestyle="",ms=8,label=name +" Triaxial tests")
    
    l,=ax2.plot(group["Nspt"]*initial_f1,group["ztest"],marker="X",linestyle="",ms=8,label=name +" Nspt x f1={}".format(initial_f1))
    




def update(val):
    f1 = val
   
    l.set_xdata(f1*group["Nspt"])  
            
    l.set_label(name +" Nspt x f1={}".format(f1)) 
    ax2.legend(loc="best")
        
sf1.on_changed(update)
fig1.canvas.draw_idle()
    

ax1.set_xlabel('Nspt',fontweight="bold")    
ax1.xaxis.set_label_position('top')
ax1.xaxis.tick_top()
ax1.set_ylabel("z [mOD]")

ax2.set_xlabel('Undrained Shear Strength, su [kPa]',fontweight="bold")    
ax2.xaxis.set_label_position('top')
ax2.xaxis.tick_top()
ax2.set_ylabel("z [mOD]")

ax1.legend(loc="best") 

plt.tight_layout()

 

plt.show()

在此处输入图像描述

Here is a simple example, where one slider controls:这是一个简单的示例,其中一个 slider 控制:

  • two lines两条线
  • two labels + legend entries两个标签 + 图例条目
  • the limits of the x-axis x 轴的范围

The two functions are not very creative: y1=sin(alpha*x) and y2=cos(alpha*x) and the limits on the x axis are: [0, 3*pi*alpha]这两个函数不是很有创意: y1=sin(alpha*x)y2=cos(alpha*x) ,x 轴上的限制是: [0, 3*pi*alpha]

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

x = np.linspace(0, np.pi * 2, 1000)
y1 = np.sin(1*x)
y2 = np.cos(1*x)

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.25)
ax.set_xlim(0, 2*np.pi)
h1 = ax.plot(x, y1, label='dummy')[0]
h2 = ax.plot(x, y2, label='dummy')[0]

ax.legend(loc='upper right')
slider_alpha = Slider(plt.axes([0.2, 0.1, 0.6, 0.05]), 'alpha',
                      valmin=1, valmax=2, valinit=1, valstep=0.01)

def update(val):
    # Update lines 
    x = np.linspace(0, np.pi*3*val, 1000)
    y1 = np.sin(x*val)
    y2 = np.cos(x*val)
    h1.set_data(x, y1)
    h2.set_data(x, y2)
   
    # Update labels + legend()
    h1.set_label("y=sin(x*{:.3})".format(float(val)))
    h2.set_label("y=cos(x*{:.3})".format(float(val)))
    ax.legend(loc='upper right')
    
    # Update x limits
    ax.set_xlim(0, np.pi*3*val)

slider_alpha.on_changed(update)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM