如何使用 for 循环从二进制数为相应的十进制数生成动画方波
How do you generate an animated square wave from binary number for the respective decimal number using for loop
我正在使用以下代码使用 for 循环生成方波格式 [例如:从 0 到 5]。我能够打印相应的二进制值,但无法绘制方波 dynamically.In 除此之外,我无法动态调整图中 x 轴的大小 window。我在 Matplotlib 动画部分找不到任何合适的代码。有人可以帮助我吗?
import numpy as np
import matplotlib.pyplot as plt
limit=int(raw_input('Enter the value of limit:'))
for x in range(0,limit):
y=bin(x)[2:]
data = [int(i) for i in y]
print data
xs = np.repeat(range(len(data)),2)
ys = np.repeat(data, 2)
xs = xs[1:]
ys = ys[:-1]
plt.plot(xs, ys)
plt.xlim(0,len(data)+0.5)
plt.ylim(-2, 2)
plt.grid()
plt.show()
#plt.hold(True)
#plt.pause(0.5)
plt.clf()
你所说的问题很含糊,所以我要冒险,假设你想要的是使用相同的数字绘制一系列等长的二进制代码中间有延迟。
所以,这里有两个问题:
生成适当的二进制代码
连续绘制这些代码
1。生成适当的二进制代码
据我合理猜测,您想绘制相同长度的二进制代码。因此,您必须对代码进行零填充,使它们的长度相同。一种方法是使用 python 的内置 zfill 函数。
例如
bin(1).zfill(4)
这也说明了如果要保持 x 轴范围不变,则必须知道要绘制的最大二进制字符串的长度。由于不清楚您是否想要恒定长度的字符串,我就把它留在这里。
2。连续绘制这些代码
有几种不同的方法可以在 matplotlib 中创建动画。我发现手动更新数据比目前的动画 API 更灵活,错误更少,所以我将在这里这样做。我还删除了一些我不清楚的代码部分。
这是一个简单的实现:
import matplotlib.pyplot as plt
import numpy as np
# Enable interactive plotting
plt.ion()
# Create a figure and axis for plotting on
fig = plt.figure()
ax = fig.add_subplot(111)
# Add the line 'artist' to the plotting axis
# use 'steps' to plot binary codes
line = plt.Line2D((),(),drawstyle='steps-pre')
ax.add_line(line)
# Apply any static formatting to the axis
ax.grid()
ax.set_ylim(-2, 2)
# ...
try:
limit = int(raw_input('Enter the value of limit:'))
codelength = int(np.ceil(np.log2(limit)))+1 # see note*
ax.set_xlim(0,codelength)
for x in range(0,limit):
# create your fake data
y = bin(x)[2:].zfill(codelength)
data = [int(i) for i in y]
print data
xs = range(len(data))
line.set_data(xs,data) # Update line data
fig.canvas.draw() # Ask to redraw the figure
# Do a *required* pause to allow figure to redraw
plt.pause(2) # delay in seconds between frames
except KeyboardInterrupt: # allow user to escape with Ctrl+c
pass
finally: # Always clean up!
plt.close(fig)
plt.ioff()
del ax,fig
结果
*注意:我用一个额外的零填充二进制代码以使绘图看起来正确。
我正在使用以下代码使用 for 循环生成方波格式 [例如:从 0 到 5]。我能够打印相应的二进制值,但无法绘制方波 dynamically.In 除此之外,我无法动态调整图中 x 轴的大小 window。我在 Matplotlib 动画部分找不到任何合适的代码。有人可以帮助我吗?
import numpy as np
import matplotlib.pyplot as plt
limit=int(raw_input('Enter the value of limit:'))
for x in range(0,limit):
y=bin(x)[2:]
data = [int(i) for i in y]
print data
xs = np.repeat(range(len(data)),2)
ys = np.repeat(data, 2)
xs = xs[1:]
ys = ys[:-1]
plt.plot(xs, ys)
plt.xlim(0,len(data)+0.5)
plt.ylim(-2, 2)
plt.grid()
plt.show()
#plt.hold(True)
#plt.pause(0.5)
plt.clf()
你所说的问题很含糊,所以我要冒险,假设你想要的是使用相同的数字绘制一系列等长的二进制代码中间有延迟。
所以,这里有两个问题:
生成适当的二进制代码
连续绘制这些代码
1。生成适当的二进制代码
据我合理猜测,您想绘制相同长度的二进制代码。因此,您必须对代码进行零填充,使它们的长度相同。一种方法是使用 python 的内置 zfill 函数。
例如
bin(1).zfill(4)
这也说明了如果要保持 x 轴范围不变,则必须知道要绘制的最大二进制字符串的长度。由于不清楚您是否想要恒定长度的字符串,我就把它留在这里。
2。连续绘制这些代码
有几种不同的方法可以在 matplotlib 中创建动画。我发现手动更新数据比目前的动画 API 更灵活,错误更少,所以我将在这里这样做。我还删除了一些我不清楚的代码部分。
这是一个简单的实现:
import matplotlib.pyplot as plt
import numpy as np
# Enable interactive plotting
plt.ion()
# Create a figure and axis for plotting on
fig = plt.figure()
ax = fig.add_subplot(111)
# Add the line 'artist' to the plotting axis
# use 'steps' to plot binary codes
line = plt.Line2D((),(),drawstyle='steps-pre')
ax.add_line(line)
# Apply any static formatting to the axis
ax.grid()
ax.set_ylim(-2, 2)
# ...
try:
limit = int(raw_input('Enter the value of limit:'))
codelength = int(np.ceil(np.log2(limit)))+1 # see note*
ax.set_xlim(0,codelength)
for x in range(0,limit):
# create your fake data
y = bin(x)[2:].zfill(codelength)
data = [int(i) for i in y]
print data
xs = range(len(data))
line.set_data(xs,data) # Update line data
fig.canvas.draw() # Ask to redraw the figure
# Do a *required* pause to allow figure to redraw
plt.pause(2) # delay in seconds between frames
except KeyboardInterrupt: # allow user to escape with Ctrl+c
pass
finally: # Always clean up!
plt.close(fig)
plt.ioff()
del ax,fig
结果
*注意:我用一个额外的零填充二进制代码以使绘图看起来正确。