Python:如何将x轴上的方形图移动给定值?

时间:2016-07-07 09:11:06

标签: python python-3.x matplotlib

X = [0.3,0.7,0,0.8]

我想如下图。但是使用一般plt.plot我不能在下面。

这里我错过了实现这个的逻辑?

enter image description here

2 个答案:

答案 0 :(得分:2)

我希望这个简单的例子可以帮到你:

import numpy as np
import matplotlib.pyplot as plt

N = 5
values = (5, 7, 4, 9, 2)

ind = np.arange(N)  
width = 0.35      

fig, ax = plt.subplots()
rects1 = ax.bar(ind, values, width, color='r')

# add some text for labels, title and axes ticks
ax.set_ylabel('Y-label')
ax.set_title('X-label')
ax.set_xticks(ind + width)
ax.set_xticklabels(('1', '2', '3', '4', '5'))


def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)

plt.show()
  

输出:   enter image description here

答案 1 :(得分:2)

在了解到你想要绘制一个看起来像二进制信号图的图形之后,我找到了你可能会觉得有用的例子:

import numpy as np
from matplotlib.pyplot import step, xlim, ylim, show
x = np.arange(0, 7)
y = np.array([0, 1, 0, 1, 1, 0, 1])
xlim(0, 7)
ylim(0, 1.5)
step(x, y)
show()

enter image description here