Seaborn: Statistical Data Visualization

repository·master·Indexed 12 days ago

https://github.com/mwaskom/seaborn

A high-level Python data visualization library built on top of matplotlib, designed to create attractive and informative statistical graphics. It provides a high-level interface for drawing attractive and informative statistical graphics, including functions like sns.barplot, sns.boxplot, and sns.boxenplot.

Tokens
109.2K
Snippets
381
Records
534
Agent score
95%

What's inside Seaborn

  1. Get started with seaborn

    master

    Seaborn is a high-level Python data visualization library built on top of matplotlib. It is designed to create attractive and informative statistical graphics.

    To begin using seaborn, you should follow these steps:

    1. Install the package: Refer to the installation guide to download and set up the environment.
    2. Learn the core concepts: Read the introductory notes to understand the library's design philosophy.
    3. Explore examples: Browse the example gallery to see various plot types and implementations.
    4. Consult the API: Use the API reference to find specific function signatures and parameters for your plotting tasks.
  2. Understand the difference between 'axes-level' and 'figure-level' functions

    master

    Seaborn functions are categorized into two types based on how they manage matplotlib objects:

    • axes-level functions: These plot onto a single subplot (an Axes object). They can be used to draw on an existing subplot that you have created. Examples include scatterplot and lineplot when used as standalone calls.
    • figure-level functions: These internally create a matplotlib figure and manage the overall layout, potentially including multiple subplots (facets). They combine one or more axes-level functions with a manager object like FacetGrid or JointGrid. Examples include relplot (which uses FacetGrid) and jointplot (which uses JointGrid).

    Why this matters: If you want to customize the layout or create faceted plots (multiple subplots based on data categories), you should use figure-level functions. If you want to place a plot into a specific part of a complex matplotlib layout, you should use axes-level functions.

  3. Migrate from regplot to jointplot for marginal distributions

    master

    In version 0.3, regplot was changed to an "Axes-level" function, meaning it plots onto a specific set of matplotlib axes and no longer automatically produces marginal distributions.

    To achieve the previous behavior (a bivariate plot with marginal distributions), use jointplot with the parameter kind="reg".

  4. Understand changes to stripplot defaults and behavior

    master

    In version 0.7.0, stripplot defaults were updated to align more closely with swarmplot. Key changes include:

    • Points are smaller by default.
    • Points have no outlines by default.
    • Points are not split by default when using the hue parameter.
    • When using hue nesting with split=False, different hue levels are no longer drawn strictly on top of each other, improving visibility.

    All these settings remain customizable via function parameters.

  5. Use the seaborn.objects interface

    master

    Seaborn v0.12.0 introduced the seaborn.objects interface, a new declarative, composable, and extensible API for statistical graphics inspired by the grammar of graphics (similar to ggplot2 and vega-lite).

    Note: This interface is considered "experimental" and may undergo breaking changes in future minor releases. It is intended for users seeking a more structured way to compose plots by defining data mappings and transformations.

  6. Fix legend placement in interactive matplotlib backends

    master
    In version 0.8.1, compatibility was improved for FacetGrid and PairGrid when using interactive matplotlib backends. When setting legend_out=True, the legend will now correctly move outside the figure instead of remaining inside the figure area.
  7. Use diverging colormaps in heatmap and clustermap

    master

    In heatmap and clustermap, automatic detection of diverging data has been removed. To use a diverging colormap, you must explicitly provide the center parameter.

    When center is specified, the colormap is modified so that its middle color corresponds exactly to the center value. This ensures the colorbar limits correspond to the data range, though the full range of the colormap might not be used unless the data is symmetric around the center.

    New perceptual uniform colormaps available in the seaborn.cm namespace include:

    • Sequential: "rocket", "mako"
    • Diverging: "icefire", "vlag"
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.random.randn(10, 12)
    # Explicitly specify 'center' to trigger diverging colormap behavior
    sns.heatmap(data, center=0, cmap="icefire")
    plt.show()
  8. Understand why KDE y-axis values can exceed 1

    master

    When using kdeplot, the y-axis represents the probability density function (PDF), not the actual probability.

    In a continuous probability distribution, the probability of any specific value is zero. The PDF is normalized so that the area under the curve (the integral) equals 1. If the data is concentrated in a very small range, the height of the curve (the density) must be greater than 1 to ensure the total area remains 1.

  9. Explore seaborn plot types and interfaces

    master

    Seaborn provides several specialized interfaces and plot categories for statistical visualization. You can explore these through their respective tutorials and API references:

    • Objects interface: The modern interface for composing graphics.
    • Relational plots: For visualizing relationships between variables.
    • Distribution plots: For visualizing the distribution of data.
    • Categorical plots: For visualizing data categorized by discrete variables.
    • Regression plots: For visualizing statistical relationships and regression models.
    • Multi-plot grids: For creating complex layouts of multiple subplots.
    • Figure theming: For controlling the overall aesthetics and styles of your figures.
    • Color palettes: For managing color schemes and mapping data to colors.
  10. Use enhanced data ingestion features

    master

    The data processing engine has been refactored to support more flexible input formats:

    • Pandas Indexing: Named variables in long-form data can now refer directly to a pandas.DataFrame index or multi-index levels without needing to call .reset_index() first.
    • relplot Flexibility: Now accepts both long- and wide-format data, as well as data vectors in long-form mode.
    • Dictionary Support: The data parameter can now accept a Python dict for wide-form data.
    • Robustness: Wide-form data objects containing a mixture of types will now have non-numeric types removed automatically instead of raising an error.
  11. How categorical functions work and the 'native_scale' parameter

    master

    Seaborn's categorical functions are designed for variables that take a finite number of non-numeric values (like strings).

    Internal Mechanism: Historically, seaborn handles these by mapping unique values to 0-based integer indexes before passing them to matplotlib. This behavior occurs by default even if both $x$ and $y$ variables are numeric, which can cause unexpected behavior when mixing categorical and non-categorical plots.

    Control with native_scale: Starting in version 0.13, you can use the native_scale parameter to control this:

    • native_scale=False (default): Seaborn uses its internal integer mapping.
    • native_scale=True: Seaborn preserves the original properties of the data used for categorical grouping, allowing for more predictable behavior with numeric data.
  12. Unified API for categorical plots in v0.6.0

    master

    Starting with version 0.6.0, seaborn unified the API for categorical plots. These functions show the relationship between one numeric variable and one or two categorical variables.

    Categorical Plot Groups:

    • Distribution plots: boxplot, violinplot, and stripplot (shows distribution of numeric variable in each bin).
    • Statistical estimation plots: pointplot, barplot, and countplot (applies statistical estimation within each bin).

    Key Features of the Unified API:

    • Data Formats: Supports both long-form and wide-form data.
    • Orientation: Can be drawn vertically or horizontally; orientation is inferred from data types when using long-form data.
    • Grouping: All functions natively support a hue variable for a second layer of categorization.
    • Faceting: These functions can be drawn by FacetGrid. Additionally, factorplot can create faceted versions of these plots using the kind parameter, often making direct FacetGrid usage unnecessary.