Python is a general-purpose, high-level language used here to analyze data, create visualizations, and carry a data science project from experimentation through reporting. section 1.2 section 1.2.1
Python supports practical data storytelling in two complementary ways:
- Use imperative libraries such as Matplotlib and Seaborn by specifying visualization steps, including axes, labels, titles, and styling. section 1.2
- Use declarative Altair by describing the data, variables, chart type, and desired result while the library handles much of the construction. section 1.2
- Keep analysis and reporting in Python instead of repeatedly exporting data to Tableau or Power BI during project revisions. section 1.2.1
Plotting square numbers
A line chart for x values 1 through 5 and y values 1, 4, 9, 16, and 25 can be built imperatively with Matplotlib or declaratively with Altair. The Matplotlib version specifies plot, title, axis labels, and display; the Altair version places the same data in a pandas DataFrame, encodes x and y, selects mark_line, and saves the chart. section 1.2
Listing 1.1 Imperative library
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.title('Square Numbers')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Inspect the step-by-step Matplotlib implementation of the square-numbers line chart. section 1.2
Listing 1.2 Declarative library
import altair as alt
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [1, 4, 9, 16, 25]})
chart = alt.Chart(df).mark_line().encode(
x='x',
y='y'
).properties(
title='Square Numbers'
)
chart.save('chart.png')
Inspect the concise Altair implementation using a DataFrame, encodings, a line mark, and a chart title. section 1.2

























