欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python實現(xiàn)嵌套繪圖并為條形圖添加自定義標注

 更新時間:2024年02月27日 08:36:06   作者:geoli91  
論文繪圖時經(jīng)常需要多圖嵌套,正好最近繪圖用到了,所以這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)嵌套繪圖并為條形圖添加自定義標注,感興趣的可以了解下

論文繪圖時經(jīng)常需要多圖嵌套,正好最近繪圖用到了,記錄一下使用Python實現(xiàn)多圖嵌套的過程。

首先,實現(xiàn) Seaborn 分別繪制折線圖和柱狀圖。

'''繪制折線圖'''
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", "use_inf_as_na")
 
# 獲取繪圖數(shù)據(jù)
df_fmri=sns.load_dataset("fmri")
# 繪制折線圖
sns.lineplot(data=df_fmri, x="timepoint", y="signal", hue="event")
 
# 創(chuàng)建繪圖數(shù)據(jù)
df_bar=df_fmri[['subject','signal']].groupby('subject',observed=True).agg('max').reset_index()
# 繪制條形圖
ax_bar=sns.barplot(
    data=df_bar,
    x="subject", y="signal",
    palette='Set2',
)

接下來實現(xiàn)條形圖與折線圖的嵌套,核心是使用 inset_axes 函數(shù)創(chuàng)建一個新的軸,然后再繪制第二個圖時指定繪圖的軸為剛才新建的軸。

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pyplot as plt
 
# 獲取繪圖數(shù)據(jù)
df_fmri = sns.load_dataset("fmri")
df_bar=df_fmri[['subject','signal']].groupby('subject',observed=True).agg('max').reset_index()
 
# 繪制折線圖
ax=sns.lineplot(data=df_fmri, x="timepoint", y="signal", hue="event")
plt.legend(loc='upper left')
 
# 使用 inset_axes 函數(shù)添加一個軸,用來顯示條形圖
ax_bar = inset_axes(
    ax, # 父軸
    width='40%', height='50%', # 新軸相對于父軸的長寬比例
    loc='lower left', # 新軸的錨點相對于父軸的位置
    bbox_to_anchor=(0.55,0.45,1,1), # 新軸的bbox
    bbox_transform=ax.transAxes # bbox_to_anchor 的坐標基準
    )
# 繪制條形圖
ax_bar=sns.barplot(
    data=df_bar,
    x="subject", y="signal", 
    palette='Set2',
    ax=ax_bar
)

可以看到,右上角的條形圖顯得很擁擠,x軸標注相互重疊比較嚴重,因此,考慮將條形圖由縱向變?yōu)闄M向,在 Seaborn 繪圖時交換 x 軸和 y 軸就能實現(xiàn)。此外,bar上方的空間也比較大,考慮將x軸的標注標注到bar上方,以進一步節(jié)約空間。bar的標注可以通過 ax.bar_label() 函數(shù)實現(xiàn),該函數(shù)不僅可以直接標注每個bar的數(shù)值,也可以自定義要標注的內(nèi)容和格式。修改后的代碼和結果圖如下:

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pyplot as plt
 
# 準備數(shù)據(jù)
df_fmri = sns.load_dataset("fmri")
df_bar=df_fmri[['subject','signal']].groupby('subject',observed=True).agg('max').reset_index()
 
# 繪制折線圖
ax=sns.lineplot(data=df_fmri, x="timepoint", y="signal", hue="event")
plt.legend(loc='upper left')
 
# 使用 inset_axes 函數(shù)添加一個軸,用來顯示條形圖
ax_bar = inset_axes(
    ax, # 父軸
    width='47%', height='52%', # 新軸相對于父軸的長寬比例
    loc='lower left', # 新軸的錨點相對于父軸的位置
    bbox_to_anchor=(0.5,0.44,1,1), # 新軸的bbox
    bbox_transform=ax.transAxes # bbox_to_anchor 的坐標基準
    )
# 繪制條形圖
ax_bar=sns.barplot(
    data=df_bar,
    # 交換 x 軸和 y 軸列名實現(xiàn)橫向條形圖
    x="signal", y="subject", 
    palette='Set2',
    ax=ax_bar
)
# 使用 sns 的 bar_label 函數(shù)為條形圖添加標注
ax_bar.bar_label(
    ax_bar.containers[0], # 條形圖的 BarContainer 對象
    labels=df_bar['subject'], # 要標注的labels,默認為 bar 的數(shù)值,此處傳入自定義的label序列
    label_type='edge', # 標注顯示的位置,可選 edge 或 center
    padding=2, # 標注與bar之間的距離
    # fmt='%.2f' # 標注格式化字符串
    fontsize=10 # 設置標注的字體大小
    )
# 為了避免標注超出繪圖范圍,將x軸的繪圖范圍擴大
plt.xlim(0,0.62)
# 隱藏左側y軸
ax_bar.yaxis.set_visible(False)
# 去除多余的軸線
sns.despine()

打完收工!

到此這篇關于使用Python實現(xiàn)嵌套繪圖并為條形圖添加自定義標注的文章就介紹到這了,更多相關Python嵌套繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論