簡體   English   中英

如何調整xlim,增加每個圖形之間的距離以及為seaborn的“山脊圖”設置標題

[英]How to adjust xlim, increase the distance between each graph and set a title for the 'ridge plot' from seaborn

我想基於seaborn的“山脊圖”示例( https://seaborn.pydata.org/examples/kde_ridgeplot.html )執行以下操作:

  1. 調整xlim,以便在錯誤標題(例如“ Error2-abc ...”)和圖形本身之間創建更大的距離
  2. 增加每個圖形之間的距離
  3. 為整個情節設置標題

我嘗試了sns.plt.xlim(-10, 3) ,這導致了錯誤。 這是我的整個代碼:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})


errorNames = ['Error 1 - abc.....',
              'Error 2 - abc.....',
              'Error 3 - abc.....',
              'Error 4 - abc.....',
              'Error 5 - abc.....',
              'Error 6 - abc.....',
              'Error 7 - abc.....',
              'Error 8 - abc.....',
              'Error 9 - abc.....',
              'Error 10 - abc.....',
              'Error 11 - abc.....',
              'Error 12 - abc.....',
              'Error 13 - abc.....']


# Create the data
rs = np.random.RandomState(1979)
x = rs.randn(650)
#g = np.tile(list("ABCDEFGHIJKLM"), 50)
g = np.tile(list(errorNames), 50)

df = pd.DataFrame(dict(x=x, g=g))
#m = df.g.map(ord)
#df["x"] += m

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


# sns.plt.xlim(-10, 3)


# 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 
# Erst hier wird geplotted
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)

對於1),您可以在刪除yticks的同一調用g.set更改x限制:

g.set(yticks=[], xlim=[-5, 3])

對於2),您可以僅在subplots_adjust調整hspace

對於3),您可以為圖形創建suptitle

g.fig.suptitle("Some title")

這給出了類似的內容:

在此處輸入圖片說明

以下是解決所有這三點的一種方法:

  • g.set(xlim=(-4, 3))設置x限制
  • ax.text(0, .25,...) 0.25而不是0.2的更高偏移量可以正確定位錯誤名稱。
  • plt.suptitle('Main title')在圖的頂部放置一個主標題。

g.set(xlim=(-4, 3))

def label(x, color, label):
    ax = plt.gca()
    ax.text(0, .25, label, fontweight="bold", color=color,
            ha="left", va="center", transform=ax.transAxes)

g.map(label, "x")
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.suptitle('Main title')

在此處輸入圖片說明

暫無
暫無

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

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