EXAMPLE

seaborn.pdf

MinerU

seaborn: statistical data visualization

Michael L. Waskom¹

1 Center for Neural Science, New York University

Summary

seaborn is a library for making statistical graphics in Python. It provides a high-level interface to matplotlib and integrates closely with pandas data structures. Functions in the seaborn library expose a declarative, dataset-oriented API that makes it easy to translate questions about data into graphics that can answer them. When given a dataset and a specification of the plot to make, seaborn automatically maps the data values to visual attributes such as color, size, or style, internally computes statistical transformations, and decorates the plot with informative axis labels and a legend. Many seaborn functions can generate figures with multiple panels that elicit comparisons between conditional subsets of data or across different pairings of variables in a dataset. seaborn is designed to be useful throughout the lifecycle of a scientific project. By producing complete graphics from a single function call with minimal arguments, seaborn facilitates rapid prototyping and exploratory data analysis. And by offering extensive options for customization, along with exposing the underlying matplotlib objects, it can be used to create polished, publication-quality figures.

Statement of need

Data visualization is an indispensable part of the scientific process. Effective visualizations will allow a scientist both to understand their own data and to communicate their insights to others (Tukey, 1977). These goals can be furthered by tools for specifying a graph that provide a good balance between efficiency and flexibility. Within the scientific Python ecosystem, the matplotlib (Hunter, 2007) project is very well established, having been under continuous development for nearly two decades. It is highly flexible, offering fine-grained control over the placement and visual appearance of objects in a plot. It can be used interactively through GUI applications, and it can output graphics to a wide range of static formats. Yet its relatively low-level API can make some common tasks cumbersome to perform. For example, creating a scatter plot where the marker size represents a numeric variable and the marker shape represents a categorical variable requires one to transform the size values to graphical units and to loop over the categorical levels, separately invoking a plotting function for each marker type.

The seaborn library offers an interface to matplotlib that permits rapid data exploration and prototyping of visualizations while retaining much of the flexibility and stability that are necessary to produce publication-quality graphics. It is domain-general and can be used to visualize a wide range of datasets that are well-represented within a tabular format.

Example

The following example demonstrates the creation of a figure with seaborn. The example makes use of one of the built-in datasets that are provided for documentation and generation of reproducible bug reports. It illustrates several of the features described in the Overview section, including the declarative API, semantic mappings, faceting across subplots, aggregation with error bars, and visual theme control.

import seaborn as sns
sns.set_theme(context="paper")
fmri = sns.load_dataset("fmri")
g = sns.relplot(
    data=fmri, kind="line",
    x="timepoint", y="signal",
    hue="event", style="event", col="region",
    height=3.5, aspect=.8,
)
g.savefig("paper_demo.pdf")
Two panels from the returned Seaborn figure, showing signal over time
Figure 1: An example seaborn figure demonstrating some of its key features. The image was generated using seaborn v0.11.1.

Seaborn · Michael L. Waskom · CC BY 4.0

Inside the ZIP

  • Textdocument.md
  • Document layoutdocument_middle.json
  • Structured contentdocument_structured_content.json
  • Content listdocument_content_list.json
  • Content list, version 2document_content_list_v2.json
  • Figureimages/page_1_image_4.jpg
  • MinerU’s own ZIPnative/mineru.zip
  • About these filesREADME.txt