pandas matplotlib 散点图 x 轴标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43121584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:18:29  来源:igfitidea点击:

matplotlib scatterplot x axis labels

pythonpandasmatplotlib

提问by Kornel

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

当我将 c 选项添加到 matplotlib 中的散点图时,x 轴标签消失。这是一个例子: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

这里存在 x 轴标签

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?

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

Pandas version: 0.18.1 Matplotlib: 1.5.3 Python: 3.5.2

Pandas版本:0.18.1 Matplotlib:1.5.3 Python:3.5.2

Thanks for any help, Kornel

感谢您的帮助,科内尔

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

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

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

PS 看起来像是 jupyter 问题,在没有 jupyter notebook 的情况下调用时标签很好

回答by Kewl

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

这看起来像是一个大Pandas密谋给我的奇怪错误!这是一种解决方法:

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

在此处输入图片说明

回答by c0degeas

If you are using Anaconda, installing pandaswith conda-forgechannel resolves the issue.

如果您使用的是 Anaconda,则pandas通过conda-forge频道安装可以解决该问题。

conda install -c conda-forge pandas

Example Image

示例图像