Altair: create multiple lines with the same color?

Created on 28 Jun 2018  路  2Comments  路  Source: altair-viz/altair

I'd like to create a plot with several (~50) lines, each of which is one of two colors depending on a field of the data. Right now I can't figure out how to do this, since under the hood the color parameter is being used as the group-by to determine what points should fall on the same line.

Is it possible to specify a group-by manually for drawing lines so that I can have non-unique values for color?

question

Most helpful comment

You can use the detail channel, which affects data groups but does not change the appearance of those groups on the chart. For example:

data = pd.DataFrame({
    'x': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
    'y': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    'c': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
    'd': [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]
})

alt.Chart(data).mark_line().encode(
    x='x',
    y='y',
    color='c:O',
    detail='d'
)

visualization 26

All 2 comments

You can use the detail channel, which affects data groups but does not change the appearance of those groups on the chart. For example:

data = pd.DataFrame({
    'x': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
    'y': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    'c': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
    'd': [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]
})

alt.Chart(data).mark_line().encode(
    x='x',
    y='y',
    color='c:O',
    detail='d'
)

visualization 26

That is exactly perfect. Thank you so much for the quick response!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

galloramiro picture galloramiro  路  3Comments

mroswell picture mroswell  路  4Comments

pabloinsente picture pabloinsente  路  3Comments

nielsmde picture nielsmde  路  4Comments

Juan-132 picture Juan-132  路  3Comments