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

X axis weirdness with DataFrame.plot(kind='bar') #11465

Open
colinhiggins opened this issue Oct 29, 2015 · 7 comments · Fixed by #28733
Open

X axis weirdness with DataFrame.plot(kind='bar') #11465

colinhiggins opened this issue Oct 29, 2015 · 7 comments · Fixed by #28733
Labels

Comments

@colinhiggins
Copy link

I encountered this issue when plotting some vertical axes spans on the same axes as a bar plot. Some example code is below to reproduce the issue. When using either scatter or line plots, the spans show up where they are supposed to, but the bar plot is off.

Digging a little deeper, I found that the plot call is setting the xticks to a zero-indexed array with a step size of one while setting the tick labels to the correct values.
If you specify the x ticks in the call to the bar plot, the spans are plotted at the correct position, but the bars are plotted from the same 0-indexed, singly incrementing array.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

spans = pd.DataFrame(np.array([[10,15]]), columns=['left','right'])
datdf = pd.DataFrame(zip(np.random.rand(15),np.random.rand(15)/10), columns=['foo','bar'], index=np.arange(5,20))

fig, axes = plt.subplots(nrows=3, ncols=1)

for a, ax in enumerate(axes):
    for i, row in spans.iterrows():
        ax.axvspan(row['left'],row['right'],color=(0,0,0,.5),zorder=0)


datdf.plot(ax=axes[0], kind='line') # all is good
datdf.plot(ax=axes[1], kind='bar', stacked=True) # span is wrong
datdf.plot(ax=axes[2], kind='bar', stacked=True, xticks=datdf.index.values) #bars are wrong

ticks = []
ticklabs = []

for a, ax in enumerate(axes):
    print a, 
    print zip(ax.xaxis.get_ticklocs(), [lab.get_text() for lab in ax.xaxis.get_ticklabels()])

image

axes 0
[(5.0, u''), (10.0, u''), (15.0, u''), (20.0, u''), (25.0, u''), (30.0, u''), (35.0, u'')]
axes 1
[(0, u'5'), (1, u'7'), (2, u'9'), (3, u'11'), (4, u'13'), (5, u'15'), (6, u'17'), (7, u'19'), (8, u'21'), (9, u'23'), (10, u'25'), (11, u'27'), (12, u'29'), (13, u'31'), (14, u'33')]
axes 2
[(5, u'5'), (7, u'7'), (9, u'9'), (11, u'11'), (13, u'13'), (15, u'15'), (17, u'17'), (19, u'19'), (21, u'21'), (23, u'23'), (25, u'25'), (27, u'27'), (29, u'29'), (31, u'31'), (33, u'33')]

@sinhrks
Copy link
Member

sinhrks commented Oct 29, 2015

Thanks for the report, might be related to #2980? PR is appreciated!

@tacaswell
Copy link
Contributor

I think both of these issues are related to #7612

@jorisvandenbossche
Copy link
Member

Closing as duplicate of #7612

@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Apr 6, 2017
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Apr 6, 2017
@jreback jreback modified the milestones: No action, Contributions Welcome Apr 30, 2019
@jreback
Copy link
Contributor

jreback commented Apr 30, 2019

re-opening as not fixed by #26185

@jreback jreback reopened this Apr 30, 2019
@jreback jreback removed the Duplicate Report Duplicate issue or pull request label Apr 30, 2019
nrebena added a commit to nrebena/pandas that referenced this issue May 26, 2019
Generate the tick position in BarPlot using convert tools from matlab.
Add test for issue pandas-dev#26186
Add test for issue pandas-dev#11465
@xgerrmann
Copy link

I am also running into the same problem. Only when kind='bar'.

@Gradecak
Copy link

I can confirm, running into the same issue. Any ideas on how to fix this?

nrebena added a commit to nrebena/pandas that referenced this issue Oct 1, 2019
nrebena added a commit to nrebena/pandas that referenced this issue Nov 19, 2019
nrebena added a commit to nrebena/pandas that referenced this issue Feb 12, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Feb 16, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Feb 16, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Mar 14, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Jun 13, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Jun 17, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Sep 12, 2020
charlesdong1991 pushed a commit that referenced this issue Nov 21, 2020
* TST: Test for issues #26186 and #11465

* BUG: Generate the tick position in BarPlot using convert tools from matlab.

Generate the tick position in BarPlot using convert tools from matlab.

* TST: Modify tests/plotting/test_frame.test_bar_categorical

Ticklocs are now float also for categorical bar data (as they are
position on the axis). The test is changed to compare to a array of
np.float.

* TST: Fix test for windows OS

* TST: Add test for plotting MultiIndex bar plot

A fix to issue #26186 revealed no tests existed about plotting a bar
plot for a MultiIndex, but a section of the user guide visualization
did. This section of the user guide is now in the test suite.

* BUG: Special case for MultiIndex bar plot

* DOC: Add whatsnew entry for PR #28733

* CLN: Clean up in code and doc

* CLN: Clean up test_bar_numeric

* DOC Move to whatsnew v1.1

* FIX: Make tick dtype int for backwards compatibility

* DOC: Improve whatsnew message

* ENH: Add UserWarning when plotting bar plot with MultiIndex

* CLN: Remove duplicate code line

* TST: Capture UserWarning for Bar plot with MultiIndex

* TST: Improve test explanation

* ENH: Raise UserWarning only if redrawing on existing axis with data

* DOC: Move to whatsnew v1.2.9

Co-authored-by: Marco Gorelli <m.e.gorelli@gmail.com>
@simonjayhawkins
Copy link
Member

PR that fixed this, #28733 has been reverted

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants