Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I draw a multiline graph from dataframe columns? #968

Closed
tweakimp opened this issue Jun 24, 2018 · 4 comments
Closed

How do I draw a multiline graph from dataframe columns? #968

tweakimp opened this issue Jun 24, 2018 · 4 comments
Labels

Comments

@tweakimp
Copy link

I have a dataframe with multiple columns similar to this one:

import pandas as pd
import altair as alt
df = pd.DataFrame([[x ** 2, x ** 3] for x in range(10)], columns=["squared", "cubed"])
df.index.name = "x"

# print(df)
   squared  cubed
x
0        0      0
1        1      1
2        4      8
3        9     27
4       16     64
5       25    125
6       36    216
7       49    343
8       64    512
9       81    729

How do I plot these columns in the same graph? So far I can only do this:

chart = alt.Chart(df.reset_index()).mark_line().encode(x="x", y="squared").interactive()

How can I add the cubed column to this? What about even more columns?
What I see in the multiline examples are other ways of storing the data by having two columns with one having all values and one telling where they come from.
Also, is there no other way to use the index of the dataframe as the x-axis other than to reset the index and then use it as a normal column?

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 26, 2018

One way to do this is using a layered chart:

base = alt.Chart(df.reset_index()).encode(x='x')

alt.layer(
    base.mark_line(color='blue').encode(y='squared'),
    base.mark_line(color='red').encode(y='cubed')
)

visualization 20

That requires manual specification of colors.

A better way to do this is to reshape the data using pd.melt() to combine the multiple columns into one:

data = df.reset_index().melt('x')
alt.Chart(data).mark_line().encode(
    x='x',
    y='value',
    color='variable'
)

visualization 21

See Long-form vs Wide-form data for more details on the data formats that are best supported by Altair.

@tweakimp
Copy link
Author

Thank you for your answer and keep up the good work :)

@jakevdp jakevdp closed this as completed Jun 26, 2018
@tareqhi
Copy link

tareqhi commented May 9, 2021

Great, It helped

@scastrodri
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants