微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Plotnine/ggplot 多维箱线图或抖动图

如何解决Plotnine/ggplot 多维箱线图或抖动图

我正在尝试使用 ggplot/plotnine 制作一个特定的情节。如果可能的话,我想保持它的情节性,这样我所做的一切都可以留在同一个笔记本中。

本质上,我想创建一个盒/抖动图,允许按类别分离或按类别进一步抖动。

我使用的数据评估了 4 个不同的模型及其基于特定指标的百分比误差。数据结构如下。

数据:

    Model   metric  Percent Error
0   gbr Lower   46.533009
1   gbr Lower   22.654213
2   gbr Lower   17.404358
3   gbr Lower   5.134485
4   gbr Lower   4.550838
... ... ... ...
9963    cqrn    Average 5.745320
9964    cqrn    Average 16.465810
9965    cqrn    Average 14.737193
9966    cqrn    Average 81.743560
9967    cqrn    Average 73.008793

代码

(ggplot(dat,aes(x="metric",y="Percent Error",color = "Model")) +
geom_jitter(width = .25,alpha=.4,show_legend=False) +
scale_y_log10() +
labs(title=f"Error Metrics"))

代码呈现:

enter image description here

我想要一个看起来像这样的图表(抱歉画的粗糙)。这可能会因箱线图或抖动而下降 - 尽管如果可以的话,抖动会得到加分!

enter image description here

解决方法

这可以通过设置使用 position = position_jitterdodge() 来实现:

注意:我稍微更改了您提供的示例数据,以使示例更逼真。

dat = [['gbr','Lower',46.533009],['gbr',22.654213],17.404358],['cqrn',5.134485],4.550838],'Average',5.745320],16.465810],14.737193],81.743560],73.008793]]

dat = pd.DataFrame(dat,columns = ['Model','metric','Percent Error'])

(ggplot(dat,aes(x="metric",y="Percent Error",color = "Model")) +
geom_jitter(alpha=.4,position = position_jitterdodge(jitter_width = .25)) +
scale_y_log10() +
labs(title="Error Metrics"))

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。