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

Inconsitent index for plot #26186

Open
Jeanselme opened this issue Apr 22, 2019 · 3 comments · Fixed by #28733
Open

Inconsitent index for plot #26186

Jeanselme opened this issue Apr 22, 2019 · 3 comments · Fixed by #28733
Labels

Comments

@Jeanselme
Copy link

Code Sample

import pandas as pd
import matplotlib.pyplot as plt

data = {"A":0, "B":3, "C":-4}
df = pd.DataFrame.from_dict(data, orient = "index", columns = ["Value"])
ax = df.plot.barh()

df = df.sort_values("Value") * - 2
df.plot.barh(ax = ax, color = "red")
plt.show()

Problem description

The second plot overwrites the first index even if it is not in the same order, producing incoherent figure where the ylabel does not match with the data displayed.
The current output (pandas 0.24.1) creates
current
instead of
correct

I don't know if this is related to the way pandas handles index or matplotlib.

@gfyoung gfyoung added the Visualization plotting label Apr 23, 2019
@gfyoung
Copy link
Member

gfyoung commented Apr 23, 2019

cc @TomAugspurger

@nrebena
Copy link
Contributor

nrebena commented Apr 24, 2019

I dug a bit to see if it was pandas or matplotlib related, but it seems that it is correctly handled by matplotlib.
I reused the code and add the "pure matplotlib" version, and obtained the following figures.

data = {"A":0, "B":3, "C":-4}
df = pd.DataFrame.from_dict(data, orient = "index", columns = ["Value"])
ax = df.plot.barh()

df2 = df.sort_values("Value") * - 2
df2.plot.barh(ax = ax, color = "red")
plt.show()

fig = plt.figure()
plt.barh(df.index.values, df.Value.values)
ax = plt.gca()
ax.barh(df2.index.values, df2.Value.values)
plt.show()

Pandas plotting

Figure_1

Matplotlib

Figure_2

It seems that pandas.Dataframe.plot doesn't reuse the axes info the same way that matplotlib does.

nrebena added a commit to nrebena/pandas that referenced this issue May 1, 2019
nrebena added a commit to nrebena/pandas that referenced this issue May 1, 2019
nrebena pushed a commit to nrebena/pandas that referenced this issue May 4, 2019
Add test for issue pandas-dev#26186
Generate the tick position in BarPlot using convert tools from matlab.

Working solution
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
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 Oct 3, 2019
A fix to issue pandas-dev#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.
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 Nov 19, 2019
A fix to issue pandas-dev#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.
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 12, 2020
A fix to issue pandas-dev#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.
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
A fix to issue pandas-dev#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.
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
A fix to issue pandas-dev#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.
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 Mar 14, 2020
A fix to issue pandas-dev#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.
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 13, 2020
A fix to issue pandas-dev#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.
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 Jun 17, 2020
A fix to issue pandas-dev#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.
nrebena added a commit to nrebena/pandas that referenced this issue Sep 12, 2020
nrebena added a commit to nrebena/pandas that referenced this issue Sep 12, 2020
A fix to issue pandas-dev#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.
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

The PR that fixed this issue has been reverted.

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jan 18, 2021
@mroeschke mroeschke added the Bug label Jul 2, 2021
@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.

5 participants