在matplotlib中绘制连续直方图
在Matlab中,有一个很不错的功能可以绘制背靠背的直方图。我想在matplotlib中创建一个类似的图表。有没有人能给我一个可以运行的代码示例?
2 个回答
2
这篇matplotlib用户邮件帖子里有一些示例代码,可以用来制作一种双直方图,这种图是上下排列的,而不是左右排列的。这里是他链接的示例输出。
如果上下排列的方式实在不适合你,只需要几分钟就可以把y轴和x轴的操作互换一下。
另外,你提供的链接不是一个MATLAB函数,而是一个大约40行的实际脚本。你可以查看这个脚本的源代码,尝试把它移植过来,因为MATLAB和matplotlib的语法其实挺相似的。
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()