我想绘制一个字节的按位循环移位的值。我想要一个滑块让我改变原来的输入值。我在matplotlib站点上使用滑块示例作为参考,但由于某些原因,即使我在运行脚本时传递0-255作为滑块范围,范围始终为0-7。我猜测,滑块被锁定到我的最大数量的x值,但我不知道如何。我如何让滑块让我选择完整的0-255范围?为什么matplotlib的Slider只允许0-7的范围?
此外,尽管最小/最大我给了滑块它插入一些填充在前面低于0,并随机在我的滑块中间绘制一条垂线。我如何摆脱它? (也有什么它的目的不是明显对我?)滑块
图片仅供上升到7:
代码:
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()
doh!完全忘了我是如何混合OOP和状态机界面的。 –
我知道我可以调用轴来创建一个轴对象,因为我为滑块做了这个操作,但是如何将它添加到绘图?我已经尝试了几个变体,在plot对象上调用set_axes或者将它作为axes关键字传递给构造函数,并且它们都使我的绘图消失... –
@JosephGarvin请参阅编辑 – tacaswell