前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >forestplot | Python出版级森林图绘制工具,推荐~~

forestplot | Python出版级森林图绘制工具,推荐~~

作者头像
DataCharm
发布2024-07-05 10:48:00
1140
发布2024-07-05 10:48:00
举报

前言

我们的数据可视化课程已经上线啦!!目前课程的主要方向是 科研、统计、地理相关的学术性图形绘制方法,后续也会增加商务插图、机器学等、数据分析等方面的课程。课程免费新增,这点绝对良心!

我们第一个数据可视化交流圈子也已经上线了,主要以我的第一本书籍《科研论文配图绘制指南-基于Python》为基础进行拓展,提供「课堂式」教学视频,还有更多拓展内容,可视化技巧远超书籍本身,书籍修正和新增都会分享到圈子里面~~

参与课程或者圈子的你将获取到:学员答疑、可视化资源分享、可视化技巧补充、可视化业务代做(学员和甲方对接)、副业交流、提升认知等等。

「forestplot」-Python轻松绘制森林图

在我的第一本书籍的学习圈子中,很多学员在反映书籍中绘制森林图(forest plots)的方法较为繁琐,有没有其他好用的绘制方法呢?

其实,在针对书籍中的很多内容,我们都在进行「迭代和更新」,不仅是因为书籍出版的延迟性导致代码版本较老,同时也是因为要加入很多新的内容。

今天,我们就同学提出的森林图绘制方法,介绍一个全新的绘制工具-「forestplot」

  • forestplot工具包介绍

forestplot软件包可让用户轻松制作出版级别的森林图。用户只需要提供一个数据框(DataFrame)(如电子表格),其中的行与变量/研究相对应,列包括估计值、变量标签、置信区间上下限,就可以绘制出好看的森林图啦。

当然,这样也更方便大家直接在使用pandas处理数据的结果,直接用于绘图使用。

此外,forestplot软件包还可通过其他选项,还可以在图中添加数据框中的列数值作为注释。

可通过如下方式进行快速安装:

代码语言:javascript
复制
pip install forestplot
#或者conda安装
conda install forestplot
  • forestplot包基础使用方法

首先,我们从官方导出需要绘图的样式数据集:

代码语言:javascript
复制
import forestplot as fp

df = fp.load_data("sleep")  # companion example data
df.head(3)

然后使用forestplot包中的forestplot()函数,并选择合适的变量进行即可,如下:

代码语言:javascript
复制
fp.forestplot(df,  # the dataframe with results data
              estimate="r",  # col containing estimated effect size 
              ll="ll", hl="hl",  # columns containing conf. int. lower and higher limits
              varlabel="label",  # column containing variable label
              ylabel="Confidence interval",  # y-label title
              xlabel="Pearson correlation",  # x-label title
              )

其中,每个绘图变量都解释非常清楚,我们以后在使用这个工具进行可视化绘制时,务必要保证绘图的数据集格式一致。可视化结果如下:

  • 定制化修改

如果我们需要对其分组变量、分组排序或者对评估值进行排序,可以通过如下脚本进行设置:

代码语言:javascript
复制
fp.forestplot(df,  # the dataframe with results data
              estimate="r",  # col containing estimated effect size 
              ll="ll", hl="hl",  # columns containing conf. int. lower and higher limits              
              varlabel="label",  # column containing variable label
              capitalize="capitalize",  # Capitalize labels
              groupvar="group",  # Add variable groupings 
              # group ordering
              group_order=["labor factors", "occupation", "age", "health factors", 
                           "family factors", "area of residence", "other factors"],
              sort=True  # sort in ascending order (sorts within group if group is specified)               
              )

如果需要添加P值并将备用行灰色,可通过如下脚本绘制:

代码语言:javascript
复制
fp.forestplot(df,  # the dataframe with results data
              estimate="r",  # col containing estimated effect size 
              ll="ll", hl="hl",  # columns containing conf. int. lower and higher limits
              varlabel="label",  # column containing variable label
              capitalize="capitalize",  # Capitalize labels
              groupvar="group",  # Add variable groupings 
              # group ordering
              group_order=["labor factors", "occupation", "age", "health factors", 
                           "family factors", "area of residence", "other factors"],
              sort=True,  # sort in ascending order (sorts within group if group is specified)               
              pval="p-val",  # Column of p-value to be reported on right
              color_alt_rows=True,  # Gray alternate rows
              ylabel="Est.(95% Conf. Int.)",  # ylabel to print
              **{"ylabel1_size": 11}  # control size of printed ylabel
              )

如果需要添加注释文本信息并表格化展示,可通过如下设置:

代码语言:javascript
复制
fp.forestplot(df,  # the dataframe with results data
              estimate="r",  # col containing estimated effect size 
              ll="ll", hl="hl",  # lower & higher limits of conf. int.
              varlabel="label",  # column containing the varlabels to be printed on far left
              capitalize="capitalize",  # Capitalize labels
              pval="p-val",  # column containing p-values to be formatted
              annote=["n", "power", "est_ci"],  # columns to report on left of plot
              annoteheaders=["N", "Power", "Est. (95% Conf. Int.)"],  # ^corresponding headers
              rightannote=["formatted_pval", "group"],  # columns to report on right of plot 
              right_annoteheaders=["P-value", "Variable group"],  # ^corresponding headers
              xlabel="Pearson correlation coefficient",  # x-label title
              table=True,  # Format as a table
              )

此外,还有更多的图形绘制结果样式,如下:

参考资料

[1]

forestplot包官网: https://forestplot.readthedocs.io/en/latest/。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-07-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 「forestplot」-Python轻松绘制森林图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档