【问题标题】:back-to-back histograms in matplotlibmatplotlib 中的背靠背直方图
【发布时间】:2010-11-23 08:15:32
【问题描述】:

有一个很好的函数可以在 Matlab 中绘制back to back histograms。我需要在 matplotlib 中创建一个类似的图表。任何人都可以显示一个有效的代码示例吗?

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    这个matplotlib users mailing post 有一些示例代码,用于表示上下而不是左右的双直方图Here's the example output他链接到。

    如果 up-down 绝对不适合您,只需几分钟即可将 y 轴上的操作与 x 轴上的操作交换。

    另外,您的链接不是 MATLAB 函数,而是某人用大约 40 行编写的实际脚本。您实际上可以查看脚本源并尝试移植它,因为 MATLAB 和 matplotlib 的语法相当接近。

    【讨论】:

      【解决方案2】:

      感谢 Mark Rushakoff 指出的链接,以下是我最终所做的

      import numpy as np
      from matplotlib import pylab as pl
      
      dataOne = get_data_one()
      dataTwo = get_data_two()
      
      hN = pl.hist(dataTwo, orientation='horizontal', normed=0, rwidth=0.8, label='ONE')
      hS = pl.hist(dataOne, bins=hN[1], orientation='horizontal', normed=0, 
          rwidth=0.8, label='TWO')
      
      for p in hS[2]:
          p.set_width( - p.get_width())
      
      xmin = min([ min(w.get_width() for w in hS[2]), 
                      min([w.get_width() for w in hN[2]]) ])
      xmin = np.floor(xmin)
      xmax = max([ max(w.get_width() for w in hS[2]), 
                      max([w.get_width() for w in hN[2]]) ])
      xmax = np.ceil(xmax)
      range = xmax - xmin
      delta = 0.0 * range
      pl.xlim([xmin - delta, xmax + delta])
      xt = pl.xticks()
      n = xt[0]
      s = ['%.1f'%abs(i) for i in n]
      pl.xticks(n, s)
      pl.legend(loc='best')
      pl.axvline(0.0)
      pl.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        相关资源
        最近更新 更多