Python gives the greatest freedom: any chart can be programmed, repeated exactly and combined with analysis and machine learning.

Main Libraries

Matplotlib is the foundation library; it draws almost any static chart through a figure and axes. Seaborn is built on Matplotlib and makes attractive statistical charts, such as histograms, box plots and heat maps, in one line. Plotly and Bokeh make interactive charts with hover and zoom for the web. Pandas can plot directly from a DataFrame, and Folium draws maps.

The Matplotlib Model

A plot is built from a Figure (the whole canvas) that contains one or more Axes (a single plot with its x and y axis). We call plotting methods on the axes: bar(), plot(), scatter(), hist(), and set the title, labels and ticks. The result is displayed with show() or saved with savefig() as PNG, SVG or PDF. Because the chart is code, it can be regenerated when the data change.

Strengths and Limits

Python is free, powerful, reproducible and suited to large data and automation. It needs programming skill, interactive dashboards need extra libraries such as Dash or Streamlit, and non-programmers cannot easily edit the result.

Definition: Matplotlib. The core Python plotting library that draws static charts using figure and axes objects, and on which Seaborn and the pandas plotting functions are built.

Table 2.4: Python visualization libraries

Library Strength Typical use
Matplotlib Full control, static charts Any basic chart, publication figures
Seaborn Statistical charts, good defaults Histograms, box plots, heat maps
Plotly Interactive web charts Hover, zoom, dashboards
Pandas plot Quick plots from a DataFrame Fast exploration
Folium Maps Choropleth and marker maps

Example 2.4: A bar chart in code

Problem: Draw a bar chart of sales by region for North 250, South 200 and East 60, with title and axis labels, and check the bar heights.

Solution:

  1. Create a figure and axes, and call bar() with the region names and the values.
  2. Set the title to Sales by Region and the y label to Sales.
  3. The axes then hold three bars; reading their heights gives 250, 200 and 60.
  4. savefig() would write the chart to a file; show() would display it.

Result: One chart with three bars of heights 250, 200 and 60, produced by a few lines of code.

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
regions = ["North", "South", "East"]
sales = [250, 200, 60]
fig, ax = plt.subplots()
bars = ax.bar(regions, sales, color="#1f77b4")
ax.set_title("Sales by Region")
ax.set_ylabel("Sales")
print("bars:", len(bars))
print("heights:", [int(b.get_height()) for b in bars])
print("title:", ax.get_title(), "| y label:", ax.get_ylabel())

Output

bars: 3
heights: [250, 200, 60]
title: Sales by Region | y label: Sales

Common mistakes

  • Forgetting titles and axis labels on a chart.
  • Using the default rainbow colours without thought.
  • Hard-coding values instead of reading them from the data, so the chart cannot be regenerated.
  • Expecting a static Matplotlib chart to have hover and zoom.

Key takeaways

  • Matplotlib: figure and axes; Seaborn: statistical charts; Plotly: interactive.
  • Charts as code are reproducible.
  • Use show() to display and savefig() to save.
  • Python needs programming skill but handles large data.

Practice questions

  1. Name two Python visualization libraries.
  2. What are the Figure and Axes in Matplotlib?
  3. Which Python library gives interactive hover and zoom?

Answers to the practice questions

  1. Matplotlib and Seaborn (also Plotly, Bokeh).
  2. The Figure is the whole canvas; an Axes is one plot inside it.
  3. Plotly (or Bokeh).

Sources

  • Official BCA syllabus Data Visualization, course code 26BCA404DSE11, Unit II