Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新绘图参数_Python_Matplotlib - Fatal编程技术网

Python 更新绘图参数

Python 更新绘图参数,python,matplotlib,Python,Matplotlib,我正在尝试创建一个滑块,允许用户调整我正在绘制的某些网格线的alpha值,但似乎更新功能无法正常工作。随着alpha值的增加,线条变暗,但随着alpha值的降低,线条不会变暗。感谢您的帮助 axcolor = 'lightgoldenrodyellow' axalpha = plt.axes([0.2, 0.02, 0.65, 0.03], facecolor=axcolor) salpha = Slider(axalpha, 'Grid alpha', 0.0, 1,

我正在尝试创建一个滑块,允许用户调整我正在绘制的某些网格线的alpha值,但似乎更新功能无法正常工作。随着alpha值的增加,线条变暗,但随着alpha值的降低,线条不会变暗。感谢您的帮助

    axcolor = 'lightgoldenrodyellow'
    axalpha = plt.axes([0.2, 0.02, 0.65, 0.03], facecolor=axcolor)

    salpha = Slider(axalpha, 'Grid alpha', 0.0, 1, valinit=0.1)

    def update(val):
        aval = salpha.val
        g = ax.plot([grids[:,0], grids[:,2]], [grids[:,1], grids[:,3]], 
                     color='black', alpha=aval, label = "Grid lines")
    salpha.on_changed(update)

您不是在更新线,而是在旧线的基础上不断绘制新线。要更新行,请在更新功能外部创建行,并仅使用更新功能更新行

g = ax.plot([grids[:,0], grids[:,2]], [grids[:,1], grids[:,3]], 
                     color='black', alpha=salpha.val, label = "Grid lines")
def update(val):
    for line in g:
        line.set_alpha(val)

salpha.on_changed(update) 

谢谢你的回复。当我尝试将plot函数设置为(g,)时,我得到一个错误,即有太多的值需要解包(预期为1)。当我使用salpha.on_changed(g.set_alpha)时,我得到一个错误,即“list”对象没有属性“set_alpha”,这是因为您没有提供。我仍然需要在这里猜测,但是更新后的答案现在应该适用于任何可能的情况。@jeffreyzhao如果这个答案有效,如果不同时对它进行投票,你至少应该接受它。ImportanteCofBeingernest是
matplotlib
领域的真正专家。他花在你问题上的时间至少值得。