2024-06-17 12:43:06 发布
网友
我正在尝试使用python中的pyplot绘制数字信号波形图,例如01010101010101
Pyplot有可能吗?
只需使用plt.step
plt.step
plt.step(x, y, where='pre')
有关示例,请参见the docs和Linestyle in matplotlib step function以及Step function in matplotlib。
正如tcaswell所指出的,正确的函数是step。下面是一个近似于给定绘图的示例:
step
import matplotlib.pyplot as plt import numpy as np def my_lines(ax, pos, *args, **kwargs): if ax == 'x': for p in pos: plt.axvline(p, *args, **kwargs) else: for p in pos: plt.axhline(p, *args, **kwargs) bits = [0,1,0,1,0,0,1,1,1,0,0,1,0] data = np.repeat(bits, 2) clock = 1 - np.arange(len(data)) % 2 manchester = 1 - np.logical_xor(clock, data) t = 0.5 * np.arange(len(data)) plt.hold(True) my_lines('x', range(13), color='.5', linewidth=2) my_lines('y', [0.5, 2, 4], color='.5', linewidth=2) plt.step(t, clock + 4, 'r', linewidth = 2, where='post') plt.step(t, data + 2, 'r', linewidth = 2, where='post') plt.step(t, manchester, 'r', linewidth = 2, where='post') plt.ylim([-1,6]) for tbit, bit in enumerate(bits): plt.text(tbit + 0.5, 1.5, str(bit)) plt.gca().axis('off') plt.show()
这是我的尝试。我使用的技巧是将每个x和y坐标与np.repeat加倍,然后将它们偏移1以生成每个角的坐标。如果需要更好的解释请告诉我。可以使用plt.grid(True)添加网格线。
np.repeat
plt.grid(True)
import numpy as np import matplotlib.pyplot as plt data = [1, 0, 0, 1, 1, 0, 1, 0] xs = np.repeat(range(len(data)), 2) ys = np.repeat(data, 2) xs = xs[1:] ys = ys[:-1] plt.plot(xs, ys) plt.ylim(-0.5, 1.5) plt.show()
只需使用
plt.step
有关示例,请参见the docs和Linestyle in matplotlib step function以及Step function in matplotlib。
正如tcaswell所指出的,正确的函数是
step
。下面是一个近似于给定绘图的示例:这是我的尝试。我使用的技巧是将每个x和y坐标与
np.repeat
加倍,然后将它们偏移1以生成每个角的坐标。如果需要更好的解释请告诉我。可以使用plt.grid(True)
添加网格线。相关问题 更多 >
编程相关推荐