-
Notifications
You must be signed in to change notification settings - Fork 794
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
ordering of legend labels #1059
Comments
You can specify the order using the import altair as alt
from vega_datasets import data
stocks = data.stocks()
alt.Chart(stocks).mark_line().encode(
x='date',
y='price',
color=alt.Color('symbol', sort=['GOOG'])
) |
Oh this is great. I should have guessed that. Thanks very much. |
Hi Jake, I'm trying to follow your instructions here for reording the legend labels. For some reason the "stock prices" example works, but when using my own data I get an error despite the same data structure and Altair code. What am I missing here? Note that the json works fine in the Vega editor for both examples. this worksimport altair as alt
from vega_datasets import data
stocks = data.stocks()
alt.Chart(stocks).mark_line().encode(
x='date',
y='price',
color=alt.Color('symbol', sort=['GOOG', 'IBM'])
) this doesn't(I'm hiding some of the data here but you can see the structure) alt.Chart(data_melt).mark_line().encode(
x='year',
y='percentage of students',
color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
)
Javascript Error: Expression parse error: "datum.percent categories === 'applied to university only' ? 0 : datum.percent categories === 'applied to college only' ? 1 : 2". This usually means there's a typo in your chart specification. See the JavaScript console for the full traceback. |
I can't reproduce this error. If I run the following it works without an issue: import altair as alt
import pandas as pd
data = pd.DataFrame({'year': range(2010, 2016),
'percentage of students': range(10, 16),
'percent categories': 3 * ['applied to university only', 'applied to college only']})
alt.Chart(data).mark_line().encode(
x='year',
y='percentage of students',
color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
) |
How odd. If I run the code you just sent I get the same error. |
You may need to update your frontend to get a newer version of vega/vega-lite. |
This error is not coming from Python, it is coming from Javascript, so it is the javascript library version that is relevant here. That library version is not controlled by Altair, but controlled by the extension your frontend (jupyterlab, jupyter notebook, colab, etc.) is using to render Altair's outputs. |
Okay thanks. I will upgrade JL and try again. |
I updated JL and your example works but only if no layering present. If I layer some dots, the legend goes back to its original ordering. import altair as alt
import pandas as pd
data = pd.DataFrame({'year': range(2010, 2016),
'percentage of students': range(10, 16),
'percent categories': 3 * ['applied to university only', 'applied to college only']})
base=alt.Chart(data).encode(
x='year:O',
y='percentage of students',
color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
)
line=base.mark_line()
dot=base.mark_circle()
both=line+dot
both |
That is a known issue in Vega-Lite: see vega/vega-lite#2177 and #820 |
Ah I see. Thanks for letting me know. |
… improvements to demonstrate column and legend sorting. Documentation includes two new examples showing how encoded channels can be sorted based on their data type or with an explicit order.
This should be fixed as of Altair 4.0 |
Hi Jake,
Can we change around the order of the legend entries?
Say, put Google at the top in this example from the docs:
The text was updated successfully, but these errors were encountered: