Matplotlib中的连续直方图

4

在Matlab中,有一个很好的函数可以绘制背靠背直方图。我需要在matplotlib中创建一个类似的图形。有人能展示一个可行的代码示例吗?

2个回答

6

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

2
这个matplotlib用户邮件帖子有一些关于双直方图的示例代码,它上下移动而不是左右移动。他链接到的这里是示例输出

如果上下移动绝对行不通,只需要花费几分钟时间将y轴操作与x轴操作交换即可。

此外,你的链接不是MATLAB函数,而是一个实际的脚本,由大约40行代码编写而成。你可以查看脚本源代码并尝试移植,因为MATLAB和matplotlib具有相当接近的语法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接