New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stacked kwarg to hist and implement stacked hists for step histtype #847
Conversation
Sorry this PR has languished for so long. @WeatherGod: Do you have any comments on this approach in relation to the more general plans for "stacked" plots. And I will make my usual comment that it's great to see an example -- we should also have a unit test for this. |
I will likely be at the 2012 SciPy conference. I am sure that we can discuss issues such as this and develop some sort of over-arching design plan. |
Sorry for the delay, I was taking my PhD candidacy exam, so I didn't have time to work on this. I added a test for the new functionality. Incidentally, I don't think there are any other tests for for axes.hist. Maybe in the future, I'll write some. Did any of the higher-ups get a chance to discuss the fate of stacked plots at Scipy2012? |
I totally understand the need to focus on the candidacy exam. At SciPy2012, many things got discussed, but not that, unfortunately. Maybe a discussion should be started on the mailing list? |
And it looks like you need to do a rebase. Make sure that your test images are based on the rebased version as there may have been changes to small details such as text rendering or line anti-aliasing. |
I've rebased and squashed it down to 1 commit. I also sent around a message to matplotlib-devel trying to start a conversation about the general idea of stacked-ness. Please reply there if you interested in the more general issues of how to approach this problem. |
So I haven't gotten any replies to the message I sent to matplotlib-devel. Any thoughts on how to proceed? |
for i in xrange(nx): | ||
# this will automatically overwrite bins, | ||
# so that each histogram uses the same bins | ||
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) | ||
if mlast == None : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny style point: please delete space between condition and colon, here and below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny style point #3: it is preferable to use obj is None
(PEP8: "Comparisons to singletons like None should always be done with is or is not, never the equality operators.").
I'm sorry you have been having a hard time getting responses. |
Just cleaned up a few style issues and rebased. |
@@ -7919,6 +7927,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |||
# multiple hist with data of different length | |||
x = [np.asarray(xi) for xi in x] | |||
|
|||
x = x[::-1] # reverse datasets for caculating stacked hist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you reverse the order? This seems like an unnecessary change from the previous behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the loop starting on line 8002, I want to iterate over the datasets in reverse order so that the first input goes on top. Note that I put them back in the correct order at line 8027.
Now that you point this out, it might be better to reverse the loop than the vector. This would result in the first dataset being plotted at the bottom, which might be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've put up the alternative. This is probably the minimally invasive way to do it.
P.S. I was mistaken about the first dataset going at the bottom. That would require more extensive changes.
Will this be merged in soon? Looking forward to this feature! For now I am using many complicated https://github.com/rootpy/rootpy/blob/master/rootpy/plotting/root2matplotlib.py but ROOT plots stacked histograms like this "stacked stepfilled" feature. In matplotlib, stacked bar plots can have very thin white lines between bars in vector-based formats, which I find unappealing. |
@ndawe If you don't mind installing from source, it is possible to use this changeset without it having been merged into the main codebase. Add a new git remote to point to the repo of @neggert. Then you can checkout his branch and install :) If you need help with any of this, let me know. At least now you don't have to wait for it to be merged. |
The only thing left to do is to update pyplot.py using the boilerplate.py script in the mpl root directory and to add a note to docs/users/whats_new.rst. Note, I am not sure if the feature freeze has happened yet or not, so it is possible this may get deferred to the next release. |
I think we can still get this in to 1.2 by the end of the week, if @WeatherGod's tiny fixes are addressed. |
Done. The changes made by boilerplate.py included a bunch of whitespace changes. I left them out of the commit, but I can put them back if there was some good reason for them. |
Rebased as well. |
New hist functionality | ||
---------------------- | ||
|
||
Nic Eggert added a new `stack kwarg to :meth:`~matplotlib.pyplot.hist` that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should stack
have a closing backtick?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, isn't the kwarg called stacked
, not stack
?
# this will automatically overwrite bins, | ||
# so that each histogram uses the same bins | ||
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) | ||
if mlast is None: | ||
mlast = np.zeros(len(bins)-1, np.int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought... are we guaranteed that bins will have a non-zero length? If not, then it would be a good idea to catch the length-zero case and raise a descriptive exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bins
argument to np.histogram
takes either the number of bins or an array of bin edges.
I just played around a little, and I don't think it's possible for np.histogram
to return a zero-length array of bin edges. It raises an exception if one tries to set bins=0
In fact, the only way I can get it to return anything less than a length-2 array is by explicitly passing only one bin edge. This will obviously result in some strange behavior, but that's a pretty edge case. If np.histogram
doesn't raise an exception for that, I don't think we should, either.
Anyone have any idea why the bot keeps telling me the PR fails? I've rebased onto the latest version of master. |
Don't worry too much about the Travisbot right now, we are still working out the kinks. As for np.histogram(), I have a vague recollection of it being possible for it to return empty arrays in bad situations, which was later fixed. But it was an extreme edge case that I am not sure is possible to trigger here anyway. |
However, the logs do speak of stacked plots failing (not yours, but some of the originals). Maybe worth investigating further. |
I'm not seeing any additional test failures with this branch on my local machine. Are others (ignoring Travis, since it seems to throw a lot of false positive failures). What's left to be done on this? Anything? |
I think mine looks fine. Here's the test log if someone more knowledgeable wants to look at it: https://gist.github.com/3691954. If those look good, IMO, this is good to go. |
Add stacked kwarg to hist and implement stacked hists for step histtype
This PR addresses #831. The main goal was to implement stacked histograms when using the
step
orstepfilled
histtypes. Rather than add new histtypes as with thebarstacked
histtype, a newstacked
kwarg was added. This kwarg behaves as expected for all four histtypes.I've added an example showing what happens when
histtype="stepfilled", stacked=True
. Here is the output of the new example: