Altair: I want to plot multi layer area plot with gradient.

Created on 4 Apr 2020  路  6Comments  路  Source: altair-viz/altair

I want to plot multilayer area plot with gradient. But as far I found there is no way to do that without compounding charts. When charts are compounded I need to create a separate legend for the chart. Is there way that I can plot a multilayer area plot with gradient including the legend without compounding. I used below function to plot single layer area chart

```def Charts(clm):
chart=alt.Chart(df_melt[df_melt.Legend==clm]).mark_area(line={'color':'blue'},
color=alt.Gradient( gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='blue', offset=1)] ,
x1=1,
x2=1,
y1=1,
y2=0)
).encode(
x='Days',
y='% of Population:Q',

    tooltip='% of Population',
    opacity=alt.value(.5)).properties(
    height=300,
    width=730).interactive()
return chart `
question

All 6 comments

Out put should look like this. Points are not required.
VNMor

It's possible, with some manual configuration of gradients in each layer. Drawing from https://altair-viz.github.io/gallery/area_chart_gradient.html, you could do something like this:

import altair as alt
from vega_datasets import data

source = data.stocks()

def stock_gradient(symbol, color):
  return alt.Chart(source).transform_filter(
      f'datum.symbol==="{symbol}"'
  ).mark_area(
      line={'color': color},
      color=alt.Gradient(
          gradient='linear',
          stops=[alt.GradientStop(color='white', offset=0),
                alt.GradientStop(color=color, offset=1)],
          x1=1,
          x2=1,
          y1=1,
          y2=0
      )
  ).encode(
      alt.X('date:T'),
      alt.Y('price:Q')
  )

alt.layer(
    stock_gradient('GOOG', 'darkgreen'),
    stock_gradient('AAPL', 'darkblue'),
    stock_gradient('MSFT', 'darkorange')
)

visualization - 2020-04-04T061853 391

Thank you for the quick response. But still, I have to add legend manually. Is there another way?

Oh, your example chart didn't have a legend, so I didn't think you wanted one. You can add a legend by adding a stroke encoding:

import altair as alt
from vega_datasets import data

source = data.stocks()

def stock_gradient(symbol, color):
  return alt.Chart(source).transform_filter(
      f'datum.symbol==="{symbol}"'
  ).mark_area(
      line={'color': color},
      color=alt.Gradient(
          gradient='linear',
          stops=[alt.GradientStop(color='white', offset=0),
                alt.GradientStop(color=color, offset=1)],
          x1=1,
          x2=1,
          y1=1,
          y2=0
      )
  ).encode(
      alt.X('date:T'),
      alt.Y('price:Q'),
  )

alt.layer(
    stock_gradient('GOOG', 'darkgreen'),
    stock_gradient('AAPL', 'darkblue'),
    stock_gradient('MSFT', 'darkorange')
).encode(
    alt.Stroke('symbol', scale=alt.Scale(domain=['GOOG', 'AAPL', 'MSFT'], range=['darkgreen', 'darkblue', 'darkorange']))
)

Just what I needed. Thank you very much. Love Altair and your books @jakevdp .

download (1)

But still legend is filled with green, can we fill the legend with white color?

Was this page helpful?
0 / 5 - 0 ratings