2012-10-19 43 views
1

我想绘制一个字节的按位循环移位的值。我想要一个滑块让我改变原来的输入值。我在matplotlib站点上使用滑块示例作为参考,但由于某些原因,即使我在运行脚本时传递0-255作为滑块范围,范围始终为0-7。我猜测,滑块被锁定到我的最大数量的x值,但我不知道如何。我如何让滑块让我选择完整的0-255范围?为什么matplotlib的Slider只允许0-7的范围?

此外,尽管最小/最大我给了滑块它插入一些填充在前面低于0,并随机在我的滑块中间绘制一条垂线。我如何摆脱它? (也有什么它的目的不是明显对我?)滑块

图片仅供上升到7: enter image description here

代码:

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

from numpy import uint8 
from numpy import uint16 
from numpy import uint32 
from numpy import uint64 

def sizeof(x): 
    return [uint8, uint16, uint32, uint64].index(x) + 1 

def rot(x, i): 
    return type(x)((x >> i) | (x << (sizeof(type(x))*8 - i))) 

def plotShifts(x): 
    origType = type(x) 
    maxval = type(x)(-1) 

    numrots = sizeof(type(x)) * 8 
    vals = [rot(x, i) for i in range(numrots)] 

    print vals 

    l, = plt.plot(range(numrots), vals, 'ro') 

    axcolor = 'lightgoldenrodyellow' 
    inputax = plt.axes([0.15, 0.05, 0.65, 0.03], axisbg=axcolor) 
    inputsl = Slider(inputax, 'Input', 0, maxval, valinit=0, valfmt="%d") 

    def update(x): 
     vals = [rot(origType(x), i) for i in range(numrots)] 
     l.set_ydata(vals) 
     plt.draw() 
    inputsl.on_changed(update) 

    plt.axis([-0.5, numrots-1 + 0.5, -2, maxval + 2]) 

plotShifts(uint8(1)) 
plt.show() 

回答

3

的问题是在最后一行plt.axis([-0.5, numrots-1 + 0.5, -2, maxval + 2])其作用于保持所述滑块,而不是在轴的轴与数据。

我建议使用面向matplotlib的OO接口,而不是使用pyplot接口进行任何编程。 pyplot界面适合交互式的东西,但它有很多隐藏状态。

由于回调工作的方式,您还需要返回对slider对象的引用。

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

from numpy import uint8 
from numpy import uint16 
from numpy import uint32 
from numpy import uint64 

def sizeof(x): 
    return 2 ** [uint8, uint16, uint32, uint64].index(x) 

def rot(x, i): 
    return type(x)((x >> i) | (x << (sizeof(type(x))*8 - i))) 

def plotShifts(x): 
    fig = plt.figure() # make a new figure 
    ax = fig.add_axes([0.15, 0.2, 0.65, 0.7]) # add data axes 
    origType = type(x) 
    maxval = type(x)(-1) 

    numrots = sizeof(type(x)) * 8 
    vals = [rot(x, type(x)(i)) for i in range(numrots)] 

    print vals 
    print maxval 
    l, = ax.plot(range(numrots), vals, 'ro') # plot to data axes 

    axcolor = 'lightgoldenrodyellow' 
    inputax = fig.add_axes([0.15, 0.05, 0.65, 0.03], axisbg=axcolor) 
    inputsl = Slider(inputax, 'Input', 0, maxval, valinit=0, valfmt="%d") 

    def update(x): 
     vals = [rot(origType(x), origType(i)) for i in range(numrots)] 
     l.set_ydata(vals) 
     plt.draw() 
    inputsl.on_changed(update) 

    ax.set_ylim([-2,maxval +2]) # set ylim on data axes 
    ax.set_xlim([-.5,numrots-1+.05]) # set xlim on data axes 


    return inputsl 

sldr = plotShifts(uint8(1)) 
plt.show() 
+0

doh!完全忘了我是如何混合OOP和状态机界面的。 –

+0

我知道我可以调用轴来创建一个轴对象,因为我为滑块做了这个操作,但是如何将它添加到绘图?我已经尝试了几个变体,在plot对象上调用set_axes或者将它作为axes关键字传递给构造函数,并且它们都使我的绘图消失... –

+1

@JosephGarvin请参阅编辑 – tacaswell

1

很可能是因为MAXVAL = 7在这条线

inputsl = Slider(inputax, 'Input', 0, maxval, valinit=0, valfmt="%d") 
+0

其实当我现在尝试它... MAXVAL是255,所以也许那不是它... –

+0

没错这就是255,因为当你指定-1到一个无符号数,你获得最高的无符号数,这对于一个uint8是255. –