Figure types¶
The right figure depends on what you want to show. This page has a table that leads from what you want to show to a figure type. It then shows two ways to plot data that has many columns: the pair plot and principal component analysis (PCA).
The Pair Plots and PCA notebook runs all the code on this page. A comment after a line of code shows the result of that line. The code uses these imports.
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
Choose the figure by what you want to show¶
| You want to show | Figures to consider | Where to find the code |
|---|---|---|
| The distribution of one number | Histogram, box plot, violin plot, CDF | Descriptive statistics and distributions |
| The relationship between two numbers | Scatter plot, 2D histogram | Correlation |
| A change over time | Line plot | Figure design principles, whose worked example is a line plot |
| A comparison between groups | Bar plot, box plots side by side | For the box plot, see Box plot and violin plot. For the bar plot, see the Matplotlib documentation of bar |
| Many numbers for each data point | Pair plot, dimension reduction | Pair plot and Dimension reduction with PCA on this page |
| Values by place | Map, cartogram | Maps and cartograms on this page |
| Connections between accounts | Network plot | Network analysis |
The Python Graph Gallery lists many more figure types, each with its code.
Data with many columns¶
A figure shows two columns of a table easily, and a dataset often has more columns.
The example is the iris data: four measurements, in centimeters, of 150 iris flowers of three species.
R. A. Fisher published the measurements in 1936 (Fisher, 1936).
The code downloads the file iris.csv from the repository of the seaborn example data, mwaskom/seaborn-data, at a fixed commit.
IRIS_URL = (
"https://raw.githubusercontent.com/mwaskom/seaborn-data/"
"71e2436a092d714350de0fc409ca8a8714e7e78f/iris.csv"
)
iris = pd.read_csv(IRIS_URL)
iris
The table has 150 rows, one for each flower, and 5 columns.
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| ... | ... | ... | ... | ... | ... |
| 149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
Four columns are measurements: the length and the width of a sepal, and the length and the width of a petal.
The sepals are the outer parts of the flower, and the petals are the inner parts.
The fifth column is the species: setosa, versicolor, or virginica, with 50 flowers each.
Pair plot¶
A pair plot draws every pair of columns.
- Off the diagonal, each panel is a scatter plot of two columns.
- On the diagonal, each panel is the histogram of one column.
sns.pairplot uses every number column of the table.

Four columns give 16 panels.
Each scatter plot appears twice, once above and once below the diagonal, with the two axes swapped.
The panel of petal_length and petal_width shows that flowers with long petals also have wide petals.
In most panels the points form two groups.
The plot does not show which species a point belongs to.
Use a pair plot for a first look at a table with a few number columns. The number of panels grows fast: ten columns give 100 panels.
Pair plot with color¶
Color adds one more column to every panel.
hue="species" gives each species its own color and adds a legend.
With hue, seaborn draws smooth density curves on the diagonal by default.
diag_kind="hist" keeps the histograms.

Setosa is separate from the other two species in every panel. Versicolor and virginica are next to each other, and they overlap. The two groups of the first pair plot are setosa on one side and the other two species on the other side.
Dimension reduction with PCA¶
A dimension reduction method turns many columns into two, so that one scatter plot shows all of them. Principal component analysis (PCA) is a common method of this kind. It finds new axes, called principal components.
- The first component is the direction along which the data points differ the most.
- The second component is the direction with the most remaining variance, at a right angle to the first.
StandardScaler first gives each column a mean of 0 and a standard deviation of 1.
Without this step, a column with large values dominates the result.
explained_variance_ratio_ is the share of the variance that each component holds.
features = iris.drop(columns="species")
scaled = StandardScaler().fit_transform(features)
pca = PCA(n_components=2)
components = pca.fit_transform(scaled)
pca.explained_variance_ratio_ # array([0.72962445, 0.22850762])
The first component holds 73% of the variance of the four standardized columns, and the second holds 23%. Together they hold 96%, so a scatter plot of the two components keeps most of the differences between the flowers.
components has 150 rows and 2 columns: the position of each flower on the two components.
The next code plots them, with one color for each species.
ratio = pca.explained_variance_ratio_
fig, ax = plt.subplots(layout="constrained")
for species in ["setosa", "versicolor", "virginica"]:
mask = (iris["species"] == species).to_numpy()
ax.scatter(components[mask, 0], components[mask, 1], label=species)
ax.set_xlabel(f"PC1 ({ratio[0]:.0%} of the variance)")
ax.set_ylabel(f"PC2 ({ratio[1]:.0%} of the variance)")
ax.legend(title="species", frameon=False, loc="center left", bbox_to_anchor=(1.0, 0.5))
ax.spines[["top", "right"]].set_visible(False)
plt.show()

Each point is one flower. Setosa is far from the other two species along the first component. Versicolor and virginica are next to each other and overlap a little. One scatter plot now shows the main pattern of the 16 panels of the pair plot.
layout="constrained"makes room for the legend, which is outside the plot.- A principal component is a mix of the four measurements, so its axis has no unit. The axis label gives the share of the variance instead.
PCA is also useful for data with many more columns. An example is the embeddings of posts, which have hundreds of columns.
Maps and cartograms¶
A normal map shows the area of each region. The area of a region often does not match the number that you want to show. A region with a large area and few people gets much attention on a map. A city with many people and a small area gets little attention.
The map below shows the result of the 2016 US presidential election by state. Each state has the color of the candidate who won its electors: red for Trump and blue for Clinton. Trump received 304 electoral votes, and Clinton received 227. The red area looks much larger than the blue area, because many red states are large and have few people.

Map: ElectoralCollege2016.svg by the Wikimedia user Gage, from the Wikipedia article 2016 United States presidential election. The image is a cropped screenshot of the map. The license is CC BY-SA 4.0.
A cartogram is an alternative to a normal map. A cartogram changes the area of each region so that the area is proportional to a number. In the cartogram below, the area of each state is proportional to its number of electors. The large states with few electors become small, and the states with many people become large. The red area and the blue area now compare in the same way as the numbers of electors. The white circles show the positions of the state capitals.

Cartogram: panel A of Figure 3 in Gastner, Seguy, and More (2018), Fast flow-based algorithm for creating density-equalizing map projections, PNAS 115(10). The license is CC BY-NC-ND 4.0.
Further topics¶
This page does not cover the three topics below. Each one has a link to start from.
- t-SNE. t-SNE places data points that are similar close to each other in two dimensions, but the sizes of the groups and the distances between the groups in the plot often mean nothing, and the plot changes with the setting
perplexity; see How to Use t-SNE Effectively. - UMAP. UMAP is a faster method of the same kind and a common choice for text embeddings: for example, BERTopic reduces the embedding of each post with UMAP before it groups the posts into topics; see the UMAP documentation.
- Maps. A map gives a large area more attention than a small area, also when few people live in the large area, so compare the values with the number of people before you read a pattern from a map; see Maps and cartograms above, and for the code, see the map examples of Plotly.
Links¶
- Pair Plots and PCA notebook: all the code on this page
- Python Graph Gallery: many figure types, each with its code
- The seaborn documentation of
pairplot - The scikit-learn documentation of
PCAandStandardScaler - Fisher (1936), The use of multiple measurements in taxonomic problems: the paper with the iris measurements
- mwaskom/seaborn-data: the repository with the file
iris.csv
Next: Figure design principles turns a default plot into a figure for a report.