8000 Allow scalar height for plt.bar by dstansby · Pull Request #7644 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Allow scalar height for plt.bar #7644

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

Merged
merged 4 commits into from
Jan 16, 2017

Conversation

dstansby
Copy link
Member

Fixes #6820

  • If a vertical bar, allow a scalar height
  • If a horizontal bar, allow a scalar width
  • Test that doing the above works without errors

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Dec 19, 2016

@cleanup
def test_bar_single_height():
# Check that a bar chart with a single height for all bars works
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly better to do

fig, ax = plt.subplots()
ax.bar(...)

which helps to protect from tests leaking into each other a bit more.

@@ -2025,6 +2025,8 @@ def make_iterable(x):
bottom = [0]

nbars = len(left)
if len(height) == 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 6 lines of code could be factored out of the if statement. Only nbars need to be inside the if - elif statement.

Copy link
Member Author
@dstansby dstansby Dec 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot - 4 of them can (I've done that below), but one of the if statements has to stay I think (as it's different for vertical or horizontal orientations)

Copy link
Member
@NelleV NelleV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are minor nitpicks, but it'd be nice to refactor part of the code outside of the if - elif statement, to avoid code duplication.

Copy link
Mem 8000 ber
@NelleV NelleV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@NelleV NelleV changed the title Allow scalar height for plt.bar [MRG+1] Allow scalar height for plt.bar Jan 1, 2017
@NelleV
Copy link
Member
NelleV commented Jan 1, 2017

@dstansby Can you rebase ? It seems there is a conflict between your branch and master.

@dstansby
Copy link
Member Author
dstansby commented Jan 1, 2017

Done!

@codecov-io
Copy link
codecov-io commented Jan 1, 2017

Current coverage is 62.11% (diff: 100%)

Merging #7644 into master will decrease coverage by <.01%

@@             master      #7644   diff @@
==========================================
  Files           174        174          
  Lines         56022      56022          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
- Hits          34799      34797     -2   
- Misses        21223      21225     +2   
  Partials          0          0          

Powered by Codecov. Last update 159f36e...70afe12


tick_label_axis = self.yaxis
tick_label_position = bottom
else:
raise ValueError('invalid orientation: %s' % orientation)

if len(height) == 1:
height *= nbars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is leaving in place an old bug--probably harmless, since no one seems to have tripped over it. The potential problem is that if a kwarg like 'height' is passed in as an ndarray of length 1, it will arrive here and have its value multiplied by nbars, which might not be 1.

_make_iterable could be modified to ensure that if an argument comes in with length 1, it is returned as a list; or it could be turned into a _to_list, and turn any argument into a list. The point is that in at least parts of the code the returned object is assumed to be a list, not just an iterable.

I think the _to_list approach is the way to go; for these variables, there is no advantage in leaving their type as an indeterminate 'iterable'.

@anntzer anntzer mentioned this pull request Jan 8, 2017
@anntzer
Copy link
Contributor
anntzer commented Jan 8, 2017

Now also addressed by #7562 (#7562 (comment)), which also covers the issue mentioned by @efiring.

@dstansby
Copy link
Member Author
dstansby commented Jan 9, 2017

Great, if/when that gets merged feel free to close this PR.

@NelleV
Copy link
Member
NelleV commented Jan 13, 2017

I am sorry… I don't want to have to review #7562.
@anntzer Do you mean that your patch supersedes this one?

@anntzer
Copy link
Contributor
anntzer commented Jan 13, 2017

Actually I noticed that my PR doesn't include the docstring update, so you can merge this one and let my PR just overwrite the fix with another fix...

@tacaswell
Copy link
Member

To check I understand:

  • this adds a small enhancement + docstrings and should be merged as-is
  • Cleanup: broadcasting #7562 does a bunch of other clean up + adds the same enhancement in a slightly different way, @anntzer is ok cleaning up any re-base related issues other

@anntzer
Copy link
Contributor
anntzer commented Jan 16, 2017

Yes on my side.

@tacaswell tacaswell merged commit 9187e3f into matplotlib:master Jan 16, 2017
@QuLogic QuLogic changed the title [MRG+1] Allow scalar height for plt.bar Allow scalar height for plt.bar Jan 16, 2017
@dstansby dstansby deleted the bar-error-message branch January 22, 2017 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0