亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 geom_hlines 圖例添加到 plotnine 的圖中?

如何將 geom_hlines 圖例添加到 plotnine 的圖中?

至尊寶的傳說 2022-06-28 16:41:04
我想在我的情節中添加一個圖例,其中包含所有 hline 的統計描述。有什么辦法嗎?def test_plot():Q1=test['age'].quantile(0.25)Q3=test['age'].quantile(0.75)IQR=Q3-Q1fig = (    ggplot(test) +    aes(x=arr,y='age')+    geom_point()+    labs(        title='Test',        x='Index',        y='Age',        )+    geom_hline(aes(yintercept =test.age.mean(),),color = 'gray')+    geom_hline(aes(yintercept =test.age.median()),color = 'green')+    geom_hline(aes(yintercept =IQR),color = 'blue')+    geom_hline(aes(yintercept =test['age'].quantile(0.1)),color= 'red')+    geom_hline(aes(yintercept =test['age'].quantile(0.9)),color= 'yellow')+    geom_hline(aes(yintercept =test['age'].std()),color= 'purple')    )
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

在大多數情況下,當您發現自己與圖例作斗爭時,這表明您正在繪制的數據沒有被有意義地排列。圖例旨在幫助解釋映射變量。在您的情況下,所有這些水平線都可以用一個變量表示,即“年齡統計”。


然后解決方案是將它們放入數據框中并使用一次調用,geom_hline以便繪圖系統可以處理圖例。


sdf = pd.DataFrame({

    'age_statistic': [

         'mean', 'median', IQR,

         '10th Percentile', '90th Percentile',

         'std'

    ],

    'value' : [

         test.age.mean(), test.age.median(), IQR,

         test['age'].quantile(0.1), test['age'].quantile(0.9),

         test['age'].std()

    ]

})


(ggplot(...)

 ...

 + geom_hline(sdf, aes(yintercept='value', colour='age_statistic'), show_legend=True)

)


查看完整回答
反對 回復 2022-06-28
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號