Skip to content

Bar width expands between subsequent bars #15351

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

Closed
DavidMertz opened this issue Sep 30, 2019 · 2 comments
Closed

Bar width expands between subsequent bars #15351

DavidMertz opened this issue Sep 30, 2019 · 2 comments

Comments

@DavidMertz
Copy link

Bug report

Bug summary

In an non-obvious fashion, sometimes bars get wrong width. Specifically, each subsequent bar expands in width versus the prior one. However, it does this for some datasets but not for others, with no pattern obvious to me.

Code for reproduction

Simplified from my actual code, but this demonstrates problem.

print("Matplotlib version:", mpl.__version__)
print("\nWomen:\n", women)
print("\nMen:\n", men)

attr = 'weight'
fig, ax1 = plt.subplots()
ax2 = ax1.twinx() 
width = 9
ax2.bar(women[attr], women.total, width=0.75*width, alpha=0.15, label="Log Sales (W)")
ax2.bar(men[attr], men.total, width=width, alpha=0.15, label="Log Sales (M)")
ax2.grid(False)
ax2.set_yscale('log')

The datasets are these:

Women:
     weight  return_rate    total
0       90     0.163740     5240
1      100     0.193345   197812
2      110     0.180067   317877
3      120     0.169454  1048820
4      130     0.163293   760693
5      140     0.157449  1294401
6      150     0.154873   604145
7      160     0.149343   800071
8      170     0.147225   319150
9      180     0.146417   465691
10     190     0.146796   165134
11     200     0.150957   195493

Men:
     weight  return_rate   total
12      90     0.135135      37
13     100     0.120954    3522
14     110     0.117764    6547
15     120     0.128846   41833
16     130     0.103845   38750
17     140     0.110603  134761
18     150     0.107671   93628
19     160     0.111633  215824
20     170     0.112077  109800
21     180     0.102720  231551
22     190     0.104230   89821
23     200     0.098818  132213

Actual outcome

Under Matplotlib 3.1.1 I get this distorted effect:

mpl-incorrect-bars-3 1

It appears that each subsequent bar is Nx wider. I.e. first bar is fine, next is 2x width, next is 3x width, etc.

Even stranger, the distortion applies only to the "Men" bars. This is true if I change the order they are rendered in, and also is true if I comment out one or the other set of bars. I.e. women are fine, whether displayed solely or in combo; men are broken whether or not the women bars are displayed.

Expected outcome

Under Matplotlib 3.0.3, the output looks as desired. Specifically, the two sets of bars overlay each other with one narrower to aid reading both sets.

mpl-correct-bars-3 0

Matplotlib version

  • Operating system: Linux popbmi 5.0.0-28-generic Overhaul make.osx in master #30-Ubuntu SMP Wed Sep 4 11:47:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • Matplotlib version: 3.1.1
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: '3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) \n[GCC 7.3.0]'
  • Jupyter version (if applicable): 1.0.0
  • Other libraries:

Conda installs.

@ImportanceOfBeingErnest
Copy link
Member

Thanks for reporting this. This issue has already been reported in #15162. It's been fixed by #15166 and will hence be available once matplotlib 3.1.2 is released.

@ImportanceOfBeingErnest
Copy link
Member

Forgot to say: You can for now easily work around this by making sure to use a zero indexed dataframe

men = men.reset_index()

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

2 participants