marlon
marlon

Reputation: 7713

Altair doesn't display charts when running a script from terminal?

I am following this tutorial example on my Mac Pro Big Sur.

https://altair-viz.github.io/gallery/simple_bar_chart.html

vtest.py is below:

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
)

When I execute this on a terminal, it doesn't display anything, no visualization showing up, no error or warning message either.

% python vtest.py

Doesn't Altair work on Mac OS?

Upvotes: 5

Views: 4213

Answers (1)

joelostblom
joelostblom

Reputation: 49064

If you want to show an Altair plot by running a script from terminal, you can use the .show() method to open it in your default browser:

alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
).show()

The docs include a section with more details about how Altair charts are rendered and how to display them conveniently, starting with this passage:

Altair produces Vega-Lite visualizations, which require a Javascript frontend to display the charts. Because notebook environments combine a Python backend with a Javascript frontend, many users find them convenient for using Altair.

Upvotes: 4

Related Questions