In an elaboration of my previous post a few minutes ago about weighted histograms: As I see it I have three options in getting matplotlib to draw weighted histograms: 1) Hack up the source. This is fine but I try not to do it unless I have to. 2) Define my own function that creates the histogram, then pass it to some convenient "Plot this as a histogram" function in matplotlib. 3) Try to "force" matplotlib to do what I want be explicitly modifying its namespace. I do this routinely to add functionality that I consider to be missing from the packages that I use without maintaining a local branch of the source. Here's one example: ############################ # Add some stuff to the scipy namespace import scipy def lrange(l, h, dex): """Log-spaced array""" return 10**arange(log10(l), log10(h), dex) scipy.lrange = lrange In this case I would do something like define my_make_hist() and my_draw_hist() to do what I want, and then force them into the matplotlib namespace with: matplotlib.matlab.hist = my_plot_hist matplotlib.mlab.hist = my_make_hist Unfortunately the call to gca() in matplotlib.matlab.hist() seems to prevent this from working the way I want it to. So, option 1 is unpalateable but possible, option 2 is out b/c matplotlib seems to lack such a function, and option 3 is out b/c matplotlib isn't resolving the function references the way I would like. So, I guess the question is does anyone know any neat tricks to get this to work? Thanks a bunch, Greg
>>>>> "Greg" == Greg Novak <no...@uc...> writes: Greg> So, I guess the question is does anyone know any neat tricks Greg> to get this to work? Ask you friendly neighborhood developer to support this in the hist signature? There are a few ways to go here. 1) hist takes an option kwarg n=None which is len(bins) and gives the counts. If n is not None, use it rather than calling mlab.hist. In this case, the user would be obliged to pass bins as a sequence rather than an int. If n is None, call mlab.hist 2) hist takes an optional kwarg func=None. If func is None use mlab.hist, otherwise call n,bins = func(x, bins, normed); ie func has the same signature as mlab.hist. 3) matplotlib.axes.Axes.hist (which the matlab interface function wraps) is just a 5 line function, and one of those lines is calling mlab.hist - take a look at the src code in axes.py. Once you have your n, bins from your custom function, you can call bar, just as hist does. Ie it's so easy to plot a histogram using bar that you may not need to bother with altering the matplotlib.matlab hist. I'm happy with any of these. Suggestions welcome. JDH
On Tue, 2004年09月28日 at 18:52, John Hunter wrote: > Once you have > your n, bins from your custom function, you can call bar, just as > hist does. Ie it's so easy to plot a histogram using bar that you > may not need to bother with altering the matplotlib.matlab hist. I personally vote for this option. I had a similar need just a couple of days ago, namely creating a summed histogram from a number of repeated simulations, and I just did the sums myself and called bar. It is probably best to keep matplotlib.matlab hist clean and simple. Steve Walton