Notebook: matplotlib x axis labels dissapear in scatterplot

Created on 30 Mar 2017  路  11Comments  路  Source: jupyter/notebook

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.

Most helpful comment

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

%matplotlib notebook
import matplotlib.pyplot as plt1

All 11 comments

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?

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

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..

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

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

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

I should google before I type: https://github.com/ipython/ipython/issues/8653 which traces to pandas-dev/pandas#10678 which traces to https://github.com/pandas-dev/pandas/issues/10611 which has a PR https://github.com/pandas-dev/pandas/pull/12949

This can probably be closed as a pandas bug.

Thanks @tacaswell for the detective work!

@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.

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

%matplotlib notebook
import matplotlib.pyplot as plt1

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.

Was this page helpful?
0 / 5 - 0 ratings