簡體   English   中英

matplotlib 散點圖 x 軸標簽

[英]matplotlib scatterplot x axis labels

當我將 c 選項添加到 matplotlib 中的散點圖時,x 軸標簽消失。 這是一個例子: https : //github.com/Kornel/scatterplot-matplotlib/blob/master/Scatter%20plot%20x%20axis%20labels.ipynb (歡迎拉請求:))

這是與筆記本中相同的示例:

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

這里存在 x 軸標簽

到:

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

在此處輸入圖片說明

x 軸標簽在哪里? 這是我缺少的功能嗎?

熊貓版本:0.18.1 Matplotlib:1.5.3 Python:3.5.2

感謝您的幫助,科內爾

編輯:@Kewl 指出的解決方案是調用 plt.subplots 並指定軸:

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

解決了

PS 看起來像是 jupyter 問題,在沒有 jupyter notebook 的情況下調用時標簽很好

這看起來像是一個大熊貓密謀給我的奇怪錯誤! 這是一種解決方法:

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

這將為您提供您期望的圖表:

在此處輸入圖片說明

如果使用的是蟒蛇,安裝pandasconda-forge渠道解決問題。

conda install -c conda-forge pandas

示例圖像

你可以這樣做。

plt.scatter(x="X", y="Y", c=df.color)
plt.xlabel("X axis label")
plt.ylabel("Y axis label")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM