我已经开始使用plotnine了,我想创建一个红色区域在40以上,蓝色区域在-40以下的图表。
chart with red and blue rectangles
我可以用下面的代码来近似它,但它看起来很粗糙。做这件事的“正确”方法是什么?
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) 发布于 2021-02-05 17:44:41
您可以使用geom geom_rect()通过plotnine添加一个矩形区域。
+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之下)
https://stackoverflow.com/questions/66043654
复制相似问题