#!/usr/bin/env python # coding: utf-8 # In[1]: import sys sys.path.append('../') import pandas as pd import numpy as np import forestplot as fp pd.set_option('display.max_rows', 100) df = (pd.read_excel('../examples/data/seg-multmodel-group0.xls') .reset_index(drop=True) .query("group!='poi'") ) df.head(3) # In[2]: std_opts = dict( dataframe = df, estimate = "r", ll ="ll", hl = "hl", varlabel = "var", model_col = "model", ) # ## Normie usage # In[3]: # Vanilla _df, ax = fp.mforestplot(**std_opts) # In[4]: # Vanilla # Color rows and add borders _df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, despine=False) # ## Make table # In[5]: # Vanilla # Color rows # Add annotation and make it a table # Add right annotations _df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, annote=["group"], annoteheaders=["Group"], rightannote=["source"], right_annoteheaders=["Data source"], table=True) # In[6]: # With groups _df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, groupvar="group", rightannote=["source"], right_annoteheaders=["Data source"], variable_header="Covariate", table=True) # In[7]: # With groups and group ordering _df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, groupvar="group", group_order=["census", "fnb", "age"], rightannote=["source"], right_annoteheaders=["Data source"], variable_header="Covariate", capitalize=None, table=True) # In[8]: # With groups and group ordering # Capitalize _df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, groupvar="group", group_order=["census", "fnb", "age"], rightannote=["source"], right_annoteheaders=["Data source"], variable_header="Covariate", capitalize="capitalize", table=True) # In[9]: # Add groups _df, ax = fp.mforestplot(**std_opts, groupvar='group', return_df=True, ) # In[10]: # Add groups # With border + color alternate rows _df, ax = fp.mforestplot(**std_opts, groupvar='group', despine=False, color_alt_rows=True, return_df=True, ) # In[11]: # Add groups # Add annotation on right _df, ax = fp.mforestplot(**std_opts, groupvar='group', rightannote=['source'], right_annoteheaders=['Data Source'], return_df=True, ) # In[12]: # No groups # Add annotation on right _df, ax = fp.mforestplot(**std_opts, rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], return_df=True, ) # In[13]: # No groups # Add annotation on right # No flush _df, ax = fp.mforestplot(**std_opts, rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], flush=False, return_df=True, ) # In[14]: # No groups # Add annotation on right # Add row colors # No flush _df, ax = fp.mforestplot(**std_opts, rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], flush=False, color_alt_rows=True, return_df=True, ) # In[15]: # W/ groups # Add annotation on right # Add row colors # No flush _df, ax = fp.mforestplot(**std_opts, groupvar='group', rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], flush=False, color_alt_rows=True, return_df=True, ) # ## Add more annotations on left # In[16]: # No groups _df, ax = fp.mforestplot(**std_opts, annote=['group','source'], annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], return_df=True, ) # In[17]: # Add group _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], return_df=True, ) # In[18]: # Add group # Color row _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, ) # In[19]: # Add group # Keep borders _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, **{'despine':False, "figsize":(4,8)} ) # ## Legends # In[20]: # Add group # Color row # Change legend label _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], modellabels=["Model 1", "Model 2"], annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, ) # In[21]: # Add group # Color row # Change legend label # Add x-label _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], modellabels=["Model 1", "Model 2"], xlabel="X-label", annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, ) # In[22]: # Add group # Color row # Change legend label # Add x-label _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], modellabels=["Model 1", "Model 2"], xlabel="X-label", annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, bbox_to_anchor=(.5,.95) ) # In[23]: # Add group # Color row # Change legend label # Add x-label _df, ax = fp.mforestplot(**std_opts, groupvar='group', annote=['group','source'], modellabels=["Model 1", "Model 2"], xlabel="X-label", annoteheaders=['Group', 'Data Source'], rightannote=['group','source'], right_annoteheaders=['Group', 'Data Source'], color_alt_rows=True, return_df=True, # bbox_to_anchor=None, ) # In[24]: import forestplot as fp df = fp.load_data("sleep") # companion example data df.head(3) # In[32]: 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 xline=.15 ) # In[30]: ax = 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 ) ax.axvline(.15) # In[ ]: