Why Does Matplotlib's Slider Only Allow A Range Of 0-7?
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 matplotl
Solution 1:
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.
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.
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
defsizeof(x):
return2 ** [uint8, uint16, uint32, uint64].index(x)
defrot(x, i):
returntype(x)((x >> i) | (x << (sizeof(type(x))*8 - i)))
defplotShifts(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 inrange(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")
defupdate(x):
vals = [rot(origType(x), origType(i)) for i inrange(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 axesreturn inputsl
sldr = plotShifts(uint8(1))
plt.show()
Solution 2:
most likely because maxval =7 in this line
inputsl = Slider(inputax, 'Input', 0, maxval, valinit=0, valfmt="%d")
Post a Comment for "Why Does Matplotlib's Slider Only Allow A Range Of 0-7?"