Altair: vary the color of a line with points overlayed

Created on 11 Jun 2018  路  6Comments  路  Source: altair-viz/altair

Hi Altair users,

Loving Altair but I have a question. How would I have a line mark that varies in color and has the circle marks overlayed? The following does not show the line:

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

x = np.arange(100)
data = pd.DataFrame({'x': x,
                     'sin(x)': np.sin(x / 5)})

c=alt.Chart(data).encode(
    x='x',
    y='sin(x)',
    color='sin(x)'
)

c.mark_circle() + c.mark_line()

visualization
Am I doing something wrong here? I have also tried the trail marker. This shows the same as the attached image.

In fact the chart is empty if I only use the line mark here.

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

x = np.arange(100)
data = pd.DataFrame({'x': x,
                     'sin(x)': np.sin(x / 5)})

c=alt.Chart(data).encode(
    x='x',
    y='sin(x)',
    color='sin(x)',

)

c.mark_line()

visualization 1

Thanks for the great package.

Al

enhancement vega-lite-related

Most helpful comment

Thanks Jake. I will hack in that way if needed. I mentioned it to the Vega people.

Take care,
Al

All 6 comments

I don't know of a good solution off-hand, but I can tell you why you see no line.

Under the hood, encodings are like arguments to a group-by... So you are grouping points by the value of sin(x), which results in a bunch of groups of size 1. You then ask for a line to be drawn within each of these groups. A line through 1 point is nothing, so the result is nothing.

Thanks Jake, that makes sense. This is why I thought the trail mark might be better since it allows size to vary along a line. But unfortunately not color at this point as far as I can tell. It seems that so far the only way to vary color is by using marks that are discrete (bar, points, circles, etc...). I will ask the Vega-lite folks as well.

Thanks very much for the wonderful tool.

Allan

You could probably hack it by manually constructing line segments containing two points each, duplicating the data between start and end point. But that's not particularly convenient.

I agree that making the trail mark support color would be the natural way to support this within the vega-lite grammar. It might be worth a feature request to vega-lite!

Thanks Jake. I will hack in that way if needed. I mentioned it to the Vega people.

Take care,
Al

You can use mark_trail to have lines that vary some of their attributes (like their thickness), though this doesn't seem to work for color...

To make any (upstream) progress on this issue easier to track, here are a few useful links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

morberg picture morberg  路  3Comments

fischcheng picture fischcheng  路  4Comments

Juan-132 picture Juan-132  路  3Comments

maxgerma picture maxgerma  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments