【问题标题】:Unfilled bar plot in matplotlibmatplotlib 中未填充的条形图
【发布时间】:2012-08-07 04:41:40
【问题描述】:

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

【问题讨论】:

  • 是 plt.bar(x, y, color="None") 你想要什么?
  • 不,因为我想消除条形相邻的垂直线。
  • stackoverflow.com/questions/11297030/… 我在这里找到了答案。这实际上正是我想要做的(使用条形图作为直方图),但我没有找到它,因为我正在寻找关于条形图的问题。

标签: matplotlib bar-chart


【解决方案1】:

[阅读cmets后添加答案] 将条形图的可选关键字设置为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)")

将给出:

或者使用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)")

【讨论】:

  • 第二张图正是我认为他想要的,这也是我想要的。
【解决方案2】:

虽然 OP 链接到的帖子回答了一个与直方图阶梯图有关的稍微不同的问题,但对于通过这里的任何人来说,这里是一个解决方案,专门试图关闭 pyplot.bar 条形图中的面部颜色:

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()

请注意,条形边缘具有可设置的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()

【讨论】:

    【解决方案3】:

    我看到你在 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)))
    

    【讨论】:

    • 请提供代码使用matplotlib.pyplot.step回答问题,作为说明。
    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多