0

我有以下内容dataframe

import pandas as pd
import numpy as np
from plotnine import *

df = pd.DataFrame.from_dict({'variable': {0: 'intercept', 1: 'intercept', 2: 'intercept', 3: 'intercept', 4: 'intercept', 5: 'intercept', 6: 'intercept', 7: 'intercept', 8: 'intercept', 9: 'intercept', 10: 'intercept', 11: 'intercept', 12: 'intercept', 13: 'intercept', 14: 'intercept', 15: 'intercept', 16: 'intercept', 17: 'intercept', 18: 'intercept', 19: 'intercept', 20: 'intercept', 21: 'intercept', 22: 'intercept', 23: 'intercept', 24: 'intercept', 25: 'intercept', 26: 'intercept', 27: 'intercept', 28: 'intercept', 29: 'intercept', 30: 'intercept', 31: 'intercept', 32: 'intercept', 33: 'intercept', 34: 'intercept', 35: 'intercept', 36: 'intercept', 37: 'intercept', 38: 'intercept', 39: 'intercept', 40: 'intercept', 41: 'intercept', 42: 'intercept', 43: 'intercept', 44: 'intercept', 45: 'intercept', 46: 'intercept', 47: 'intercept', 48: 'intercept', 49: 'intercept', 50: 'rsd_language_english', 51: 'rsd_language_english', 52: 'rsd_language_english', 53: 'rsd_language_english', 54: 'rsd_language_english', 55: 'rsd_language_english', 56: 'rsd_language_english', 57: 'rsd_language_english', 58: 'rsd_language_english', 59: 'rsd_language_english', 60: 'rsd_language_english', 61: 'rsd_language_english', 62: 'rsd_language_english', 63: 'rsd_language_english', 64: 'rsd_language_english', 65: 'rsd_language_english', 66: 'rsd_language_english', 67: 'rsd_language_english', 68: 'rsd_language_english', 69: 'rsd_language_english', 70: 'rsd_language_english', 71: 'rsd_language_english', 72: 'rsd_language_english', 73: 'rsd_language_english', 74: 'rsd_language_english', 75: 'rsd_language_english', 76: 'rsd_language_english', 77: 'rsd_language_english', 78: 'rsd_language_english', 79: 'rsd_language_english', 80: 'rsd_language_english', 81: 'rsd_language_english', 82: 'rsd_language_english', 83: 'rsd_language_english', 84: 'rsd_language_english', 85: 'rsd_language_english', 86: 'rsd_language_english', 87: 'rsd_language_english', 88: 'rsd_language_english', 89: 'rsd_language_english', 90: 'rsd_language_english', 91: 'rsd_language_english', 92: 'rsd_language_english', 93: 'rsd_language_english', 94: 'rsd_language_english', 95: 'rsd_language_english', 96: 'rsd_language_english', 97: 'rsd_language_english', 98: 'rsd_language_english', 99: 'rsd_language_english', 100: 'rsd_gender_male', 101: 'rsd_gender_male', 102: 'rsd_gender_male', 103: 'rsd_gender_male', 104: 'rsd_gender_male', 105: 'rsd_gender_male', 106: 'rsd_gender_male', 107: 'rsd_gender_male', 108: 'rsd_gender_male', 109: 'rsd_gender_male', 110: 'rsd_gender_male', 111: 'rsd_gender_male', 112: 'rsd_gender_male', 113: 'rsd_gender_male', 114: 'rsd_gender_male', 115: 'rsd_gender_male', 116: 'rsd_gender_male', 117: 'rsd_gender_male', 118: 'rsd_gender_male', 119: 'rsd_gender_male', 120: 'rsd_gender_male', 121: 'rsd_gender_male', 122: 'rsd_gender_male', 123: 'rsd_gender_male', 124: 'rsd_gender_male', 125: 'rsd_gender_male', 126: 'rsd_gender_male', 127: 'rsd_gender_male', 128: 'rsd_gender_male', 129: 'rsd_gender_male', 130: 'rsd_gender_male', 131: 'rsd_gender_male', 132: 'rsd_gender_male', 133: 'rsd_gender_male', 134: 'rsd_gender_male', 135: 'rsd_gender_male', 136: 'rsd_gender_male', 137: 'rsd_gender_male', 138: 'rsd_gender_male', 139: 'rsd_gender_male', 140: 'rsd_gender_male', 141: 'rsd_gender_male', 142: 'rsd_gender_male', 143: 'rsd_gender_male', 144: 'rsd_gender_male', 145: 'rsd_gender_male', 146: 'rsd_gender_male', 147: 'rsd_gender_male', 148: 'rsd_gender_male', 149: 'rsd_gender_male', 150: 'rsd_hhcomposition_children', 151: 'rsd_hhcomposition_children', 152: 'rsd_hhcomposition_children', 153: 'rsd_hhcomposition_children', 154: 'rsd_hhcomposition_children', 155: 'rsd_hhcomposition_children', 156: 'rsd_hhcomposition_children', 157: 'rsd_hhcomposition_children', 158: 'rsd_hhcomposition_children', 159: 'rsd_hhcomposition_children', 160: 'rsd_hhcomposition_children', 161: 'rsd_hhcomposition_children', 162: 'rsd_hhcomposition_children', 163: 'rsd_hhcomposition_children', 164: 'rsd_hhcomposition_children', 165: 'rsd_hhcomposition_children', 166: 'rsd_hhcomposition_children', 167: 'rsd_hhcomposition_children', 168: 'rsd_hhcomposition_children', 169: 'rsd_hhcomposition_children', 170: 'rsd_hhcomposition_children', 171: 'rsd_hhcomposition_children', 172: 'rsd_hhcomposition_children', 173: 'rsd_hhcomposition_children', 174: 'rsd_hhcomposition_children', 175: 'rsd_hhcomposition_children', 176: 'rsd_hhcomposition_children', 177: 'rsd_hhcomposition_children', 178: 'rsd_hhcomposition_children', 179: 'rsd_hhcomposition_children', 180: 'rsd_hhcomposition_children', 181: 'rsd_hhcomposition_children', 182: 'rsd_hhcomposition_children', 183: 'rsd_hhcomposition_children', 184: 'rsd_hhcomposition_children', 185: 'rsd_hhcomposition_children', 186: 'rsd_hhcomposition_children', 187: 'rsd_hhcomposition_children', 188: 'rsd_hhcomposition_children', 189: 'rsd_hhcomposition_children', 190: 'rsd_hhcomposition_children', 191: 'rsd_hhcomposition_children', 192: 'rsd_hhcomposition_children', 193: 'rsd_hhcomposition_children', 194: 'rsd_hhcomposition_children', 195: 'rsd_hhcomposition_children', 196: 'rsd_hhcomposition_children', 197: 'rsd_hhcomposition_children', 198: 'rsd_hhcomposition_children', 199: 'rsd_hhcomposition_children'}, 'value': {0: 0.10500855602689459, 1: 0.1068098294244098, 2: 0.09342589944215585, 3: 0.1061000551420421, 4: 0.11492310029903505, 5: 0.11502647914064572, 6: 0.109085269781937, 7: 0.11479613008126498, 8: 0.11644317689263607, 9: 0.11386819144694951, 10: 0.10592535566654736, 11: 0.11564495752520938, 12: 0.11755113461268275, 13: 0.10965826644730681, 14: 0.11149942392267168, 15: 0.10866229195486116, 16: 0.11204180891895299, 17: 0.11165051062243626, 18: 0.11362694111626778, 19: 0.10359533919159215, 20: 0.10781599787996163, 21: 0.09219804942636921, 22: 0.10676301317036363, 23: 0.11885822429466916, 24: 0.09532240202670413, 25: 0.10517827889168432, 26: 0.09713698288997269, 27: 0.12086268555027128, 28: 0.10309251443652648, 29: 0.10762069384180298, 30: 0.10663527473294918, 31: 0.10960455401169773, 32: 0.11226653043183722, 33: 0.09716138081131365, 34: 0.10162932681425693, 35: 0.10818961342527236, 36: 0.10200789109756818, 37: 0.11469411901225685, 38: 0.11414604437106246, 39: 0.10008260189579704, 40: 0.10293422082695224, 41: 0.11592179353793962, 42: 0.1075167611271643, 43: 0.09794819337806711, 44: 0.10786208962972188, 45: 0.11492711221368865, 46: 0.09362129805249843, 47: 0.08892531012973201, 48: 0.10887318752959842, 49: 0.10495969091378586, 50: 0.03531973594672566, 51: 0.07746130682930821, 52: 0.03061606037589254, 53: 0.05410729783077766, 54: 0.08402334953527904, 55: 0.04578921831717423, 56: 0.07240682490202507, 57: 0.027507266029887237, 58: 0.05011211459390874, 59: 0.06995294764532407, 60: 0.03518705156012266, 61: 0.09418314861928213, 62: 0.008080400252439028, 63: 0.1100418878693322, 64: 0.1382456428259256, 65: 0.06354256107990783, 66: -0.004773723456196495, 67: 0.08497178819386066, 68: 0.04113876369234462, 69: 0.06183377164109895, 70: 0.04102534830978422, 71: 0.05118861177949173, 72: 0.07535071471094312, 73: 0.030052798636872966, 74: 0.04912494715311575, 75: 0.058599701597143734, 76: 0.07665546483600151, 77: 0.04973295929687507, 78: 0.00021473801613725767, 79: 0.12341939798792707, 80: 0.07798053575485031, 81: 0.06355974167402832, 82: 0.07631550967874717, 83: 0.034151789177944106, 84: -0.019410537591460133, 85: 0.10109249724856682, 86: 0.04274449738684026, 87: 0.05619304058716145, 88: 0.05515117607723316, 89: 0.03394694346937757, 90: 0.03616556597702144, 91: 0.06158591870898992, 92: 0.047997936977752585, 93: 0.0836763594226807, 94: 0.03560005035570244, 95: 0.07131496803895149, 96: 0.08445706130132204, 97: 0.039560734619253524, 98: -0.022867763626125092, 99: 0.11205651377755843, 100: -0.049921852423560654, 101: -0.04256408668770419, 102: -0.04612481259027823, 103: -0.04537811427940806, 104: -0.0145753476679099, 105: -0.051384132762039306, 106: -0.03238532981363419, 107: -0.009626461670841913, 108: -0.05142271503568396, 109: -0.03298321035403368, 110: -0.03347743145251076, 111: -0.044794250260707914, 112: -0.03470833308863527, 113: -0.02764333929710539, 114: -0.024311174426352326, 115: -0.04084075400348757, 116: -0.03372462355441196, 117: -0.032271602731838225, 118: -0.011886338787194384, 119: -0.05706670564669299, 120: -0.026398960425466692, 121: -0.04901327311033041, 122: -0.019354559208290316, 123: -0.03688213072443512, 124: -0.047039699436994785, 125: -0.036572712227045494, 126: -0.04193022088072711, 127: -0.03532800297054567, 128: -0.02953042933624793, 129: -0.03866723234318407, 130: -0.035577924698346905, 131: -0.034945067548079176, 132: -0.033991123953095635, 133: -0.030603222955766566, 134: -0.027897694442905113, 135: -0.03342112456797481, 136: -0.04696340632771467, 137: -0.004274415858334272, 138: -0.05246400511217966, 139: -0.02748912978585688, 140: -0.04208410901037187, 141: -0.022948010280800273, 142: -0.05425293919529483, 143: -0.025579200802965146, 144: -0.05044990377880885, 145: 0.0068282859826107045, 146: -0.03476983041260587, 147: -0.0451709091870853, 148: -0.04389203842611901, 149: -0.04414913082400231, 150: 0.03266262190163491, 151: 0.04536819785293069, 152: 0.03281434649107753, 153: 0.03738203875493892, 154: 0.021577412447683526, 155: 0.05203541214426493, 156: 0.04052347233515629, 157: 0.0430867659132176, 158: 0.02916463828656809, 159: 0.06092223328101804, 160: 0.023210921316550535, 161: 0.047876002574017355, 162: 0.026601127338873418, 163: 0.03389520969983223, 164: 0.0327821288960849, 165: 0.017954371186338983, 166: 0.02290971207556882, 167: 0.05382377404047995, 168: 0.039265412379877546, 169: 0.04596277945431912, 170: 0.04184941096077009, 171: 0.020440627174710742, 172: 0.035837037890128, 173: 0.030906535778910116, 174: 0.01646457301098387, 175: 0.009709704025368537, 176: 0.03828890010375844, 177: 0.029309552728668923, 178: 0.01046079619637862, 179: 0.03902612162796388, 180: 0.05624909222085309, 181: 0.03394318216253972, 182: 0.03924729663527593, 183: 0.04479147999573592, 184: 0.04396195097084499, 185: 0.022673424212902908, 186: 0.03372895316820565, 187: 0.08899681108336517, 188: -0.032955937159287796, 189: 0.031337769386226344, 190: 0.03893343903019695, 191: 0.05737386852814959, 192: 0.04672914327184863, 193: 0.0690133473127918, 194: 0.052538050717873916, 195: 0.0607837028614624, 196: 0.056894238993352, 197: 0.03390634428782813, 198: 0.029185865863204517, 199: 0.023115339948379363}, 'L1': {0: 'intercept', 1: 'intercept', 2: 'intercept', 3: 'intercept', 4: 'intercept', 5: 'intercept', 6: 'intercept', 7: 'intercept', 8: 'intercept', 9: 'intercept', 10: 'intercept', 11: 'intercept', 12: 'intercept', 13: 'intercept', 14: 'intercept', 15: 'intercept', 16: 'intercept', 17: 'intercept', 18: 'intercept', 19: 'intercept', 20: 'intercept', 21: 'intercept', 22: 'intercept', 23: 'intercept', 24: 'intercept', 25: 'intercept', 26: 'intercept', 27: 'intercept', 28: 'intercept', 29: 'intercept', 30: 'intercept', 31: 'intercept', 32: 'intercept', 33: 'intercept', 34: 'intercept', 35: 'intercept', 36: 'intercept', 37: 'intercept', 38: 'intercept', 39: 'intercept', 40: 'intercept', 41: 'intercept', 42: 'intercept', 43: 'intercept', 44: 'intercept', 45: 'intercept', 46: 'intercept', 47: 'intercept', 48: 'intercept', 49: 'intercept', 50: 'controls', 51: 'controls', 52: 'controls', 53: 'controls', 54: 'controls', 55: 'controls', 56: 'controls', 57: 'controls', 58: 'controls', 59: 'controls', 60: 'controls', 61: 'controls', 62: 'controls', 63: 'controls', 64: 'controls', 65: 'controls', 66: 'controls', 67: 'controls', 68: 'controls', 69: 'controls', 70: 'controls', 71: 'controls', 72: 'controls', 73: 'controls', 74: 'controls', 75: 'controls', 76: 'controls', 77: 'controls', 78: 'controls', 79: 'controls', 80: 'controls', 81: 'controls', 82: 'controls', 83: 'controls', 84: 'controls', 85: 'controls', 86: 'controls', 87: 'controls', 88: 'controls', 89: 'controls', 90: 'controls', 91: 'controls', 92: 'controls', 93: 'controls', 94: 'controls', 95: 'controls', 96: 'controls', 97: 'controls', 98: 'controls', 99: 'controls', 100: 'controls', 101: 'controls', 102: 'controls', 103: 'controls', 104: 'controls', 105: 'controls', 106: 'controls', 107: 'controls', 108: 'controls', 109: 'controls', 110: 'controls', 111: 'controls', 112: 'controls', 113: 'controls', 114: 'controls', 115: 'controls', 116: 'controls', 117: 'controls', 118: 'controls', 119: 'controls', 120: 'controls', 121: 'controls', 122: 'controls', 123: 'controls', 124: 'controls', 125: 'controls', 126: 'controls', 127: 'controls', 128: 'controls', 129: 'controls', 130: 'controls', 131: 'controls', 132: 'controls', 133: 'controls', 134: 'controls', 135: 'controls', 136: 'controls', 137: 'controls', 138: 'controls', 139: 'controls', 140: 'controls', 141: 'controls', 142: 'controls', 143: 'controls', 144: 'controls', 145: 'controls', 146: 'controls', 147: 'controls', 148: 'controls', 149: 'controls', 150: 'controls', 151: 'controls', 152: 'controls', 153: 'controls', 154: 'controls', 155: 'controls', 156: 'controls', 157: 'controls', 158: 'controls', 159: 'controls', 160: 'controls', 161: 'controls', 162: 'controls', 163: 'controls', 164: 'controls', 165: 'controls', 166: 'controls', 167: 'controls', 168: 'controls', 169: 'controls', 170: 'controls', 171: 'controls', 172: 'controls', 173: 'controls', 174: 'controls', 175: 'controls', 176: 'controls', 177: 'controls', 178: 'controls', 179: 'controls', 180: 'controls', 181: 'controls', 182: 'controls', 183: 'controls', 184: 'controls', 185: 'controls', 186: 'controls', 187: 'controls', 188: 'controls', 189: 'controls', 190: 'controls', 191: 'controls', 192: 'controls', 193: 'controls', 194: 'controls', 195: 'controls', 196: 'controls', 197: 'controls', 198: 'controls', 199: 'controls'}})

ggplot(aes(x='variable', y='value'), data=df.loc[
        (df['L1'].isin(['controls','intercept']))]) + \
                 stat_summary(fun_y=np.mean,
                              fun_ymin=np.min,
                              fun_ymax=np.max) + \
                 coord_flip() + \
                 geom_text(aes(x='variable', y='value',
                               label=round(df.loc[(df['L1'].isin(['controls','intercept']))].groupby(
                                   'variable').mean().reset_index().value, 2),
                               size=8
                               ),
                           color='red',
                           data=df.loc[(df['L1'].isin(['controls','intercept']))].groupby('variable').mean().reset_index(),
                           position=position_dodge(width=0.8)) + \
                 ggtitle('Controls') 

我使用plotnine. 我想做的是两件事:

1)稍微移动标签,使它们不会落在线上

2) 在行中添加第 25 和第 75 分位数

任何想法如何使用plotnine

4

1 回答 1

1

这是实现所需输出的一种方法:

  • 添加stat_boxplot(geom='errorbar', coef=1)以在 IQR 的 1.0 倍处绘制垂直刻度,即在第 25 个和第 75 个百分位数处
  • 传给nudge_x = 0.12_geom_text

新代码:

ggplot(aes(x='variable', y='value'), data=df.loc[
        (df['L1'].isin(['controls','intercept']))]) + \
                 stat_summary(fun_y=np.mean,
                              fun_ymin=np.min,
                              fun_ymax=np.max) + \
                 stat_boxplot(geom='errorbar',
                              coef=1) + \
                 coord_flip() + \
                 geom_text(aes(x='variable', y='value',
                               label=round(df.loc[(df['L1'].isin(['controls','intercept']))].groupby(
                                   'variable').mean().reset_index().value, 2),
                               size=8
                               ),
                           color='red',
                           data=df.loc[(df['L1'].isin(['controls','intercept']))].groupby('variable').mean().reset_index(),
                           position=position_dodge(width=0.8),
                           nudge_x = 0.12) + \
                 ggtitle('Controls') 

输出:

新的 plotnine 输出

于 2018-09-28T15:10:33.883 回答