Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mplot #108

Merged
merged 68 commits into from
Jan 21, 2024
Merged

Mplot #108

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
03380e3
Allow no drawing of CI (#58)
LSYS Feb 16, 2023
73d393a
Update CI.yml
LSYS Feb 16, 2023
4f491b4
Update nb.yml
LSYS Feb 16, 2023
c35ea1c
desbugging failed build on ubuntu Python 3.9
LSYS Feb 16, 2023
b384f05
Update options (#58)
LSYS Mar 3, 2023
a32fd96
Increase images
LSYS Mar 3, 2023
67d6ebd
Bump to v0.3.0 (#58)
LSYS Mar 3, 2023
8b2c276
Preparing data for mforestplot (closes #59)
LSYS Mar 21, 2023
05153c5
Updating workflows to work with the 'mplot' branch
LSYS Mar 21, 2023
aaea789
Add src for mplot + notebook to test some plots (#67)
LSYS Apr 6, 2023
33ac52f
Sync (#69)
LSYS Apr 6, 2023
e129a60
Bump to correct ver (#67)
LSYS Dec 14, 2023
74bf53b
Reorganizing src for mplot (closes #87)
LSYS Dec 14, 2023
705766b
Fixes #82
LSYS Dec 14, 2023
2b5ce9c
Pleasing black
LSYS Dec 14, 2023
1adcb7c
Docstring and tests for mplot_dataframe_utils.py (#88, #89)
LSYS Dec 15, 2023
62e3b19
Troubleshooting workflow error (#87)
LSYS Dec 15, 2023
9a79b54
Add branch to workflow (#88)
LSYS Dec 15, 2023
8f504af
Testing make_multimodel_tableheaders (#88)
LSYS Dec 15, 2023
79e83fc
Add test_make_multimodel_tableheaders (#88)
LSYS Dec 15, 2023
9fd352f
Pleasing linters (#88)
LSYS Dec 15, 2023
976f58d
Add tests for `mplot_dataframe_utils` (for #88) (#90)
LSYS Dec 15, 2023
ab93c4c
Add docstring & test for (#88, #89)
LSYS Dec 16, 2023
09af3d4
Fix compatibility with newer mpl versions (#82)
LSYS Dec 16, 2023
eaee867
Pleasing linters
LSYS Dec 16, 2023
ebb4294
Pleasing linters
LSYS Dec 16, 2023
794f491
Add docstring & test for mdraw_yticklabels (#88, #89) (#91)
LSYS Dec 16, 2023
be5eed0
Add docstring & test for mdraw_est_markers (#88, #89)
LSYS Dec 16, 2023
003ec98
Add docstring & test for mdraw_est_markers (#88, #89)
LSYS Dec 16, 2023
5a5bd3e
Pleasing linters
LSYS Dec 16, 2023
1268b32
Merge branch 'mplot' into mplot-dev
LSYS Dec 16, 2023
58d4104
Add docstring & test for mdraw_est_markers (#88, #89) (#92)
LSYS Dec 16, 2023
3e8eec4
Merge branch 'mplot' of https://github.com/LSYS/forestplot into mplot…
LSYS Dec 16, 2023
e567982
Add docstring & test for mdraw_ci (#88, #89)
LSYS Dec 16, 2023
6cd5721
Pleasing linters
LSYS Dec 16, 2023
9933ffa
Add docstring & test for mdraw_ci (#88, #89) (#93)
LSYS Dec 16, 2023
22faae9
Add test for mdraw_legend (#88, #89)
LSYS Dec 16, 2023
c02c47a
Troubleshooting older py/mpl ver
LSYS Dec 16, 2023
b278ac1
Troubleshooting older py/mpl ver
LSYS Dec 16, 2023
a73dd86
Pleasing linters
LSYS Dec 16, 2023
aeea499
Add test for mdraw_legend (#88, #89) (#94)
LSYS Dec 16, 2023
6a60ff8
Resolving merge conflict..
LSYS Dec 16, 2023
ffa043e
Add docstring & test for mdraw_yticklabel2 (#88, #89)
LSYS Dec 16, 2023
ea942e3
Pleasing linters
LSYS Dec 16, 2023
2121c23
Pleasing linters
LSYS Dec 16, 2023
b406f7a
Add docstring & test for mdraw_yticklabel2 (#88, #89) (#95)
LSYS Dec 16, 2023
624eb5a
Add docstring & test for mplot (#88, #89)
LSYS Dec 16, 2023
d88530f
Pleasing linters
LSYS Dec 23, 2023
33bf8d0
Add docstring & test for mplot (#88, #89) (#96)
LSYS Dec 23, 2023
1a895a0
Add multimodel example plot (#99)
LSYS Jan 7, 2024
17c7dcc
Update README.md
LSYS Jan 7, 2024
b903bd1
Fix mcolor arg (#59)
LSYS Jan 14, 2024
7799515
Fixing merge conflict..
LSYS Jan 14, 2024
8b949ec
Adding back example notebook..
LSYS Jan 14, 2024
c1d28c8
Fixing merge conflict..
LSYS Jan 14, 2024
05c1c05
Merge pull request #100 from LSYS/mplot-dev
LSYS Jan 14, 2024
8b649ef
Adding back example notebook..
LSYS Jan 14, 2024
7d0402f
Fix notebook example
LSYS Jan 14, 2024
29f0c0c
Merge pull request #101 from LSYS/mplot-dev
LSYS Jan 14, 2024
ff36ae0
Move xrefline to zorder=0
LSYS Jan 15, 2024
f866b20
Merge pull request #102 from LSYS/mplot-dev
LSYS Jan 15, 2024
f822f4d
Add option to remove mplot legend (#103)
LSYS Jan 15, 2024
6956b9d
Mplot dev (#104)
LSYS Jan 15, 2024
bee1af9
Add option to set x-axis limit (xlim)
LSYS Jan 16, 2024
a12e6ff
Add docstring to mplot.py (#105)
LSYS Jan 21, 2024
7fc2dd1
Update readme (#106)
LSYS Jan 21, 2024
deabdfc
Bump ver (#107)
LSYS Jan 21, 2024
6f4baf0
Merge branch 'main' into mplot
LSYS Jan 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ notebook:
cd examples && runpynb readme-examples.ipynb

BLACK_OPTS := -l 95
SRC_FILES := arg_validators dataframe_utils graph_utils plot text_utils
SRC_FILES := arg_validators dataframe_utils graph_utils text_utils plot mplot
SRC_FILES := $(addprefix forestplot/, $(addsuffix .py, $(SRC_FILES)))
.PHONY: lint
lint: # Check with mypy, pyflakes, black
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Additional options allow easy addition of columns in the `dataframe` as annotati
> - [Quick Start](#quick-start)
> - [Some Examples with Customizations](#some-examples-with-customizations)
> - [Gallery and API Options](#gallery-and-api-options)
> - [Multi-models](#multi-models)
> - [Known Issues](#known-issues)
> - [Background and Additional Resources](#background-and-additional-resources)
> - [Contributing](#contributing)
Expand Down Expand Up @@ -257,6 +258,57 @@ fp.forestplot(df, # the dataframe with results data
</details>
<p align="right">(<a href="#top">back to top</a>)</p>

<!------------------- Multi-models ------------------->
## Multi-models[![](https://raw.githubusercontent.com/LSYS/forestplot/main/docs/images/pin.svg)](#multi-models)

For coefficient plots where each variable can have multiple estimates (each `model` has one).

```python
import forestplot as fp

df_mmodel = pd.read_csv("../examples/data/sleep-mmodel.csv").query(
"model=='all' | model=='young kids'"
)
df_mmodel.head(3)
```

| | var | coef | se | T | pval | r2 | adj_r2 | ll | hl | model | group | label |
|---:|:------|-----------:|---------:|----------:|---------:|---------:|-----------:|-----------:|--------:|:-----------|:--------------|:------------|
| 0 | age | 0.994889 | 1.96925 | 0.505213 | 0.613625 | 0.127289 | 0.103656 | -2.87382 | 4.8636 | all | age | in years |
| 3 | age | 22.634 | 15.4953 | 1.4607 | 0.149315 | 0.178147 | -0.0136188 | -8.36124 | 53.6293 | young kids | age | in years |
| 4 | black | -84.7966 | 82.1501 | -1.03222 | 0.302454 | 0.127289 | 0.103656 | -246.186 | 76.5925 | all | other factors | =1 if black |

```python
fp.mforestplot(
dataframe=df_mmodel,
estimate="coef",
ll="ll",
hl="hl",
varlabel="label",
capitalize="capitalize",
model_col="model",
color_alt_rows=True,
groupvar="group",
table=True,
rightannote=["var", "group"],
right_annoteheaders=["Source", "Group"],
xlabel="Coefficient (95% CI)",
modellabels=["Have young kids", "Full sample"],
xticks=[-1200, -600, 0, 600],
mcolor=["#CC6677", "#4477AA"],
# Additional kwargs for customizations
**{
"markersize": 30,
# override default vertical offset between models (0.0 to 1.0)
"offset": 0.35,
"xlinestyle": (0, (10, 5)), # long dash for x-reference line
"xlinecolor": ".8", # gray color for x-reference line
},
)
```
<p align="left"><img width="100%" src="https://raw.githubusercontent.com/LSYS/forestplot/mplot-dev/docs/images/multimodel.png"></p>

Please note: This module is still experimental. See [this jupyter notebook](https://nbviewer.org/github/LSYS/forestplot/blob/mplot-dev/examples/test-multmodel-sleep.ipynb) for more examples and tweaks.

<!------------------- GALLERY AND API OPTIONS ------------------->
## Gallery and API Options[![](https://raw.githubusercontent.com/LSYS/forestplot/main/docs/images/pin.svg)](#gallery-and-api-options)
Expand Down
Binary file modified docs/images/group-grouporder-pvalue-sort-colorrows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/multimodel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions examples/data/sleep-mmodel.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var,coef,se,T,pval,r2,adj_r2,ll,hl,model,group,label
age,0.99488919909132,1.9692489158653246,0.5052125158358395,0.6136248760479441,0.12728876841881298,0.10365635595820055,-2.8738245461702143,4.863602944352854,all,age,in years
age,1.4828927472424902,2.8854619757926523,0.5139186583233804,0.6077171047169159,0.18038221190248027,0.13910649595512325,-4.197237184638171,7.163022679123152,men,age,in years
age,0.953199898987002,2.9432093495480007,0.32386411762839373,0.7463430958352442,0.10910549372734824,0.05342458708530751,-4.846720706047407,6.75312050402141,women,age,in years
age,22.634017225177878,15.49532005480011,1.4607002078776912,0.14931480960176477,0.17814691642754532,-0.013618803072694252,-8.361237717039167,53.62927216739492,young kids,age,in years
black,-84.79661217544819,82.15012465488611,-1.0322152587312559,0.3024539030548957,0.12728876841881298,0.10365635595820055,-246.18571533392443,76.59249098302806,all,other factors,=1 if black
black,-132.96710936577898,132.581166096424,-1.0029109961898683,0.31677594790578495,0.18038221190248027,0.13910649595512325,-393.95763922402284,128.02342049246488,men,other factors,=1 if black
black,-68.35063065532516,108.90175418428296,-0.6276357177834123,0.5308818950534195,0.10910549372734824,0.05342458708530751,-282.95362113400626,146.25235982335596,women,other factors,=1 if black
black,-119.74103936970126,296.4329532300549,-0.40393970395313283,0.6876932233318072,0.17814691642754532,-0.013618803072694252,-712.6952298056457,473.21315106624314,young kids,other factors,=1 if black
clerical,22.505827481347207,48.96326328570245,0.45964721244221146,0.6459626857575079,0.12728876841881298,0.10365635595820055,-73.6855925875676,118.69724755026202,all,occupation,=1 if clerical worker
clerical,-229.27596235505098,102.88822122848798,-2.228398543754476,0.026653770224379605,0.18038221190248027,0.13910649595512325,-431.81492143364045,-26.737003276461508,men,occupation,=1 if clerical worker
clerical,106.5072576745419,61.1273211607197,1.7423838580216282,0.08281375364786414,0.10910549372734824,0.05342458708530751,-13.950911427517042,226.96542677660085,women,occupation,=1 if clerical worker
clerical,-239.90257363807342,211.76019987926273,-1.1328973705864291,0.2617642403464634,0.17814691642754532,-0.013618803072694252,-663.4860400434246,183.68089276727778,young kids,occupation,=1 if clerical worker
construc,113.9295583737603,105.04513651515707,1.0845771841833085,0.2786144612102424,0.12728876841881298,0.10365635595820055,-92.43824014829062,320.2973568958112,all,occupation,=1 if construction worker
construc,65.69605933555773,107.88920815173707,0.6089215081008081,0.5430737247378323,0.18038221190248027,0.13910649595512325,-146.6875124081796,278.07963107929504,men,occupation,=1 if construction worker
construc,361.75642960038044,428.2648335748007,0.8447026261314451,0.3991781717050136,0.10910549372734824,0.05342458708530751,-482.1869456962427,1205.6998048970036,women,occupation,=1 if construction worker
construc,-186.41520083935393,358.22350129635726,-0.5203879705400266,0.604708210492017,0.17814691642754532,-0.013618803072694252,-902.9688899343187,530.1384882556108,young kids,occupation,=1 if construction worker
educ,-7.210558397084539,7.516735606083484,-0.9592672637373134,0.33787250903288846,0.12728876841881298,0.10365635595820055,-21.977659764751728,7.55654297058265,all,labor factors,years of schooling
educ,-7.545419560820269,9.72034104139323,-0.7762504966326546,0.4382610079422641,0.18038221190248027,0.13910649595512325,-26.680241145893653,11.589402024253113,men,labor factors,years of schooling
educ,-6.278396591271823,11.869882954315754,-0.5289350042823354,0.5973742665985272,0.10910549372734824,0.05342458708530751,-29.66931804803307,17.11252486548942,women,labor factors,years of schooling
educ,-20.972115853313863,26.97403159331749,-0.7774928186303995,0.43992297847502027,0.17814691642754532,-0.013618803072694252,-74.92821247458355,32.98398076795583,young kids,labor factors,years of schooling
gdhlth,-82.58043355263888,58.2047169680557,-1.4187928033042618,0.15656198407017147,0.12728876841881298,0.10365635595820055,-196.9272724713884,31.766405366110618,all,health factors,=1 if in good or excel. health
gdhlth,-188.28450807319786,80.4765387271364,-2.339619857553702,0.0200113537818631,0.18038221190248027,0.13910649595512325,-346.70530871902446,-29.863707427371253,men,health factors,=1 if in good or excel. health
gdhlth,-24.491583169191312,87.716866542274,-0.2792117882754956,0.780339918384747,0.10910549372734824,0.05342458708530751,-197.3474018508352,148.3642355124526,women,health factors,=1 if in good or excel. health
gdhlth,-141.81497580656173,203.21302167135576,-0.6978636242903301,0.4879582244422951,0.17814691642754532,-0.013618803072694252,-548.3015402664399,264.67158865331646,young kids,health factors,=1 if in good or excel. health
lhrwage,21.60569674505552,31.989300709296707,0.6754038464734707,0.4997211854315843,0.12728876841881298,0.10365635595820055,-41.23930286789559,84.45069635800664,all,labor factors,log hourly wage
lhrwage,3.5447518688419417,47.045922578822974,0.0753466331307011,0.9399931101323359,0.18038221190248027,0.13910649595512325,-89.06674509586989,96.15624883355378,men,labor factors,log hourly wage
lhrwage,-22.5804823252749,53.782320170113664,-0.4198495389163717,0.6749977887003282,0.10910549372734824,0.05342458708530751,-128.56451196594747,83.40354731539766,women,labor factors,log hourly wage
lhrwage,35.609316976870375,122.3237377739899,0.29110716877098325,0.7719733324342494,0.17814691642754532,-0.013618803072694252,-209.07458915614382,280.29322310988454,young kids,labor factors,log hourly wage
prot,-9.249429608907253,39.7714621826478,-0.23256448471594685,0.8161916436328263,0.12728876841881298,0.10365635595820055,-87.38297645285611,68.88411723504161,all,other factors,=1 if Protestant
prot,-20.967327503532506,52.20106018322577,-0.4016647828595275,0.6882393483114742,0.18038221190248027,0.13910649595512325,-123.72688798645723,81.79223297939222,men,other factors,=1 if Protestant
prot,1.0666432850620384,61.89804739666575,0.017232260627327883,0.986266664673677,0.10910549372734824,0.05342458708530751,-120.91032738556035,123.04361395568444,women,other factors,=1 if Protestant
prot,-24.173124098859226,127.56649739999632,-0.18949429976949358,0.8503450117765196,0.17814691642754532,-0.013618803072694252,-279.34411088810623,230.9978626903878,young kids,other factors,=1 if Protestant
selfe,-21.27417859824218,63.303918327210305,-0.33606416728074434,0.7369588252939454,0.12728876841881298,0.10365635595820055,-145.63872032189542,103.09036312541107,all,labor factors,=1 if self employed
selfe,3.4840942455998385,77.58570652121905,0.044906393223949924,0.964214160112589,0.18038221190248027,0.13910649595512325,-149.246004987894,156.21419347909367,men,labor factors,=1 if self employed
selfe,-90.68593564430114,112.55649657600425,-0.8056925935240449,0.42127457554209236,0.10910549372734824,0.05342458708530751,-312.4910015401089,131.11913025150665,women,labor factors,=1 if self employed
selfe,-370.9565115107388,241.15347311683067,-1.538259046059946,0.12924271026812853,0.17814691642754532,-0.013618803072694252,-853.3352783269672,111.42225530548967,young kids,labor factors,=1 if self employed
smsa,-40.65355700424955,39.50772275371498,-1.0290027916232358,0.30395952409445154,0.12728876841881298,0.10365635595820055,-118.26897109938261,36.96185709088351,all,area of residence,=1 if live in smsa
smsa,-27.631427416163216,52.418592857611465,-0.5271302778237585,0.5985235118106444,0.18038221190248027,0.13910649595512325,-130.81920835921053,75.55635352688411,men,area of residence,=1 if live in smsa
smsa,-57.146400428034234,61.521030174672646,-0.928892124624412,0.35394487787468343,0.10910549372734824,0.05342458708530751,-178.38041682847313,64.08761597240468,women,area of residence,=1 if live in smsa
smsa,43.42957155402094,138.68308939805914,0.31315693746456685,0.7552474332941742,0.17814691642754532,-0.013618803072694252,-233.9779099865449,320.83705309458674,young kids,area of residence,=1 if live in smsa
south,82.48697958253655,46.37757109786662,1.7785963695354232,0.07589350453103752,0.12728876841881298,0.10365635595820055,-8.624685038753597,173.5986442038267,all,area of residence,=1 if live in south
south,79.40169407723894,64.43983230307614,1.2321834374706864,0.2189216981608257,0.18038221190248027,0.13910649595512325,-47.450305131492144,206.25369328597003,men,area of residence,=1 if live in south
south,114.62293382753175,69.30049378126785,1.6539988039524578,0.09952837233805101,0.10910549372734824,0.05342458708530751,-21.941378827550423,251.18724648261394,women,area of residence,=1 if live in south
south,71.85138083602305,136.01011187506683,0.5282796980714398,0.5992538218558838,0.17814691642754532,-0.013618803072694252,-200.2093495895635,343.9121112616096,young kids,area of residence,=1 if live in south
spsepay,-0.0010054015767941848,0.002647615337851829,-0.3797385377023568,0.7042954459596942,0.12728876841881298,0.10365635595820055,-0.006206808959675351,0.004196005806086981,all,other factors,spousal wage income
spsepay,0.0030693469858871225,0.0049240034441705854,0.623343793457507,0.533569693312251,0.18038221190248027,0.13910649595512325,-0.006623721085208695,0.01276241505698294,men,other factors,spousal wage income
spsepay,-0.0005081661923143077,0.0036213813114609078,-0.14032385673004633,0.8885302141467499,0.10910549372734824,0.05342458708530751,-0.0076444998990057005,0.006628167514377085,women,other factors,spousal wage income
spsepay,-0.0047013059752814,0.01144291160114906,-0.4108487541588026,0.6826470962008051,0.17814691642754532,-0.013618803072694252,-0.027590537117622167,0.018187925167059367,young kids,other factors,spousal wage income
totwrk,-0.1519224329433428,0.020366493775550474,-7.459429915507711,3.698705255539423e-13,0.12728876841881298,0.10365635595820055,-0.19193369499227117,-0.11191117089441441,all,labor factors,mins worked per week
totwrk,-0.19100803367191987,0.029765234919101078,-6.41715189519117,5.964764273464163e-10,0.18038221190248027,0.13910649595512325,-0.2496019098733212,-0.13241415747051855,men,labor factors,mins worked per week
totwrk,-0.1255122674915504,0.0327128882144724,-3.8367834313089766,0.00016213485682276793,0.10910549372734824,0.05342458708530751,-0.18997664357626337,-0.06104789140683743,women,labor factors,mins worked per week
totwrk,-0.11299498951128406,0.07636280752220165,-1.479712351833476,0.14418175650843867,0.17814691642754532,-0.013618803072694252,-0.2657433470078354,0.039753367985267324,young kids,labor factors,mins worked per week
yrsmarr,-0.07595761901493851,2.009355661684067,-0.03780197824773214,0.9698601588766725,0.12728876841881298,0.10365635595820055,-4.023463596978298,3.8715483589484214,all,family factors,years married
yrsmarr,0.2241161052983935,2.93479128035093,0.0763652620882786,0.939183433631042,0.18038221190248027,0.13910649595512325,-5.553120238550619,6.001352449147406,men,family factors,years married
yrsmarr,-1.4141371514041594,2.9319849741652555,-0.48231391492951575,0.630053922467078,0.10910549372734824,0.05342458708530751,-7.191938879277817,4.363664576469498,women,family factors,years married
yrsmarr,-31.211144074101608,16.91401104102699,-1.8452834161214147,0.06993266917751552,0.17814691642754532,-0.013618803072694252,-65.04420350482327,2.621915356620054,young kids,family factors,years married
Loading
Loading