首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python plotnine添加矩形区域

Python plotnine添加矩形区域
EN

Stack Overflow用户
提问于 2021-02-04 18:22:03
回答 1查看 83关注 0票数 1

我已经开始使用plotnine了,我想创建一个红色区域在40以上,蓝色区域在-40以下的图表。

chart with red and blue rectangles

我可以用下面的代码来近似它,但它看起来很粗糙。做这件事的“正确”方法是什么?

代码语言:javascript
复制
import pandas as pd
from plotnine import *

vals = np.random.randint(-50, 50, size=20)
df = pd.DataFrame({"val":vals})
ggplot(df, aes(x=df.index, y = 'val')) \
   + geom_line() \
   + geom_hline(yintercept=40, size=20, colour='red', alpha=0.5) \
   + geom_hline(yintercept=-40, size=20, colour='blue', alpha=0.5) 
EN

回答 1

Stack Overflow用户

发布于 2021-02-05 17:44:41

您可以使用geom geom_rect()通过plotnine添加一个矩形区域。

代码语言:javascript
复制
+geom_rect(data = dfwithnumbersyoulike, aes(xmin = yourxmin, xmax = yourxmax, ymin = yourymin, ymax = yourymax, fill = #ff0000, alpha = 0.7)) #red
+geom_rect(data = dfwithnumbersyoulike2, aes(xmin = yourxmin2, xmax = yourxmax2, ymin = yourymin2, ymax = yourymax2, fill = #0000ff, alpha = 0.7)) #blue

(或者只是将geom_hline%s转换为在40之上和-40之下)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66043654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档