Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Chart with Custom Confidence Interval in Altair

Suppose i have the data frame below:

Selection_101

I checked the documentation but it's only based on a single column.

Reproducible code:

x = np.random.normal(100,5,100)
data = pd.DataFrame(x)
epsilon = 10
data.columns = ['x']
data['lower'] = x - epsilon
data['upper'] = x + epsilon
data

I'd actually like to use altair, since i like it's interactivity.

like image 426
Benj Cabalona Jr. Avatar asked Mar 12 '20 07:03

Benj Cabalona Jr.


People also ask

How to set the confidence interval of a chart line?

You can use forecast function under Analytics pane to set confidence interval: However, t he forecasting feature is only available for line chart visuals so that you could not show shaded areas in chart line instead of using Area chart. If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

What are the advantages of using Altair?

The most significant thing about creating visualizations in Altair is that you can disengage the style of the graph, whether line or scatter or bar (called the mark) from the undisputable encoding of the data that can be Categorical (Nominal or Ordered) or Quantitative or Temporal and so on….

What is Transform_filters function of Altair?

The transform_filters function of Altair, reduces the dataframe to the selection of stock and “nearest” Date on x-axis chosen. The output is the graph seen at the top of the article where only the stock selected is charted with color and the tooltip shows its price, whereas the rest and sent to the background in grey.


Video Answer


1 Answers

You can layer a line and an area chart, usng the y and y2 encodings to specify the range:

import altair as alt
import pandas as pd
import numpy as np

x = np.random.normal(100,5,100)
epsilon = 10
data = pd.DataFrame({
    'x': x,
    'lower': x - epsilon,
    'upper': x + epsilon
}).reset_index()

line = alt.Chart(data).mark_line().encode(
    x='index',
    y='x'
)

band = alt.Chart(data).mark_area(
    opacity=0.5
).encode(
    x='index',
    y='lower',
    y2='upper'
)

band + line

enter image description here

like image 178
jakevdp Avatar answered Nov 15 '22 09:11

jakevdp