【问题标题】:How can I set the dash length in pyplot.hist?如何在 pyplot.hist 中设置破折号长度?
【发布时间】:2015-09-19 23:42:08
【问题描述】:

我知道可以在 plt.plot 中设置破折号长度和间隙大小,但有没有办法在 plt.hist 中设置?这是我的命令的样子: plt.hist(x, bins=5, range=(7.,11.), facecolor='None', linestyle='dashed', linewidth=2, normed=1)

【问题讨论】:

  • 线型的设置已经统一在master上。

标签: python matplotlib plot histogram linestyle


【解决方案1】:

只需阅读official documentation

set_dashes 是一个函数,它采用一系列以点为单位的开和关长度。

所以set_dashes((3,3)) 应该产生与set_dashes((15,15)) 不同的东西。

现在,对于hist,这将无法真正起作用,因为设置线条属性最多只能改变轮廓的外观。

你可以做的是

  1. 使用numpyhistogram函数;无论如何,它被pyplot的hist使用,然后
  2. 使用stem绘制结果。

【讨论】:

  • set_dashes()lines, = plt.plot(x)lines.set_dashes(...) 类似。但是hist 函数不允许将绘图存储到变量中。还是这样?
  • @tcaswell:嗯,不。实际上,使用 stem 可以让您绘制线条而不是矩形。
  • @MarcusMüllerꕺꕺ:不确定如何让stem 绘制直方图。那可能吗?我检查了文档,但一无所获。
  • 直方图只是一组高度,对吧?所以没有什么是茎图无法想象的。
【解决方案2】:

正如另一个答案指出的那样,set_dashes 方法不适用于直方图。但是,您可以通过将破折号元组直接传递给“线型”来实现对线型的精细控制(请参阅此处的文档https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/linestyles.html

在您的示例中,您可以在链接中实现“松散虚线”样式,如下所示

x=[7]*10
plt.hist(x, bins=5, range=(7.,11.), ec='k', facecolor='None', 
         linewidth=2, normed=1, linestyle=(0,(5,10)))

这适用于我的 matplotlib 3.0.3。请注意,我还必须添加 ec='k' 才能完全显示直方图的轮廓...

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 2013-10-09
    • 1970-01-01
    • 2023-02-08
    • 2023-03-20
    • 2016-04-05
    • 2021-10-13
    • 1970-01-01
    • 2011-12-17
    相关资源
    最近更新 更多