From: Tony S Yu <ts...@gm...> - 2010-10-04 20:35:05
|
On Oct 4, 2010, at 4:09 PM, Friedrich Romstedt wrote: >> """ >> First attempt at a histogram strip chart (made up name). >> if-main block taken from [1] except that I've replaced uniform distributions >> with normal distributions. >> [1] http://matplotlib.sourceforge.net/examples/pylab_examples/boxplot_demo3.html >> """ >> import numpy as np >> import matplotlib.pyplot as plt >> from matplotlib import collections >> >> NORM_TYPES = dict(max=max, sum=sum) >> class BinCollection(collections.PatchCollection): >> def __init__(self, hist, bin_edges, x=0, width=1, cmap=plt.cm.gray_r, >> norm_type='max', **kwargs): >> yy = (bin_edges[:-1] + bin_edges[1:])/2. >> heights = np.diff(bin_edges) >> bins = [plt.Rectangle((x, y), width, h) for y, h in zip(yy, heights)] >> norm = NORM_TYPES[norm_type] >> fc = cmap(np.asarray(hist, dtype=float)/norm(hist)) > >> super(BinCollection, self).__init__(bins, facecolors=fc, **kwargs) > > Is this equivalent to writing collections.PatchCollection.__init__() > and what are the advantages of super()? I believe collections.PatchCollection.__init__() is equivalent. In this instance, I don't think there are advantages (or disadvantages) to using super---it's just how I'm used to writing classes. > I think you can use axes.pcolor() to replace BinCollection. pcolor() > just adds a collection similar to what you do now by hand for you. > With appropriate arguments it should do the job. You can also look > into pcolorfast() and pcolormesh(). Yes, you're right. This was actually my main question in the original post; i.e. what plotting function to start with. I'm not really sure how I overlooked pcolor(mesh) as a viable option. Thanks. > >> def histstrip(x, positions=None, widths=None, ax=None): >> if ax is None: >> ax = plt.gca() >> if positions is None: >> positions = range(1, len(x) + 1) >> if widths is None: >> widths = np.min(np.diff(positions)) / 2. * np.ones(len(positions)) >> for data, x_pos, w in zip(x, positions, widths): >> x_pos -= w/2. >> hist, bin_edges = np.histogram(data) > > No other arguments to numpy.histogram() allowed? As I mentioned, this was just a function I hacked together. I didn't try to make it general purpose (yet). > > In my opinion this is too special to be added as a general matplotlib > plotting feature. I'd agree that it's pretty specialized; especially since I haven't been able to find any mention of it. I'm still curious if there's a name for this type of plot if anyone out there knows. Best, -Tony > > Friedrich |