matplotlib中未填充的条形图

时间:2012-08-09 21:26:02

标签: matplotlib bar-chart

使用直方图,有一个简单的内置选项histtype='step'。如何制作相同风格的条形图?

3 个答案:

答案 0 :(得分:7)

[阅读评论后添加答案] 为条形图设置可选关键字为fill=False

import matplotlib.pyplot as plt

plt.bar(bins[:5], counts[:5], fill=False, width=60)  # <- this is the line

plt.title("Number of nodes with output at timestep")
plt.xlabel("Node count")
plt.ylabel("Timestep (s)")

会给: enter image description here

或者使用plt.plot与关键字ls='steps'

plt.plot(bins[-100:], counts[-100:], ls='steps')
plt.title("Number of nodes with output at timestep")
plt.xlabel("Node count")
plt.ylabel("Timestep (s)")

enter image description here

答案 1 :(得分:2)

尽管OP链接到一个帖子,该帖子回答了与直方图步骤图有关的略有不同的问题,但这里有一个解决方案,适用于通过此处专门尝试关闭pyplot.bar条形图中的面部颜色的人:< / p>

import matplotlib.pyplot as plt
import numpy as np

# create x coords for the bar plot
x = np.linspace(1, 10, 10)

# cook up some random bar heights -- exact results may vary :-P
y = np.random.randn(10)
z = np.random.randn(10) * 2

# plot bars with face color off
plt.bar(x-0.2, y, width=0.4, edgecolor='purple', color='None')
plt.bar(x+0.2, z, width=0.4, edgecolor='darkorange', color='None')
plt.show()

enter image description here

请注意,条形边缘具有可设置的matplotlib.lines.Line2D属性,例如linewidthlinestylealpha等等:

plt.bar(x-0.2, y, width=0.4, edgecolor='purple', color='None', 
linewidth=0.75, linestyle='--')
plt.bar(x+0.2, z, width=0.4, edgecolor='darkorange', color='None',
linewidth=1.5, linestyle='-.')
plt.show()

enter image description here

答案 2 :(得分:0)

我看到您在this other topic上找到了答案,尽管如此,我仍然感觉matplotlib.pyplot.step也在做这项工作,而且更直接(see here)。

编辑: ,根据要求提供了一些示例代码来说明plt.step

的用法
import matplotlib.pyplot as plt
plt.step(list(range(10)),list(range(5))+list(range(5)))