在 Matlab 中有一个很好的函数可以绘制背靠背直方图。我需要在 matplotlib 中创建一个类似的图表。任何人都可以显示一个工作代码示例吗?
问问题
2933 次
2 回答
5
感谢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()
于 2009-08-27T11:32:36.310 回答
2
这个matplotlib 用户邮件帖子有一些用于上下而不是左右 的双直方图的示例代码。这是他链接到的示例输出。
如果 up-down 绝对不适合您,只需几分钟即可将 y 轴上的操作与 x 轴操作交换。
此外,您的链接不是 MATLAB 函数,而是某人用大约 40 行编写的实际脚本。您实际上可以查看脚本源并尝试移植它,因为 MATLAB 和 matplotlib 具有相当接近的语法。
于 2009-08-27T11:07:49.853 回答