Matplotlib中类似ggplot的geom_ribbon?

3 投票
1 回答
3188 浏览
提问于 2025-04-18 08:11

我想要实现一种效果,就是在某个数值周围用带状图显示置信区间,像ggplot中的geom_ribbon或geom_smooth那样。

我尝试单独计算置信区间,然后用fill_between()来绘制,效果差不多,但感觉还是不太对。请问如何让x轴的样式像plot_date()那样,同时又能使用fill_between()?还有,带状图的平滑处理该怎么做呢?

下面是一个示例输出:

这里输入图片描述 这里输入图片描述

ggplot的代码大致是这样的(这是几个找到的例子):

qplot(wt, mpg, data=mtcars, colour=factor(cyl)) +
  geom_smooth(aes(ymin = lcl, ymax = ucl), data=grid, stat="identity")

ggplot(answers.overall, aes(Date, Answers)) + geom_line() + 
  geom_smooth(method="loess") + theme(axis.text.x = element_text(angle = 45, hjust = 1))

1 个回答

3

有没有什么原因让你不能在Python中使用ggplot?因为这样会让事情简单很多:

import ggplot as gg

mtcars = gg.mtcars
gg.qplot(mtcars.wt, mtcars.mpg) + gg.geom_smooth(color="blue")

撰写回答