簡體   English   中英

如何閱讀/解釋 plotnine 文檔

[英]How to read/interpret plotnine documentation

俗話說:“授人以魚,一日不餓;授人以漁,終生不餓。”

在我的一生中,我無法解釋 plotnine 文檔。 這根本不是對開發 package 的優秀人士的批評——我真的很喜歡 package,這就是我努力變得更好的原因,我真的很感謝他們。

但我無法理解如何將文檔實施到我的工作中。 我必須花很長時間在谷歌上搜索一個使用我想要實現的功能的工作示例的人,這可能非常耗時。

以在下面的 plot 中插入箭頭的任務為例。

文檔在這里:

https://plotnine.readthedocs.io/en/stable/generated/plotnine.geoms.arrow.html

plotnine.geoms.arrow(angle=30, length=0.2, ends='last', type='open')

我嘗試了以下組合:

p + geoms_arrow(angle=30, length=0.2, ends='last', type='open')
p + geom_arrow(angle=30, length=0.2, ends='last', type='open')
p + geoms.arrow(angle=30, length=0.2, ends='last', type='open')
p + geom.arrow(angle=30, length=0.2, ends='last', type='open')
p + geoms('arrow',angle=30, length=0.2, ends='last', type='open')
p + geom('arrow',angle=30, length=0.2, ends='last', type='open')
p + arrow(angle=30, length=0.2, ends='last', type='open')

與此相關,我也無法理解如何更改圖例中的行數和列數。 文檔:

https://plotnine.readthedocs.io/en/stable/generated/plotnine.guides.guide_legend.html

plotnine.guides.guide_legend(**kwargs)

我嘗試了以下各種組合:

p + guides(guide_legend(nrow=1))

但無法得到任何工作。 我能找到實現guide_legend的唯一示例是:

http://www.danielrothenberg.com/blog/2017/Jul/declarative-visualization-in-python-update/

基於此,我還嘗試了:

+ guides(color=guide_legend(nrow=1))

我現在只是嘗試隨機的東西,沒有任何成功。

您如何破譯 plotnine 文檔來完成上述兩件事(即在下面的代碼中將圖例更改為 1 行 6 列,並插入一個箭頭)。

注意:我真的想了解如何閱讀文檔,而不僅僅是解決這兩個問題。 這也將幫助我解決許多其他問題......

一些示例代碼:

import pandas as pd
from plotnine import *

df = pd.DataFrame({})
df['cat1'] = ('1A', '1A', '1A', '1A', '1A', '1A', '1B', '1B', '1B', '1B', '1B', '1B', '1C', '1C', '1C', '1C', '1C', '1C')
df['cat2'] = ('2A', '2B', '2C', '2D', '2E', '2F', '2A', '2B', '2C', '2D', '2E', '2F', '2A', '2B', '2C', '2D', '2E', '2F')
df['value'] = (0.8965, 0.0579, 0.0250, 0.0119, 0.0060, 0.0027, 0.7645, 0.0989, 0.0456, 0.0319, 0.0268, 0.0322, 0.5889, 0.0947, 0.0819, 0.0772, 0.0707, 0.0866)

p = (ggplot(df, aes(x='cat1', y='value', fill='cat2'))
  + theme_light(8)
  + geom_bar(stat='identity',width=0.8, position='dodge', alpha=0.80)
  + theme(
        legend_direction='horizontal',
        legend_position='bottom',
  )
  + guides(guide_legend(nrow=1))
)
p

您錯過了幫助頁面中的一個重要部分:

這用於為 geom_path 定義箭頭。

不太確定如何在 plot 中使用它,因為 x 是一個因素,所以我在下面展示了箭頭如何工作的示例。 您還可以查看 geom_path 的幫助頁面

da = pd.DataFrame({'x':[1,2,3],'y':[4,5,6]})
p = (ggplot(da, aes(x='x', y='y'))
  + geom_path(arrow = arrow(angle=30, length=0.2, ends='last', type='open'))
)
p

在此處輸入圖像描述

對於圖例,您需要指定要修改的圖例,在您的情況下是:

p = (ggplot(df, aes(x='cat1', y='value', fill='cat2'))
  + theme_light(8)
  + geom_bar(stat='identity',width=0.8, position='dodge', alpha=0.80)
  + theme(
        legend_direction='horizontal',
        legend_position='bottom',
  )
  + guides(fill=guide_legend(nrow=1))
)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM