Welcome to matplotlib-helpers’s documentation!

Grid plot

The matplotlib_helpers.chart.encode() function is inspired by the altair project.

With matplotlib_helpers.chart.encode() (quoted from the altair documentation):

  • The data source is a DataFrame that consists of columns of different data types (quantitative, ordinal, nominal and date/time).
  • The DataFrame is in a tidy format where the rows correspond to samples and the columns correspond the observed variables.
  • The data is mapped to the visual properties (position, color, size, shape, faceting, etc.) using the group-by operation of Pandas.

Usage

The examples below plot vehicle fuel economy (in miles per gallon) versus horsepower for a dataset from the altair project.

Set marker color by the Year column and set the shape of the each marker according to the Origin column:

from altair import load_dataset
import matplotlib as mpl
import matplotlib.style
import matplotlib_helpers as mplh
import matplotlib_helpers.chart

# load data as a pandas DataFrame
cars = load_dataset('cars')

with mpl.style.context(['ggplot']):
    mplh.chart.encode(cars,
                      x='Horsepower',
                      y='Miles_per_Gallon',
                      shape='Year',
                      color='Origin',
                      cell_size=5, fill=False)

Fuel economy vs horsepower

Split plot into multiple subplots, with the subplot in each column corresponding to a distinct value in the Origin column.

The same type of handling can be applied using the row keyword.

with mpl.style.context(['ggplot']):
    mplh.chart.encode(cars,
                      x='Horsepower',
                      y='Miles_per_Gallon',
                      color='Year',
                      shape='Year',
                      column='Origin',
                      cell_size=5, fill=False)

Fuel economy vs horsepower (columns by “Origin”)

By default, all plots share the same x axis scale and y axis scale. This behaviour can be changed by setting the sharexscale keyword argument or the shareyscale keyword argument.

For example, note that the subplots below all have different x axis and y axis scales.

with mpl.style.context(['ggplot']):
    mplh.chart.encode(cars,
                      x='Horsepower',
                      y='Miles_per_Gallon',
                      color='Year',
                      shape='Year',
                      column='Origin',
                      sharexscale=False,
                      shareyscale=False,
                      cell_size=5, fill=False)

Fuel economy vs horsepower (axis scales not shared)

See the matplotlib_helpers.chart.encode() documentation for more details.


Contents:

Project Modules

matplotlib_helpers Package

matplotlib_helpers Package
chart Module
class matplotlib_helpers.chart.Chart(df)[source]

Bases: object

Methods

encode(**kwargs)[source]
matplotlib_helpers.chart.data_groups(df, group_key, data_key)[source]
matplotlib_helpers.chart.encode(df_data, **kwargs)[source]
Parameters:
  • x (str) – Label of column containing x-dimension.
  • y (str) – Label of column containing y-dimension.
  • row (str, optional) – Label of column containing row categories. If None, all data is plotted in a single row of plots.
  • column (str, optional) – Label of column containing column categories. If None, all data is plotted in a single column of plots.
  • color (str, optional) – Label of column containing color categories. If None, all data is plotted in the same color.
  • shape (str, optional) – Label of column containing shape categories. If None, all data is plotted using the same marker shape.
  • style (str, optional) – Label of column containing style categories. If None, all data is plotted using the same line style.
  • sharexscale (bool or 'column', optional) – If True (default) all subplots share the same scale on the x axis. If 'column' all subplots in the same column share the same x axis. If False, the x axis of each subplot is scaled independently.
  • shareyscale (bool or 'row', optional) – If True (default) all subplots share the same scale on the y axis. If 'row' all subplots in the same row share the same y axis. If False, the y axis of each subplot is scaled independently.
  • fill (bool, optional) – Fill markers
  • stroke (bool, optional) – Draw marker outlines
  • linestyle (str, optional) –

    Line style to use for plot.

    By default, if shape is set, linestyle is set to "none". If shape is not set, linestyle is set to "--" by default.

Returns:

The matplotlib figure (fig), a nested dictionary (axes) indexed by row key then by column key, a pandas.Series (keys) mapping each categorical argument name to the corresponding column label, a pandas.Series (values) mapping each categorical argument name to a corresponding list of unique category values.

Return type:

(fig, axes, keys, values)

matplotlib_helpers.chart.groupif(df, key)[source]
matplotlib_helpers.chart.time_safe(series)[source]
matplotlib_helpers.chart.time_total_seconds(t)
matplotlib_helpers.chart.unique_by_column(df)[source]
Parameters:df (pandas.DataFrame) – Data frame.
Returns:Mapping from each column label to ordered list of unique values in corresponding column in data frame.
Return type:pandas.Series

Indices and tables