import altair as alt import pandas as pd # 1. Data data = pd.DataFrame({'a': ['A', 'B', 'C'], 'b': [28, 55, 43]}) # 2. & 3. Chart + Mark + Encoding chart = alt.Chart(data).mark_bar().encode( x='a', y='b' ) # Display (if in notebook) # chart.show() Use code with caution. Copied to clipboard 3. Data Transformation & Aggregation
# Create and activate a virtual environment python -m venv altair-venv source altair-venv/bin/activate # On Windows: altair-venv\Scripts\activate # Install Altair and dependencies python -m pip install altair pandas notebook Use code with caution. Copied to clipboard 2. Core Concepts: The Chart Object Every Altair chart follows three basic steps: Pass a pandas DataFrame to alt.Chart() . altair
Altair works best with tidy data—long-form data where each row is an observation and each column is a variable. import altair as alt import pandas as pd # 1
Choose the chart type (e.g., mark_point() , mark_bar() , mark_line() ). Chart + Mark + Encoding chart = alt
You can save your chart as a JSON file (Vega-Lite spec) or render it as an image/HTML file. chart.save('chart.html') Use code with caution. Copied to clipboard
If your data relies on a pandas index, use .reset_index() before passing it to Altair.