簡體   English   中英

無法顯示seaborn distplot

[英]Can't display the seaborn distplot

我正在嘗試使用Pythonseaborn模塊制作直方圖和密度圖,並且在繪圖上我還嘗試在模式處繪制一條垂直線。 但是,生成的圖不會顯示任何直方圖和/或密度曲線。 我的代碼如下:

# a function to compute mode of the histograms shown in Figures `2` and `3` in the paper.
def compute_mode(layer_list):
    
    ax = sns.distplot(layer_list, hist=False, kde=True, kde_kws={'linewidth': 2})
    x = ax.lines[0].get_xdata()
    y = ax.lines[0].get_ydata()
    mode_idx = y.argmax()
    mode_x = x[mode_idx]
    plt.close()
        
    return mode_x

# function to plot the histogram of the layer lists.
def make_density(layer_list, color):

    
    # Plot formatting
    plt.xlabel('Median Stn. MC-Loss')
    plt.ylabel('Density')

    
    # Draw the histogram and fit a density plot.
    sns.distplot(layer_list, hist = True, kde = True,
                 kde_kws = {'linewidth': 2}, color=color)
    
    # compute mode of the histogram.
    mode_x = compute_mode(layer_list)
    
    # draw a vertical line at the mode of the histogram.
    plt.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)
    plt.text(mode_x, 0.16, 'mode: {:.4f}'.format(mode_x))

layer_list = [ 1.0,2.0,3.0,4.0,2.0,3.0,1.0,6.0,10.0,2.0]
make_density(layer_list, 'green')

在此處輸入圖片說明 我認為問題出自plt.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)plt.text(mode_x, 0.16, 'mode: {:.4f}'.format(mode_x))

我在這里做錯了什么?

謝謝,

主要問題是在compute_mode() plt.close() ,它關閉了之前在make_density()創建的make_density() 請注意, sns.distplot主要是一個繪圖函數,不應僅用於計算。 由於 kde 已經在make_density()繪制, make_density() ax.lines[0]可以傳遞給compue_mode()以提取曲線數據,而無需再次創建繪圖。

其他一些說明:

  • 在 seaborn 0.11 中, distplot已被棄用,取而代之的是兩個函數: histplot創建直方圖,可選擇使用 kde。 displot (沒有“T”),它創建了一個直方圖/kdeplots 網格。 除了名稱混亂, kde_kws的參數distplot被稱為line_kwshistplot ,而kde_kwshistplot是用於那些計算KDE功能。 此外,kde 曲線始終使用與直方圖相同的顏色。
  • Seaborn的“軸水平”函數返回的ax ,可用於其他格式。 在您的原始代碼中,您在調用distplot之前添加了一些標簽,但由於distplot也可能會更改設置,因此之后進行所有這些格式化調用會更安全。
  • 您可能想了解面向對象的接口以及ax.set_xlabel()plt.xlabel()之間的區別。
import matplotlib.pyplot as plt
import seaborn as sns

# a function to compute mode of the histograms shown in Figures `2` and `3` in the paper.
def compute_mode(line_object):
    x = line_object.get_xdata()
    y = line_object.get_ydata()
    mode_idx = y.argmax()
    return x[mode_idx], y[mode_idx]

# function to plot the histogram of the layer lists.
def make_density(layer_list, color):
    # Draw the histogram and fit a density plot.
    ax = sns.histplot(layer_list, kde=True,
                      line_kws={'linewidth': 2}, color=color)
    # compute mode of the histogram.
    mode_x, mode_y = compute_mode(ax.lines[0])

    # draw a vertical line at the mode of the histogram.
    ax.axvline(mode_x, color='blue', linestyle='dashed', linewidth=1.5)
    ax.text(mode_x, mode_y, 'mode: {:.4f}'.format(mode_x))
    # Plot formatting
    ax.set_xlabel('Median Stn. MC-Loss')
    ax.set_ylabel('Density')

layer_list = [1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 1.0, 6.0, 10.0, 2.0]

make_density(layer_list, 'green')
plt.show()

示例圖

在計算模式中調用 seaborn 的 distplot 而不指定與繪圖compute_mode的斧頭。 您可以簡單地用以下代碼替換compute_mode

def compute_mode(layer_list):
    dummy_fig, dummy_ax = plt.subplots()
    ax = sns.distplot(layer_list, hist=False, kde=True, kde_kws={'linewidth': 2}, ax=dummy_ax)
    x = ax.lines[0].get_xdata()
    y = ax.lines[0].get_ydata()
    mode_idx = y.argmax()
    mode_x = x[mode_idx]
    plt.close()

    return mode_x

即使此解決方法有效,請考慮使用專用工具(如scipy 的 gaussian_kde )計算模式。 它可以防止你搞亂圖形庫來做數學題。

暫無
暫無

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

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