Skip to content

matplotlib x axis labels dissapear in scatterplot #2353

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
Kornel opened this issue Mar 30, 2017 · 11 comments
Closed

matplotlib x axis labels dissapear in scatterplot #2353

Kornel opened this issue Mar 30, 2017 · 11 comments

Comments

@Kornel
Copy link

Kornel commented Mar 30, 2017

Please take look at http://stackoverflow.com/questions/43121584/matplotlib-scatterplot-x-axis-labels or https://github.com/Kornel/scatterplot-matplotlib/blob/master/Scatter%20plot%20x%20axis%20labels.ipynb

When I create a scatter plot with the colours specified using c="C" the x axis labels disappear.

import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina'

test_df = pd.DataFrame({
        "X": [1, 2, 3, 4],
        "Y": [5, 4, 2, 1],
        "C": [1, 2, 3, 4]
    })

# This shows the x axis labels just fine
test_df.plot(kind="scatter", x="X", y="Y");

# This does not
test_df.plot(kind="scatter", x="X", y="Y", c="C");

The solution is to provide the axes explicitly:

fig, ax = plt.subplots()
test_df.plot(kind="scatter", x="X", y="Y", s=50, c="C", cmap="plasma", ax=ax);

This looks like a jupyter issue since calling this from "regular" python with savefig does not yield this problem.

@takluyver
Copy link
Member

Not sure how we could be causing that. Maybe it's a layout issue, and the labels are getting cut off the edge of the plot? @tacaswell, any ideas?

@tacaswell
Copy link

@Kornel Can you reproduce this without pandas? If not this should probably go to them.

@Kornel
Copy link
Author

Kornel commented Apr 4, 2017

Hm, it works using matplotlib as follows:

plt.scatter(x=test_df.X.values, y=test_df.Y.values, s=50);
plt.xlabel("X");

or

plt.scatter(x=test_df.X.values, y=test_df.Y.values, s=50, c=test_df.C.values);
plt.xlabel("X");

This would indicate that your guess is correct - a pandas issue.

However @tacaswell why does this:


import pandas as pd
import matplotlib.pyplot as plt

test_df = pd.DataFrame({
        "X": [1, 2, 3, 4],
        "Y": [5, 4, 2, 1],
        "C": [1, 2, 3, 4]
    })


test_df.plot(kind="scatter", x="X", y="Y", s=50);
plt.savefig("scatter-1.png")

test_df.plot(kind="scatter", x="X", y="Y", c="C");
plt.savefig("scatter-2.png")

test_df.plot(kind="scatter", x="X", y="Y", s=50, c="C", cmap="plasma");
plt.savefig("scatter-3.png")

work ? I can't explain that..

@tacaswell
Copy link

The inline backend also implicitly passes bbox_inches='tight' to the underlying savefig call, that may be related.

@Kornel
Copy link
Author

Kornel commented Apr 4, 2017

Doesn't look like that, please take a look at https://github.com/Kornel/scatterplot-matplotlib/blob/master/test.py and the resulting files, e.g. https://github.com/Kornel/scatterplot-matplotlib/blob/master/scatter-3.png All is fine..

I'm calling the script using the same conda env as with jupyter simply from the cmd line:
$ python test.py

@tacaswell
Copy link

I am having dejavu on this issue to another on involving hexbin...

@tacaswell
Copy link

I should google before I type: ipython/ipython#8653 which traces to pandas-dev/pandas#10678 which traces to pandas-dev/pandas#10611 which has a PR pandas-dev/pandas#12949

This can probably be closed as a pandas bug.

@takluyver
Copy link
Member

Thanks @tacaswell for the detective work!

@Kornel
Copy link
Author

Kornel commented Apr 6, 2017

@tacaswell well done :) thank you, however I don't quite get it why it works the way it works. Hope the pandas pr will fix it.

@minrk minrk added this to the Reference milestone Sep 13, 2018
@luvlogic
Copy link

luvlogic commented Sep 2, 2019

try this before you import pyplot, and it works fine.

%matplotlib notebook
import matplotlib.pyplot as plt1

@discdiver
Copy link

discdiver commented Sep 2, 2020

This problem is back as pandas 0.24.0 and 1.1.0. I submitted an issue to pandas here. Solutions that worked for me are in this SO thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants