如何阅读/解释plotnine文档

2024-05-15 04:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

俗话说:“给一个人一条鱼,他们一天都不会饿。教他们如何钓鱼,他们一辈子都不会饿。”

就我个人而言,我无法解释plotnine文件。这根本不是对开发该软件包的优秀人员的批评——我真的很喜欢该软件包,这就是为什么我要努力变得更好,我真的很感激他们

但我无法理解如何将文档应用到我的工作中。我不得不花很长时间在谷歌上搜索某人,他有一个使用我想要实现的功能的工作示例,这可能非常耗时

例如,在下图中插入箭头的任务

文件如下:

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

Tags: 文件dftypeopenlengthguideplotninelast
1条回答
网友
1楼 · 发布于 2024-05-15 04:31:01

您错过了帮助页面中的一个重要部分:

This is used to define arrow heads for geom_path.

因为x是一个因子,所以我不太确定在绘图中如何使用它,所以我在下面展示了一个箭头如何工作的示例。您还可以检查help page的geom_路径:

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

enter image description here

对于图例,您需要指定要修改的图例,在您的情况下是:

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))
)

enter image description here

相关问题 更多 >

    热门问题