Python嵌套绘图并为条形图添加自定义标注

论文绘图时经常需要多图嵌套,正好最近绘图用到了,记录一下使用Python实现多图嵌套的过程。

首先,实现 Seaborn 分别绘制折线图和柱状图。

|---|--------------------------------------------------------------------------------------------------|
| | '''绘制折线图''' |
| | import seaborn as sns |
| | import matplotlib.pyplot as plt |
| | import warnings |
| | warnings.filterwarnings("ignore", "use_inf_as_na") |
| | |
| | # 获取绘图数据 |
| | df_fmri=sns.load_dataset("fmri") |
| | # 绘制折线图 |
| | sns.lineplot(data=df_fmri, x="timepoint", y="signal", hue="event") |
| | |
| | # 创建绘图数据 |
| | 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', |
| | ) |

接下来实现条形图与折线图的嵌套,核心是使用 inset_axes 函数创建一个新的轴,然后再绘制第二个图时指定绘图的轴为刚才新建的轴。

|---|--------------------------------------------------------------------------------------------------|
| | from mpl_toolkits.axes_grid1.inset_locator import inset_axes |
| | import matplotlib.pyplot as plt |
| | |
| | # 获取绘图数据 |
| | 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 函数添加一个轴,用来显示条形图 |
| | 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轴标注相互重叠比较严重,因此,考虑将条形图由纵向变为横向,在 Seaborn 绘图时交换 x 轴和 y 轴就能实现。此外,bar上方的空间也比较大,考虑将x轴的标注标注到bar上方,以进一步节约空间。bar的标注可以通过 ax.bar_label() 函数实现,该函数不仅可以直接标注每个bar的数值,也可以自定义要标注的内容和格式。修改后的代码和结果图如下:

|---|--------------------------------------------------------------------------------------------------|
| | from mpl_toolkits.axes_grid1.inset_locator import inset_axes |
| | import matplotlib.pyplot as plt |
| | |
| | # 准备数据 |
| | 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 函数添加一个轴,用来显示条形图 |
| | 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 轴列名实现横向条形图 |
| | x="signal", y="subject", |
| | palette='Set2', |
| | ax=ax_bar |
| | ) |
| | # 使用 sns 的 bar_label 函数为条形图添加标注 |
| | ax_bar.bar_label( |
| | ax_bar.containers[0], # 条形图的 BarContainer 对象 |
| | labels=df_bar['subject'], # 要标注的labels,默认为 bar 的数值,此处传入自定义的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() |

相关推荐
waterHBO36 分钟前
python 爬虫 selenium 笔记
爬虫·python·selenium
limingade2 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal7 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali7 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ7 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.8 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python