Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - 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 (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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...