Pyplot,使用滑块绘制许多不同的线条

时间:2015-02-05 22:52:20

标签: python matplotlib

我正在尝试使用pyplot创建一个包含许多(真的很多......)线条的交互式图片。

所有这些行都存储在列表列表中,其中每个列表的第一个元素是模型的参数,接下来的5个元素是输出,我想将其绘制为一条线。

我可以绘制给定参数的行(第一个元素)但是如何让滑块运行以便我看到当我更改参数时会发生什么?现在更改滑块只会消除线条,只剩下轴。

我猜这里有一些关于我每次如何绘制多行的信息,这使得程序很难为新的情节提取信息。我很感激帮助。

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
import numpy as np

#the list of lists.
equilibria = np.loadtxt('foo', delimiter = ',')
equilibria = equilibria.tolist()

#a function that extracts the lines based on the first element
def lines_param(z1):
    lines_z1=[]
    for x in equilibria:
        if x[0]==z1:
            lines_z1.append(x[1:6])
    return lines_z1

#the x-axis values on plot, same for every line.
x_m=[-1.0,-0.5,0.0,0.5,1.0]

#setting up the slider
axcolor = 'lightgoldenrodyellow'
axz1 = plt.axes([0.125,0.1,0.775,0.03], axisbg = axcolor)
sz1 = Slider(axz1, '$Z_1$',-2.1,2.1, valinit=-1.5)

#the starting lines plotted
z1= -1.5
lines=lines_param(z1)

plt.subplot(211)
plt.axis([-1.0,1.0,0,10])
for y in lines:
    plt.plot(x_m, y, linewidth=.2)

#what happens when the slider updates
def updated(val):
    z1 = sz1.val
    lines=lines_param(z1)
    #clear out the old lines and plot the new ones
    plt.cla()
    for y in lines:
        plt.plot(x_m, y, linewidth=.2)

sz1.on_changed(updated)
plt.show()

1 个答案:

答案 0 :(得分:0)

尝试在“已更新”功能中调用plt.draw(),如下所示:

def updated(val):
    z1 = sz1.val
    lines=lines_param(z1)
    #clear out the old lines and plot the new ones
    plt.cla()
    for y in lines:
        plt.plot(x_m, y, linewidth=.2)
    plt.draw()

我无法在我的机器上运行您的代码,因为我不确定您的'foo'目录的数据结构如何。如果您可以发布数据格式,则可以更轻松地调试您的问题。