question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I want to plot multi layer area plot with gradient.

See original GitHub issue

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

    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 `

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Apr 4, 2020

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']))
)
1reaction
jakevdpcommented, Apr 4, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Create a Gradient Area Chart in Tableau (Kizley ...
The gradient area chart can be used to display any measure over time provided all values are on the same side of the...
Read more >
Create a Gradient Plot in Excel - PolicyViz
Learn how to create a gradient chart in Excel using stacked bar/column charts and a scatterplot. An application to a ProPublica story as ......
Read more >
A Complete Guide to Area Charts | Tutorial by Chartio
Area charts combine the line chart and bar chart for more specific purposes. Use this guide to learn the best times and ways...
Read more >
Is it possible to get color gradients under curve in matplotlib?
His method opens to door to many gradient/blur/drop-shadow effects. For example, to make the lines have an evenly blurred underside, you could use...
Read more >
Set the following fill effects in the active chart: Gradient fill
Set the following fill effects in the active chart : Gradient fill: color 1: red and color 2: white, background 1Type: Linear and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found