3b1b / 3b1b/manim

Best way to implement `fill_between` to display uncertainty?

Open
#1,063 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
93.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hello,

I would like to use something similar to `fill_between` that can be found in matplotlib. This is particularly useful to plot uncertainty.

I came up with a hacky solution but as I'm pretty new to the lib, I was wondering if there was a better way?

My solution (using a Polygon to fill the space between [mean - std, mean + std]:

```python
def fill_between(axes, graphs, color, opacity=1, dt=0.01, x_max=None):
points = []
if x_max is None:
x_max = axes.x_max
x_points = np.arange(axes.x_min, x_max, dt)
for x in x_points:
y = np.min([graph.underlying_function(x) for graph in graphs])
points.append(axes.coords_to_point(x, y))

for x in x_points[::-1]:
y = np.max([graph.underlying_function(x) for graph in graphs])
points.append(axes.coords_to_point(x, y))

filling_graph = Polygon(*points)
filling_graph.set_stroke(color, opacity=opacity)
filling_graph.set_fill(color, opacity=opacity)
return filling_graph
```
In matplotlib, I would do something like:

```python

plt.plot(x, mean, label='mean pred')
plt.fill_between(x, mean + std, mean - std, alpha=0.5)

```

EDIT: if the `stroke_width` could be function of the x value, that would solve this issue

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.