CatInABox
CatInABox

Reputation: 497

matplotlib scatterplot x axis labels

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

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

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

Upvotes: 23

Views: 29192

Answers (3)

ASH
ASH

Reputation: 20302

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")

Upvotes: 1

c0degeas
c0degeas

Reputation: 832

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

conda install -c conda-forge pandas

Example Image

Upvotes: 0

Kewl
Kewl

Reputation: 3417

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

Upvotes: 25

Related Questions