问题
There is a nice function that draws back to back histograms in Matlab. I need to create a similar graph in matplotlib. Can anyone show a working code example?
回答1:
Thanks to the link pointed by Mark Rushakoff, following is what I finally did
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:
This matplotlib users mailing post has some sample code for a bihistogram that goes up and down instead of left and right. Here's the example output he linked to.
If up-down absolutely won't work for you, it should only take a few minutes to swap the operations on the y-axis with the x-axis operations.
Also, your link isn't a MATLAB function, it's an actual script that someone wrote in about 40 lines. You could actually look at the script source and try porting it, since MATLAB and matplotlib have fairly close syntax.
来源:https://stackoverflow.com/questions/1340338/back-to-back-histograms-in-matplotlib