亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用數據框中的 matplotlib 并排繪制多個箱線圖

使用數據框中的 matplotlib 并排繪制多個箱線圖

一只名叫tom的貓 2022-12-27 16:36:57
我正在嘗試從一個數據框中并排繪制 60 多個箱線圖,我想知道是否有人可以提出一些可能的解決方案。目前我有df_new一個包含 66 列的數據框,我用它來繪制箱線圖。我發現繪制箱線圖的最簡單方法是使用 pandas 中的箱線圖包:boxplot = df_new.boxplot(column=x, figsize = (100,50))這給了我一個非常非常小的圖表,軸難以辨認,我似乎無法更改字體大小,所以我試圖在 matplotlib 中本地執行此操作,但我想不出一種有效的方法。我試圖避免使用類似以下內容創建 66 個單獨的箱線圖:fig, ax = plt.subplots(nrows = 1,                        ncols = 66,                         figsize = (10,5),                        sharex = True) ax[0,0].boxplot(#insert parameters here)我實際上不知道如何將數據從 df_new.describe() 獲取到箱線圖函數中,所以任何關于此的提示將不勝感激!文檔令人困惑。不確定 x 向量應該是什么。理想情況下,我只想為箱線圖函數提供數據框,并讓它通過動態計算所有四分位數、列分隔等來自動創建所有箱線圖——這甚至可能嗎?
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

我嘗試將 the 替換為boxplotridge plot,它占用的空間更少,因為:

  1. 它需要寬度的一半

  2. 你可以部分重疊山脊

  3. 它是垂直發展的,所以你可以向下滾動所有的情節

我從seaborn 文檔中獲取代碼并對其進行了一些調整,以便擁有 60 個不同的正態分布的脊;這里的代碼:

import numpy as np

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

import itertools

sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})


# # Create the data

n = 20

x = list(np.random.randn(1, 60)[0])

g = [item[0] + item[1] for item in list(itertools.product(list('ABCDEFGHIJ'), list('123456')))]

df = pd.DataFrame({'x': n*x,

                   'g': n*g})


# Initialize the FacetGrid object

pal = sns.cubehelix_palette(10, rot=-.25, light=.7)

g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)


# Draw the densities in a few steps

g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)

g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)

g.map(plt.axhline, y=0, lw=2, clip_on=False)



# Define and use a simple function to label the plot in axes coordinates

def label(x, color, label):

    ax = plt.gca()

    ax.text(0, .2, label, fontweight="bold", color=color,

            ha="left", va="center", transform=ax.transAxes)



g.map(label, "x")


# Set the subplots to overlap

g.fig.subplots_adjust(hspace=-.25)


# Remove axes details that don't play well with overlap

g.set_titles("")

g.set(yticks=[])

g.despine(bottom=True, left=True)


plt.show()

這是我得到的結果:


http://img1.sycdn.imooc.com//63aaaedd0001ca9a03151288.jpg

我不知道它是否適合您的需求,無論如何請記住,將如此多的發行版彼此相鄰放置總是需要大量空間(和非常大的屏幕)。也許您可以嘗試將分布分成更小的組并一次繪制一點?



查看完整回答
反對 回復 2022-12-27
  • 1 回答
  • 0 關注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號