22

When I'm adding the c option to a scatterplot in matplotlib, the x axis labels dissapear. Here's an example: https://github.com/Kornel/scatterplot-matplotlib/blob/master/Scatter%20plot%20x%20axis%20labels.ipynb (pull requests are welcome:))

Here's the same example as in the notebook:

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]
    })

Now compare the result of:

test_df.plot(kind="scatter", x="X", y="Y", s=50);

here the x axis labels are present

To:

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

enter image description here

Where are the x axis labels? Is this a feature I'm missing?

Pandas version: 0.18.1 Matplotlib: 1.5.3 Python: 3.5.2

Thanks for any help, Kornel

EDIT: The solution as pointed out by @Kewl is to call plt.subplots and specify the axes:

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

gives

solved

P.S. It looks like a jupyter issue, the label is fine when called without a jupyter notebook

CatInABox
  • 387
  • 2
  • 4
  • 12
  • Just to say that I tried to run the same code from a python shell lanched from the anaconda console and I am able to see the "X" label. pd.__version__: 0.19.2 matplotlib.__version__: 2.0.0 Python 3.6.0. Maybe it's just the version of one or more of these packages? If I had the time (or if I were in you), I would try to test it with a new conda (or virtual) env to see if this fixes the problem... – umbe1987 Mar 30 '17 at 15:35
  • 1
    Also: have you tried running your code only as a notebook (with Jupyter)? If so, I would test it without a notebook, to see if it's not just a matter of the "X" axis not showing up because it has no sufficient space in the notebook frame (just a guess). – umbe1987 Mar 30 '17 at 15:40
  • 1
    @umbe1987 You're right! It works outside of jupyter. I've tried calling tight_layout but that did not change anything :/ Looks like a jupyter bug to me. – CatInABox Mar 30 '17 at 18:39
  • For me, installing pandas with `conda-forge` channel worked. `conda install -c conda-forge pandas` – c0degeas Apr 01 '19 at 22:06

3 Answers3

25

That looks like a strange bug with pandas plotting to me! Here's a way around it:

fig, ax = plt.subplots()
df.plot(kind='scatter',x='X', y='Y', c='C', ax=ax)
ax.set_xlabel("X")
plt.show()

This will give you the graph you expect:

enter image description here

Kewl
  • 3,327
  • 5
  • 26
  • 45
  • Thank you so much! You know what's funny? set_xlabel is redundat. Calling plt.subplots does the trick. Please see the updated notebook. – CatInABox Mar 30 '17 at 18:45
1

You can do it like this.

plt.scatter(x="X", y="Y", c=df.color)
plt.xlabel("X axis label")
plt.ylabel("Y axis label")
ASH
  • 20,759
  • 19
  • 87
  • 200
0

If you are using Anaconda, installing pandas with conda-forge channel resolves the issue.

conda install -c conda-forge pandas

Example Image

c0degeas
  • 762
  • 9
  • 19