簡體   English   中英

Matplotlib有多個Y軸,xlabels消失了嗎?

[英]Matplotlib multiple Y-axes, xlabels disappear?

我正在使用Python 3.x使用matplotlib生成箱圖。

一般:我有一個數據框,我想在框圖中創建的內容。 問題是,列之間的比例不一致; l1和l2需要在l3:l5的不同Y軸上繪制。 第一列包含17列,另外兩列各有1列。

l1 = [32107.0,20490.0, 32107.0, 22134.0,31564.0, 32107.0, 22134.0, 20732.0, 20490.0, 28406.0]

l2 = [54.4, 40.2, 54.4, 41.1, 49.4, 54.4, 41.1, 37.0, 40.2, 34.2]

l3 = [4595.0, 2164.0, 4595.0, 3500.0, 3733.0, 4595.0, 3500.0, 3214.0, 2164.0, 3388.0]

l4 = [4868.0, 2289.0, 4868.0, 3652.0, 4128.0, 4868.0, 3652.0, 3418.0, 2289.0, 3980.0]

l5 = [3777.0, 1623.0, 3777.0, 2456.0, 3010.0, 3777.0, 2456.0, 2318.0, 1623.0, 2677.0]

df2 = pd.DataFrame(
    {'l1' : l1,
     'l2' : l2,
     'l3': l3,
     'l4' : l4,
     'l5' : l5})
fig, ax = plt.subplots(figsize=(5, 3))

ax.boxplot(df2[['l1']].transpose())
ax2 = ax.twinx()
ax2.boxplot(df2['l2'], positions = [2])
ax3 = ax.twinx()
ax3.boxplot(df2['l3'], positions = [3])
ax.set_xlim(0, 7)

它不漂亮,但它也不是我的完整數據,但結果是一致的; 前兩列的標簽已經消失,只剩下最后一列。

我已經嘗試了set_xticks()和其他幾個調用,我無法到達任何地方。

我知道如何抵消y軸和所有這些; 我基本上掛了標簽的東西。

試試這個來設置你的xaxis刻度:

l1 = [32107.0,20490.0, 32107.0, 22134.0,31564.0, 32107.0, 22134.0, 20732.0, 20490.0, 28406.0]

l2 = [54.4, 40.2, 54.4, 41.1, 49.4, 54.4, 41.1, 37.0, 40.2, 34.2]

l3 = [4595.0, 2164.0, 4595.0, 3500.0, 3733.0, 4595.0, 3500.0, 3214.0, 2164.0, 3388.0]

l4 = [4868.0, 2289.0, 4868.0, 3652.0, 4128.0, 4868.0, 3652.0, 3418.0, 2289.0, 3980.0]

l5 = [3777.0, 1623.0, 3777.0, 2456.0, 3010.0, 3777.0, 2456.0, 2318.0, 1623.0, 2677.0]

df2 = pd.DataFrame(
    {'l1' : l1,
     'l2' : l2,
     'l3': l3,
     'l4' : l4,
     'l5' : l5})
fig, ax = plt.subplots(figsize=(5, 3))

ax.boxplot(df2[['l1']].transpose())
ax2 = ax.twinx()
ax2.boxplot(df2['l2'], positions = [2])
ax3 = ax.twinx()
# ax3.boxplot(df2[['l3','l4','l5']], positions = [3,4,5])
df2[['l3','l4','l5']].plot.box(positions=[3,4,5], ax=ax3)
ax.set_xlim(0, 7)
_ = ax3.xaxis.set_ticks([1,2,3,4,5])
_ = ax3.xaxis.set_ticklabels(['l1','l2','l3','l4','l5'])

輸出:

在此輸入圖像描述

暫無
暫無

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

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