簡體   English   中英

使用滑塊matplotlib python更改散點圖的軸

[英]changes axis of scatter plot with slider matplotlib python

我有一個非常大的數據集,希望用戶能夠沿軸滑動以查看數據的各個部分。 我正在嘗試利用滑塊示例,但不確定如何更新圖形。 希望有人可以以此解釋一些幕后故事。

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()

設置新的限制時,圖形會自動更新。 您只是看不到它,因為您更新了錯誤的子圖。 只需選擇正確的子圖即可更新:

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示例中,您可以找到fig.canvas.draw_idle()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM