Skip to content
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

Make hist with 'step' histtype draw Line2D instead of Patch #233

Closed
ddale opened this issue Jun 20, 2011 · 18 comments
Closed

Make hist with 'step' histtype draw Line2D instead of Patch #233

ddale opened this issue Jun 20, 2011 · 18 comments

Comments

@ddale
Copy link
Contributor

ddale commented Jun 20, 2011

Original report at SourceForge, opened Fri Feb 11 22:02:28 2011

One of the annoying things about axes.hist is that with histtype='step' the automatic legend style is an empty box, instead of a line, as I would expect. This behaviour doesn't seem to make sense, because it seems a line would be much more appropriate for this case. Example code to demonstrate this:

import matplotlib.pyplot as plt
plt.hist([0,1,1,2,2,2], [0,1,2,3], histtype='step', label="histtype='step'")
plt.legend()
plt.show()

With the current uncustomisability of legend styles one can get around this by using proxy Line2D objects in building the legend, but this can be a common operation and becomes messy and annoying.

The cause of this is the fact that in axes.py::7799 (current SVN head), in axes.hist, patch objects are always created, even for the line-based step style. I searched the tracker, and couldn't find this mentioned before.

Attached is a patch that makes the very simple change of swapping out the call to .fill for .plot (only the function is changed here, not yet the documentation), and it appears to work but I haven't tested exhaustively.

  • Is this intended behaviour, that I am just not understanding the requirement for?
  • This will cause the return signature to possibly include Line2D's, instead of just patches. Will this break anything?

Thoughts?

SourceForge History

  • On Fri Feb 11 22:02:28 2011, by ndevenish: File Added: 401440: histtype_plot.patch
@efiring
Copy link
Member

efiring commented May 29, 2013

The patch is lost, but the idea is simple and the questions it raises are valid. This needs to be reviewed and either rejected or implemented.

@mdboom
Copy link
Member

mdboom commented May 29, 2013

I think stepped histograms do return Line2D objects presently (at least in 1.3.x), so maybe this has magically been resolved in the meantime?

@efiring
Copy link
Member

efiring commented May 29, 2013

I tried the example from the original report, and it still produced a box instead of a line in the legend.

@mdboom
Copy link
Member

mdboom commented May 29, 2013

My bad -- I was just confused. Move along... ;)

@tacaswell
Copy link
Member

@neggert As you have been poking at the histogram code recently, have you looked at this issue?

@neggert
Copy link
Contributor

neggert commented Aug 19, 2013

I haven't, but do we really want hist to return something different depending on the histtype?

@eteq
Copy link

eteq commented Sep 17, 2013

As I user, I would answer yes to @neggert's question - I almost always have to leave histograms off legends (or make a "fake" legend entry by plotting outside the axes box), as I almost always want 'step' mode but find that the box in the legend is endlessly confusing to people I show the plots to.

@tacaswell
Copy link
Member

@eteq If that is the case, than use step to draw your histograms instead of hist

@eteq
Copy link

eteq commented Sep 19, 2013

@tacaswell - the value of hist is that it automatically does the histogram counting and takes care of the edges. I realize that I don't have to do it at all - I could just do it all with bar or plot. But the point is that it makes common use-cases easy one-liners, and I think this helps the plot make more sense in the common use case.

@tacaswell
Copy link
Member

Would it be sensible to add a function hist_step which returns a Line2D object? That would seem to make every one happy, at the expense of adding yet another function to axes and pyplot.

@eteq
Copy link

eteq commented Oct 27, 2013

That would certainly work for me, and is probably a better scheme than the current histtype. (I think I may have issued the PR to create that, so if so, my bad!)

@neggert
Copy link
Contributor

neggert commented Oct 28, 2013

There's going to be a lot of code duplication between hist and hist_step. Maybe turn the guts into a helper function which both hist and hist_step call?

I guess I should also note that I prefer the outlined empty box (the current state) in the legend to just a line (the proposed change), although not strongly.

If we make the proposed change, what do we do with the legend for a filled step histogram? I think that should definitely stay as it is.

@eteq
Copy link

eteq commented Oct 28, 2013

Maybe add a hist_step that has the new behavior (and any subsequent development), but leave the hist w/ histtype keyword as it is (probably good anyway for backwards compatibility)?

@tacaswell
Copy link
Member

Issue #827 might also be of interest to this discussion.

@tacaswell
Copy link
Member

Closing this as it has been dead for almost a year and is originally from 2011.

The fundamental problem is that the return type of hist should not change based on the input (that way lies madness), but we don't want to have two copies of hist (the monster that it is) floating around. The ultimate solution is probably via the manager/controller layer.

@tlatorre-uchicago
Copy link

Is there any example on how to make it plot with lines instead of empty boxes? The only reference I could find was https://stackoverflow.com/questions/19988556/how-to-use-alternative-legend-handler-for-histogram.

@andrzejnovak
Copy link
Contributor

This is still bothering me in 2019.

@tlatorre-uchicago
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants