matplotlib散点图x轴标签 [英] matplotlib scatterplot x axis labels

查看:228
本文介绍了matplotlib散点图x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向matplotlib中的散点图添加c选项时,x轴标签消失了.这是一个示例: https://github .com/Kornel/scatterplot-matplotlib/blob/master/Scatter%20plot%20x%20axis%20labels.ipynb (欢迎拉动请求:))

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

现在比较以下结果:

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

收件人:

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

x轴标签在哪里?这是我所缺少的功能吗?

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

熊猫版本:0.18.1 Matplotlib:1.5.3 的Python:3.5.2

Pandas version: 0.18.1 Matplotlib: 1.5.3 Python: 3.5.2

感谢您的帮助, 内核

Thanks for any help, Kornel

编辑: @Kewl指出的解决方案是调用plt.subplots并指定轴:

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

给予

P.S.看起来像是一个jupyter问题,如果没有使用jupyter笔记本,则在调用该标签时效果很好

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

推荐答案

这似乎是一个奇怪的错误,大熊猫在向我密谋!解决方法:

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:

这篇关于matplotlib散点图x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆