我在python中玩游戏,我想创建一个步骤图函数,它接受一个二维列表,每个列表由1和0组成,并在单独的行上列出每个列表,类似于: clocking graph
当我运行我的代码时,它看起来像这样:Code output
可能有点难以阅读,尤其是在使用更大的列表时。我想在图形线之间创建空格,使其更具可读性。任何帮助将不胜感激。
# supress warning message
import warnings; warnings.simplefilter("ignore")
# extension libraries
import matplotlib.pyplot as plt
import numpy as np
bits = [[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]]
for i in range(len(bits)):
data = np.repeat(bits[i], 2)
t = 0.5 * np.arange(len(data))
plt.hold(True)
plt.step(t, data + i, linewidth=1.5, where='post', color='g')
plt.ylim([-1, 10])
# Labels the graphs with binary sequence
for tbit, bit in enumerate(bits[i]):
plt.text(tbit + 0.2, i, str(bit), fontsize=12, color='g')
# removes the built in graph axes and prints line every interation
plt.gca().axis('off')
plt.show()
答案 0 :(得分:0)
您只需通过增加y值来增加每条线的垂直间距。为了方便起见,我只需将i
乘以2即可将您的线条间隔得更远。如果你想要更多地控制线的位置,你可以有一个y值列表,然后迭代它。
bits = [[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]]
for i in range(len(bits)):
data = np.repeat(bits[i], 2)
t = 0.5 * np.arange(len(data))
plt.hold(True)
plt.step(t, data + i*2, linewidth=1.5, where='post', color='g')
# ^^^^^^
plt.ylim([-1, 10])
# Labels the graphs with binary sequence
for tbit, bit in enumerate(bits[i]):
plt.text(tbit + 0.2, 0.25+i*2, str(bit), fontsize=12, color='g')
# ^^^^^^^^^
# removes the built in graph axes and prints line every interation
plt.gca().axis('off')
plt.show()