为什么matplotlib的Slider只允许0-7范围? [英] Why does matplotlib's Slider only allow a range of 0-7?
问题描述
我正在尝试在1个字节上绘制按位循环移位的值.我想要一个滑块让我更改原始输入值.我正在使用matplotlib网站上的滑块示例作为参考,但是由于某些原因,即使我在运行脚本时以0-255作为滑块范围,该范围始终为0-7.我猜想滑块以某种方式被锁定到我的x值的最大数量,但是我看不到如何.如何让滑块让我选择0-255的整个范围?
I am trying to plot the values of a bitwise circular shift on 1 byte. I'd like to have a slider let me change the original input value. I'm using the slider example on the matplotlib site for reference, but for some reason even though I pass in 0-255 as my slider range when I run my script the range is always 0-7. I'm guessing that somehow the slider is getting locked to my maximum number of x values, but I don't see how. How do I get the slider to let me pick the full 0-255 range?
此外,尽管我给了滑块最小/最大值,但它在前面插入了一些填充,使其低于0,并在滑块的中间随机绘制了一条顶点线.我如何摆脱它? (也是为了什么?目的对我来说并不明显)
Also, despite the min/max I've given the slider it inserts some padding for going below 0 at the front, and randomly draws a verticle line in the middle of my slider. How do I get rid of it? (also what is it for? The purpose isn't obvious to me)
滑块的图片最多只能显示7张:
Picture of slider only going up to 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()
推荐答案
问题出在最后一行plt.axis([-0.5, numrots-1 + 0.5, -2, maxval + 2])
上,该行作用在保存滑块的轴上,而不是数据所在的轴上.
The problem is in the last line plt.axis([-0.5, numrots-1 + 0.5, -2, maxval + 2])
which is acting on the axes that holds the slider, not on the axis with the data.
对于任何编程性的东西,我建议使用面向matplotlib
的OO接口,而不是使用pyplot
接口. pyplot
界面适用于交互式内容,但具有很多隐藏状态.
I would recommend using the OO interface to matplotlib
rather than the pyplot
interface for anything programmatic. The pyplot
interface is good for interactive stuff, but it has a good deal of hidden state.
由于回调的工作方式,您还需要返回对slider
对象的引用.
You also need to return a reference to the slider
object due to the way call backs work.
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()
这篇关于为什么matplotlib的Slider只允许0-7范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!