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

Axis name breaks chart on HTML export but not in notebook #993

Closed
dcbb opened this issue Jul 3, 2018 · 4 comments
Closed

Axis name breaks chart on HTML export but not in notebook #993

dcbb opened this issue Jul 3, 2018 · 4 comments

Comments

@dcbb
Copy link

dcbb commented Jul 3, 2018

I have built a chart where renaming an axis breaks the HTML export of the chart, but not the display in a Jupyter notebook. When I open the exported HTML file, the error message from the error handler is displayed. Strangely, when I copy the pure chart dictionary from the HTML into the Vega Lite web editor, things work and the chart displays fine.

Apologies for the complex example. I could not reproduce the error with a simpler one, and I need the data generator because I cannot share my original data.

This is the version, where the output (test.html) is broken, while the chart displays fine in a notebook:

    def generate_data():
        N = 100
        for geo in 'ABC':
            for platform in 'PQ':
                y = np.cumsum(np.random.normal(size=N))
                x = np.arange(N)
                data = pd.DataFrame({'x': x,
                                     'mean': y,
                                     'q25': y - 0.1*x,
                                     'ch': y + 0.1*x,
                                     'cl2': y - 0.05*x,
                                     'ch2': y + 0.05*x,
                                    })
                data['geo'] = geo
                data['platform'] = platform
                yield data
                
    chart = alt.Chart().mark_area(opacity=0.3).encode(x='x:Q', y='q25:Q', y2='ch:Q')
    chart += alt.Chart().mark_area(opacity=0.3).encode(x='x:Q', y='cl2:Q', y2='ch2:Q')
    chart += alt.Chart().mark_line().encode(x='x:Q', 
                                            y=alt.Y('mean:Q', title='RENAMED'), 
                                            tooltip=['geo:N','x:Q','mean:Q'])
    chart = chart.facet(data=pd.concat(list(generate_data())), row='geo:N', column='platform:N')
    chart.save(io.reports_path('test.html'))
    chart

The export works again when I replace alt.Y('mean:Q', title='RENAMED') in the mark_line() part with 'mean:Q', not renaming the axis.

I'm working on fresh (yesterday) Conda install of Altair.

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 3, 2018

It might be that you are using a frontend renderer (e.g. JupyterLab/Jupyter notebook) that has an older version of Vega-Lite, which doesn't support title as an encoding argument.

What happens if you change y=alt.Y('mean:Q', title='RENAMED') to y=alt.Y('mean:Q', axis=alt.Axis(title='RENAMED'))?

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 3, 2018

If you open the javascript console, I suspect you'll see an error along the lines of

 The input spec uses vega-lite v2.5.2, but the current version of Vega-Lite is 2.4.1.

if so, this is the cause of the problem. title as an encoding argument was added in vega-lite 2.5. Note that the version of Vega-Lite used to render Altair plots is not controlled by Altair, but by the renderer (e.g. the notebook extension or jupyterlab extension) so you'll have to update that.

@dcbb
Copy link
Author

dcbb commented Jul 4, 2018

@jakevdp The change you proposed fixes the export!

Also, in the HTML generated by Altair's save() method, we have:

 <script src="https://cdn.jsdelivr.net/npm//[email protected]"></script>

When I change this manually to

 <script src="https://cdn.jsdelivr.net/npm//[email protected]"></script>

things work with the original code I posted (with the title parameter).

Please note that the chart renders well in the notebook, also with the title parameter – how can this be if I have wrong version of Vega lite?

Also, where can I update my Vega lite version?

I just conda-installed everything, and I'm not really sure which installer installed which version of vega-lite, to be honest. A conda list | grep vega just gives me

 vega                      1.3.0                    py36_0    conda-forge
 vega_datasets             0.5.0                      py_0    conda-forge

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 4, 2018

The change you proposed fixes the export!

Great!

Please note that the chart renders well in the notebook, also with the title parameter – how can this be if I have wrong version of Vega lite?

As I mentioned above, the version of Vega-Lite used by your frontend is not controlled by Altair, but by the frontend renderer.

where can I update my Vega lite version?

If you're using Jupyter Notebook (not JupyterLab), the version of Vega-Lite used to render plots is defined in the vega extension, namely in https://github.com/vega/ipyvega/blob/master/package.json via https://github.com/vega/vega-embed/blob/master/package.json. I wish that were easier to access from Python; I've opened an issue regarding that: vega/ipyvega#97

If you update the vega package from pip, it should use the most recent vega-lite version.

For HTML export, the vega-lite version is defined by Altair itself, namely here: https://github.com/altair-viz/altair/blob/master/altair/vegalite/v2/schema/__init__.py#L4
You can access it with

>>> import altair
>>> altair.VEGALITE_VERSION
'2.5.2'

@jakevdp jakevdp closed this as completed Aug 9, 2018
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