【问题标题】:changes axis of scatter plot with slider matplotlib python使用滑块matplotlib python更改散点图的轴
【发布时间】:2015-06-03 16:55:36
【问题描述】:

我有一个非常大的数据集,并且希望用户能够沿着轴滑动来查看数据的各个部分。我正在尝试利用滑块示例,但我不确定如何更新图表。希望有人可以用这个解释一些幕后花絮。

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

fig = plt.figure()
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.25)
min0 = 0
max0 = 10

x = np.arange(10)
y = np.arange(10) 
im1 = plt.scatter(x,y, s=3, c=u'b', edgecolor='None',alpha=.75)
#most examples here return something iterable

plt.ylim([0,10])#initial limits

axcolor = 'lightgoldenrodyellow'
axmin = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axmax  = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

smin = Slider(axmin, 'Min', 0, 10, valinit=min0)
smax = Slider(axmax, 'Max', 0, 10, valinit=max0)

def update(val):
    plt.ylim([smin.val,smax.val])
    ax.canvas.draw()#unsure on this
smin.on_changed(update)
smax.on_changed(update)

plt.show()

【问题讨论】:

    标签: python matplotlib widget scatter-plot


    【解决方案1】:

    当您设置新的限制时,图表会自行更新。您只是没有看到这一点,因为您更新了错误的子图。只需选择正确的子图即可更新:

    def update(val):
        plt.subplot(111)
        plt.ylim([smin.val,smax.val])
    

    (这对我有用)

    甚至可能:

    def update(val):    
        plt.ylim([smin.val,smax.val])
    
    plt.subplot(111)
    smin.on_changed(update)
    smax.on_changed(update)
    

    如果你在其他地方不做任何事情

    UPD:也可以在matplotlib examples 中找到fig.canvas.draw_idle()

    【讨论】:

    • 选择正确的子图可以解决这个问题:plt.subplot(111) 感谢您抽出宝贵时间查看此内容。
    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 2021-10-01
    • 2013-12-31
    • 1970-01-01
    • 2022-06-22
    • 2014-09-18
    • 2019-02-15
    • 2013-12-18
    相关资源
    最近更新 更多