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

centred text marks on sorted heat map #1263

Closed
Alcampopiano opened this issue Dec 10, 2018 · 8 comments
Closed

centred text marks on sorted heat map #1263

Alcampopiano opened this issue Dec 10, 2018 · 8 comments

Comments

@Alcampopiano
Copy link

Hi Jake,

I assume that Altair 2.3 does not yet have the VL 3 sorting functionality mentioned here.

I am following a workaround involving making the x axis independent. This fixes the sorting but makes the text marks misaligned.

altair version 2.3

se=pd.DataFrame({'estimator': ['mean', 'mean', 'mean', 'mean',
                                'median', 'median', 'median', 'median',
                                'trim_mean', 'trim_mean', 'trim_mean', 'trim_mean'], 
                  'distribution': ['normal', 'lognormal', 'one-wild', 'slash',
                                  'normal', 'lognormal', 'one-wild', 'slash',
                                  'normal', 'lognormal', 'one-wild', 'slash'],
                  'se': [.1, .4658, 1.90, np.nan,
                        .138, .1727, .1679, .7048,
                        .113, .177, .1433, .9649]})


c=alt.Chart().encode(
    y=alt.Y('estimator'),
    tooltip=['estimator', 'distribution', 'se']
)

a=c.mark_rect().encode(
    x=alt.X('distribution', sort=['normal', 'lognormal', 'one-wild', 'slash'], axis=alt.Axis(labelAngle=0)),
    color=alt.Color('se', scale=alt.Scale(domain=[0,.5]))
)

b=c.mark_text(align='center').encode(
    x=alt.X('distribution', sort=['normal', 'lognormal', 'one-wild', 'slash'], 
            axis=alt.Axis(title=None, labels=False, ticks=False)),
    text='se',
)

alt.layer(a,b,data=se).resolve_scale(x='independent')

visualization 61

Is there a way to centre the text while maintaining the custom sort?

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 11, 2018

I assume that Altair 2.3 does not yet have the VL 3 sorting functionality mentioned here.

No, Altair 2.3 is still tied to Vega-Lite 2.6, as Vega-Lite 3 has yet to be released:

>>> import altair as alt
>>> alt.VEGALITE_VERSION
'2.6.0'

Regarding your question, I'm not sure how to answer it. When I run your code I get the following:
visualization 15

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 11, 2018

Here is an example that is similar to what you want: starting from your code snippet, I removed the resolve_scale(x='independent') and added baseline='middle' to the text mark:

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

se=pd.DataFrame({'estimator': ['mean', 'mean', 'mean', 'mean',
                                'median', 'median', 'median', 'median',
                                'trim_mean', 'trim_mean', 'trim_mean', 'trim_mean'], 
                  'distribution': ['normal', 'lognormal', 'one-wild', 'slash',
                                  'normal', 'lognormal', 'one-wild', 'slash',
                                  'normal', 'lognormal', 'one-wild', 'slash'],
                  'se': [.1, .4658, 1.90, np.nan,
                        .138, .1727, .1679, .7048,
                        .113, .177, .1433, .9649]})


c=alt.Chart().encode(
    y=alt.Y('estimator'),
    tooltip=['estimator', 'distribution', 'se']
)

a=c.mark_rect().encode(
    x=alt.X('distribution', sort=['normal', 'lognormal', 'one-wild', 'slash'], axis=alt.Axis(labelAngle=0)),
    color=alt.Color('se', scale=alt.Scale(domain=[0,.5]))
)

b=c.mark_text(align='center', baseline='middle').encode(
    x=alt.X('distribution', sort=['normal', 'lognormal', 'one-wild', 'slash'], 
            axis=alt.Axis(title=None, labels=False, ticks=False)),
    text='se',
)

alt.layer(a,b,data=se)

visualization 16

@Alcampopiano
Copy link
Author

Alcampopiano commented Dec 11, 2018

Thanks Jake. Yes, I should have mentioned that my above example was made in the traditional notebook (so I can use RISE). Perhaps that is why it shows up oddly. In JL I get the same as you.

Thanks again.

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 11, 2018

It might be that you're using an older version of the vega extension in the notebook, in which case an older version of vega-lite is being used to render your chart.

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 11, 2018

The rendering should be identical between notebook and jupyterlab if the vega extensions are up to date.

@Alcampopiano
Copy link
Author

Hmm, I have Vega-Lite 2.6.0 and Altair 2.3.0 in the notebook. Does that tell us that I have the vega extension up-to-date?

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 11, 2018

The relevant package for the rendering of charts is not Altair, but the notebook vega extension.

It's actually quite difficult to figure out what vega-lite version is being used by the notebook to render your charts (see vega/ipyvega#97 for the open issue on this subject... cc/ @domoritz: this is an example of a situation where that enhancement would be helpful to users).

You need to update the vega package in order to use the most recent vega-lite version... version 1.4 is the most recent version.

@Alcampopiano
Copy link
Author

Thanks Jake. Will do.

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

No branches or pull requests

2 participants