Data Types Classification and Introduction to Matplotlib

Understanding data types and basic visualization with matplotlib

Data Types Classification

1. Qualitative (Categorical) Variables

1.1. Nominal Variables

  • Definition: Categories without inherent order
  • Examples:
    • Country names (France, USA, Japan)
    • Colors (red, blue, green)
    • Gender (male, female, other)
  • Operations: Equality (=, ≠) only
  • Basic visualization: Bar charts, pie charts

1.2. Ordinal Variables

  • Definition: Categories with meaningful order
  • Examples:
    • Education level (Primary < Secondary < University)
    • Survey ratings (Poor < Fair < Good < Excellent)
    • Size categories (Small < Medium < Large)
  • Operations: Equality and comparison (<, >, ≤, ≥)
  • Basic visualization: Bar charts with ordered categories

2. Quantitative (Numerical) Variables

2.1. Discrete Variables

  • Definition: Countable values, often integers
  • Examples:
    • Number of employees: 25, 126, 512
    • Year of construction: 2010, 2015, 2023
    • Number of children: 0, 1, 2, 3
  • Operations: Most of the time, all numerical operations
  • Basic visualization: Bar charts, scatter plots

2.2. Continuous Variables

  • Definition: Any value within a range
  • Examples:
    • Temperature: 23.5°C, 24.7°C
    • GDP: 1.234 trillion dollars, 45.678 billion dollars
    • Height: 1.75m, 1.823m
  • Operations: All numerical operations
  • Basic visualization: Line plots, histograms, scatter plots

Quick Reference Table

TypeOrderMath OperationsExampleBest Chart Types
NominalCount onlyCountry namesBar, Pie
OrdinalCount, CompareEducation levelOrdered Bar
DiscreteAllYear, CountBar, Scatter
ContinuousAllGDP, TemperatureLine, Histogram

Basics with Matplotlib

2.1. Getting Started with Simple Plots

Key principles

matplotlib is a powerful library for creating static, interactive, and animated visualizations in Python.

In particular, a lot of the complexity related to the the building of the layout of the plot is handled by matplotlib, without us having to worry about it.

However, if more control is needed, we can always use the matplotlib API to customize a plot.

Official documentation

Basic import and first plot

2.2. Line Plot (plt.plot)

Key parameters: x, y, color, linewidth, linestyle, marker, markersize, label, alpha

2.3. Scatter Plot (plt.scatter)

Key parameters: x, y, s (size), c (color), alpha, edgecolors, linewidths, marker

2.4. Bar Chart (plt.bar)

Key parameters: x, height, width, color, edgecolor, linewidth, alpha, label

2.5. Pie Chart (plt.pie)

Key parameters: x, labels, colors, autopct, startangle, explode, shadow

2.6. Subplots (plt.subplots)

Creating multiple plots: fig, axes = plt.subplots(nrows, ncols)

2.7. Figure and Axes Control

Key methods for customization:

  • plt.figure(): Create a new figure with figsize, dpi, facecolor
  • plt.xlabel(), plt.ylabel(): Set axis labels with fontsize, fontweight
  • plt.title(): Set plot title with fontsize, fontweight, pad
  • plt.xlim(), plt.ylim(): Set axis limits
  • plt.xticks(), plt.yticks(): Customize tick positions and labels
  • plt.legend(): Add legend with loc, fontsize, title
  • plt.grid(): Add grid with axis, alpha, linestyle

3. Real Data Examples with GDP Dataset

3.1. Loading External Data (pandas with pyodide as backend)

3.2. Line Plot with Real Time Series Data

3.3. Bar Chart with Country Comparison

3.4. Scatter Plot - Year-to-Year GDP Growth Variations

3.5. Complex Visualization with Subplots - Global GDP Analysis

4. Quick Reference Guide

Essential Matplotlib Methods

FunctionPurposeCommon Parameters
plt.figure()Create new figurefigsize=(width, height), dpi, facecolor
plt.plot()Line plotx, y, color, linewidth, linestyle, marker
plt.scatter()Scatter plotx, y, s (size), c (color), alpha, edgecolors
plt.bar()Bar chartx, height, width, color, edgecolor
plt.pie()Pie chartx, labels, colors, autopct, explode
plt.subplot()Create subplotsrows, cols, index
plt.subplots()Create figure and axesnrows, ncols, figsize
plt.xlabel()Set x-axis labellabel, fontsize, fontweight
plt.ylabel()Set y-axis labellabel, fontsize, fontweight
plt.title()Set plot titletitle, fontsize, fontweight, pad
plt.legend()Add legendlabels, loc, fontsize
plt.grid()Add gridTrue/False, axis, alpha, linestyle
plt.xlim()Set x-axis limitsleft, right
plt.ylim()Set y-axis limitsbottom, top
plt.xticks()Set x-axis ticksticks, labels, rotation
plt.tight_layout()Adjust subplot paramspad, h_pad, w_pad
plt.show()Display plot-

Common Line Styles and Markers

Line StylesDescriptionMarkersDescription
'-'Solid line'o'Circle
'--'Dashed line's'Square
'-.'Dash-dot line'^'Triangle up
':'Dotted line'v'Triangle down
'*'Star
'd'Diamond
'+'Plus
'x'Cross
Maths.pm  ne collecte aucune donnée.
  • Aucun cookie collecté
  • Aucune ligne de log écrite
  • Pas l'ombre d'une base de données distante
  • nihil omnino

  • Ni par pointcarre.app
  • Ni par notre hébergeur
  • Ni par aucun service tiers

Nous expliquons notre démarche zéro donnée conservée sur cette page.

Maths.pm, par

pointcarre.app

Codes sources
Logo licence AGPLv3
Contenus
Logo licence Creative Commons