myforestplot package¶
Submodules¶
myforestplot.cleaning_utils module¶
- myforestplot.cleaning_utils.add_pretty_risk_column(res, risk, lower, upper, fml='.2f', ref='Ref.')[source]¶
Add prrety risk string column.
- Parameters
res (
DataFrame
) – Dataframe contaning points and confidence intervals.risk (
str
) – point estimates of risk column name.lower (
str
) – lower confidence interval column name.upper (
str
) – upper confidence interval column name.fml (
str
) – formula for f string.ref (
str
) – if point esitmate column is empty, insert this string.
- Return type
Series
- myforestplot.cleaning_utils.count_category_frequency(df, categorical_cols, impute_continuous=True)[source]¶
Count category frequency.
- Parameters
df (
DataFrame
) – Original dataframe.categorical_cols (
List
[str
]) – Columns for categorical variables.impute_continuous (
bool
) – columns not specified as categorical_cols were imputed for item and number of observations (nobs).
- Return type
DataFrame
- myforestplot.cleaning_utils.sort_category_item(df_, order, item_order=None)[source]¶
Sort category and item based on categorical values.
- Parameters
df – dataframe containing category and item.
categorical – Dictionary containing column names and its order of items.
order (
List
[str
]) – if specified, category is ordered based on this variable.
- Return type
DataFrame
- myforestplot.cleaning_utils.statsmodels_fitting_result_dataframe(res, alpha=0.05, accessor=<ufunc 'exp'>)[source]¶
Create category and item columns from the statsmodels result. Categorical results are divided into original column name (category) and its items (item).
- Parameters
res – statsmodels’ fitting results.
alpha (
float
) – The significance level for the confidence interval.accessor (
Callable
[[array
],array
]) – Function to access each model result, which is summarized and displayed.
- Return type
DataFrame
- myforestplot.cleaning_utils.statsmodels_pretty_result_dataframe(data, res, order, cont_cols=None, item_order=None, fml='.2f', accessor=<ufunc 'exp'>)[source]¶
Obtain pretty result dataframe from statsmodels results. Fitting coefficients are converted by np.exp.
- Parameters
data (
DataFrame
) – original dataframe.res – statsmodels results.
categorical – Dictionary containing column names and its order of items.
order (
List
[str
]) – if specified, category is ordered based on this variable.fml (
str
) – formula for f string of pretty risk.accessor (
Callable
[[array
],array
]) – Function to access each model result, which is summarized and displayed.
- Return type
DataFrame
myforestplot.myforestplot module¶
- class myforestplot.myforestplot.ForestPlot(df, ratio=(8, 3), fig_ax_index=None, figsize=(5, 3), yticks_show=False, yticklabels_show=False, xticks_show=True, text_axis_off=True, hide_spines=<factory>, dpi=150, vertical_align=False)[source]¶
Bases:
object
- Parameters
df (
DataFrame
) – Result dataframe.ratio (
Tuple
[float
,float
]) – Number of axes field and size ratio of these axes. self.axd contains axes of which index starts from 1.fig_ax_index (
Optional
[List
[int
]]) – If specified, x ticks and x labels are left.figsize (
Tuple
[float
,float
]) – Figure size.hide_spines (
List
[str
]) – Hide outlines of axes. Takes “right”,”top”,”bottom”,”left” or these list.vertical_align (
bool
) – Align categorical names above items. It requires dataframe to have “category” and “item” column names.
- ax_method_to_figs(method, *args, **kwds)[source]¶
Apply axis method to all the figure fields.
- Parameters
method (
str
) – Name of axis method.fig_ax_index – If not specified, apply to all figs. If specified, apply to specified index axis fields.
*args – Passed to a specified method.
**kwds – Passed to a specified method.
- df: DataFrame¶
- dpi: int = 150¶
- draw_horizontal_line(y, scale=0.1, **kwds)[source]¶
Draw horizontal line.
- Parameters
kwds – Passed to ax.axhline.
- draw_outer_marker(index, lower=0, upper=1, lower_marker=4, upper_marker=5, y_adj=0, df=None, log_scale=False, scale=0, **kwds)[source]¶
Draw markers to indicate outer range of confidence intervals.
- Parameters
y_adj (
float
) – For this value, points are moved vertically.scale (
float
) – Control position of markers. scale * x range is slided towards inside.kwds – Passed to ax.scatter.
- embed_cate_strings(index, col, x, header='', fontsize=None, y_header=1.0, y_adj=0.0, text_kwds=None, header_kwds=None, replace=None, df=None)[source]¶
Embed category values on vertically aligned positions. The position of strings become different only if self.vertical_align == True.
- embed_strings(index, col, x, header='', fontsize=None, y_header=1.0, y_adj=0.0, text_kwds=None, header_kwds=None, duplicate_hide=False, replace=None, df=None)[source]¶
Embed strings/values of one column with header.
- Parameters
col (
str
) – Column name for text.x (
float
) – x axis value of text position, ranging from 0 to 1.df (
Optional
[DataFrame
]) – Dataframe for another result.
- errorbar(index, risk='risk', lower=0, upper=1, y_adj=0, errorbar_kwds=None, ref_kwds=None, df=None, errorbar_color=None, ref_color=None, label=None, log_scale=False)[source]¶
- Parameters
index (
int
) – axis’s index.risk (
str
) – Column name for risk.lower (
Union
[str
,int
]) – Column name for lower confidence interval.upper (
Union
[str
,int
]) – Column name for upper confidence interval.y_adj (
float
) – For this value, points are moved vertically.errorbar_kwds (
Optional
[dict
]) – Passed to ax.errorbar function.ref_kwds (
Optional
[dict
]) – Passed to ax.scatter function.df (
Optional
[DataFrame
]) – Dataframe for another result.errorbar_color (
Optional
[str
]) – If specified, ecolor and coloer in erorrbar_kwds is changed to this value.ref_color (
Optional
[str
]) – If specified, ecolor and coloer in ref_kwds is changed to this value.label (
Optional
[str
]) – Label for stratified drawings. Passed to ax.errorbar.log_scale (
bool
) – Plot risk in log scale (np.log).
- fig_ax_index: Optional[List[int]] = None¶
- figsize: Tuple[float, float] = (5, 3)¶
- h_multi_embed_strings(fig_ax_index, col, x, df, by, order, y_adj=0.0, multi_kwds=None, **kwds)[source]¶
Embed strings for multiple text fileds for stratified dataframe. fig_ax_index nad x takes one value or list types.
- Parameters
fig_ax_index (
Union
[int
,List
[int
]]) – Should be same as stratified group items.x (
Union
[float
,List
[float
]]) – x axis value of text position, ranging from 0 to 1.df (
DataFrame
) – DataFrame to be plotted.by (
str
) – Dataframe is stratified by this column.order (
List
[str
]) – Column items are plotted by this order.col (
str
) – Columns to be used for strings.**kwds – Passed to myforestplot.vis_utils.embed_strings_forestplot.
See also
myforestplot.vis_utils.embed_strings_forestplot myforestplot.myforestplot.ForestPlot.embed_strings myforestplot.myforestplot.ForestPlot.embed_cate_strings
- h_multi_errorbar(df, by, order, y_adj=0.0, multi_kwds=None, **kwds)[source]¶
Horizontal multiple errorbar plots.
- Parameters
df (
DataFrame
) – Dataframe to be stratified.by (
str
) – Dataframe is stratified by this column.order (
List
[str
]) – Column items are plotted by this order.y_adj (
float
) – For this value, points are moved vertically.multi_kwds (
Optional
[Dict
[str
,list
]]) – Options changed over each plotting are specified by this parameter.kwds – Passsed to ForestPlot.errorbar.
- hide_spines: List[str]¶
- horizontal_variable_separators(scale=0.1, **kwds)[source]¶
Draw horizontal lines for seprating variables.
- Parameters
kwds – Passed to ax.axhline function.
- ratio: Tuple[float, float] = (8, 3)¶
- text_axis_off: bool = True¶
- v_multi_embed_strings(index, col, x, df, by, order, scale=0.4, multi_kwds=None, **kwds)[source]¶
Embed strings for multiple text fileds for stratified dataframe. fig_ax_index nad x takes one value or list types.
- v_multi_errorbar(index, df, by, order, scale=0.4, multi_kwds=None, **kwds)[source]¶
Verticle multiple errorbar plots.
- Parameters
index (
int
) – To draw points to this fig_ax_index field.df (
DataFrame
) – Dataframe to be stratified.by (
str
) – Dataframe is stratified by this column.order (
List
[str
]) – If specified, column items are plotted by this order.scale (
float
) – [-scale, scale] is set to be a range of y_adj.multi_kwds (
Optional
[Dict
[str
,list
]]) – Options changed over each plotting are specified by this parameter.kwds – Passsed to ForestPlot.errorbar.
- vertical_align: bool = False¶
- xticks_show: bool = True¶
- yticklabels_show: bool = False¶
- yticks_show: bool = False¶
- class myforestplot.myforestplot.SimpleForestPlot(df, ratio=(8, 3), fig_ax_index=None, figsize=(5, 3), yticks_show=False, yticklabels_show=False, xticks_show=True, text_axis_off=True, hide_spines=<factory>, dpi=150, vertical_align=False)[source]¶
Bases:
ForestPlot
Simple version of a forest plot, contaning one text field and one axis field.
- df: DataFrame¶
- draw_outer_marker(*args, **kwds)[source]¶
Draw markers to indicate outer range of confidence intervals.
- Parameters
y_adj – For this value, points are moved vertically.
scale – Control position of markers. scale * x range is slided towards inside.
kwds – Passed to ax.scatter.
- embed_cate_strings(*args, **kwds)[source]¶
Embed category values on vertically aligned positions. The position of strings become different only if self.vertical_align == True.
- embed_strings(*args, **kwds)[source]¶
Embed strings/values of one column with header.
- Parameters
col – Column name for text.
x – x axis value of text position, ranging from 0 to 1.
df – Dataframe for another result.
- errorbar(*args, **kwds)[source]¶
- Parameters
index – axis’s index.
risk – Column name for risk.
lower – Column name for lower confidence interval.
upper – Column name for upper confidence interval.
y_adj – For this value, points are moved vertically.
errorbar_kwds – Passed to ax.errorbar function.
ref_kwds – Passed to ax.scatter function.
df – Dataframe for another result.
errorbar_color – If specified, ecolor and coloer in erorrbar_kwds is changed to this value.
ref_color – If specified, ecolor and coloer in ref_kwds is changed to this value.
label – Label for stratified drawings. Passed to ax.errorbar.
log_scale – Plot risk in log scale (np.log).
- hide_spines: List[str]¶
- v_multi_embed_strings(*args, **kwds)[source]¶
Embed strings for multiple text fileds for stratified dataframe. fig_ax_index nad x takes one value or list types.
- v_multi_errorbar(*args, **kwds)[source]¶
Verticle multiple errorbar plots.
- Parameters
index – To draw points to this fig_ax_index field.
df – Dataframe to be stratified.
by – Dataframe is stratified by this column.
order – If specified, column items are plotted by this order.
scale – [-scale, scale] is set to be a range of y_adj.
multi_kwds – Options changed over each plotting are specified by this parameter.
kwds – Passsed to ForestPlot.errorbar.
myforestplot.vis_utils module¶
- myforestplot.vis_utils.embed_strings_forestplot(ax, ser, y_index, x, header='', fontsize=None, y_header=1.0, text_kwds=None, header_kwds=None, replace=None)[source]¶
Embed strings/values of one column with header.
- Parameters
ser (
Series
) – Values of this series will be embedded.x (
float
) – x axis value of text position, ranging from 0 to 1.df – Dataframe for another result.
- myforestplot.vis_utils.errorbar_forestplot(ax, y_index, df=None, risk='risk', lower=0, upper=1, y_adj=0, errorbar_kwds=None, ref_kwds=None, errorbar_color=None, ref_color=None, label=None, log_scale=False)[source]¶
Error bar plot for a forest plot.
- Parameters
ax (
Axes
) – Axis to be drawn.y_index (
array
) – index to be plotted.risk (
str
) – Column name for risk.lower (
Union
[str
,int
]) – Column name for lower confidence interval.upper (
Union
[str
,int
]) – Column name for upper confidence interval.y_adj (
float
) – For this value, plotting is moved.errorbar_kwds (
Optional
[dict
]) – Passed to ax.errorbar function.ref_kwds (
Optional
[dict
]) – Passed to ax.scatter function.df (
Optional
[DataFrame
]) – Dataframe for another result.label (
Optional
[str
]) – Label for stratified drawings. Passed to ax.errorbar.log_scale (
bool
) – Plot risk in log scale (np.log).
- myforestplot.vis_utils.get_multiple_y_adjs(n, scale)[source]¶
For multiple vertical plotting, automatic adjustments of y_adj for y_index is needed.
- Parameters
n (
int
) – Number of stratificaitons to be plotted.scale (
float
) – [-scale, scale] is set to be a range of y_adj.
- Return type
array
- myforestplot.vis_utils.obtain_indexes_from_category_item(ser_cate, ser_item)[source]¶
Create index for category and item from series of category and item for vertically aligned labels and errorbar plot. It is noted that index has negative continuous values, starting from 0 to -n.
- Parameters
ser_cate (
Series
) – Series of categories matched with ser_item.ser_item (
Series
) – Series of items.
- Return type
Tuple
[array
,array
]- Returns
Indexes for caategories and items.