Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Plotly graphics

  • One of the major players in interactive graphs is Plotly.

  • Some alternatives are Bokeh and Altair.

  • Interfacing it comes in two main flavours:

    • graph_objects: low-level graphics handling

    • plotly.express: high-level graphics handling

  • In addition plotly is integrated in the dash environment with its dialect.

  • Figures are dictionaries, which we will leverage.

Plotting with AI assistance

  • Many plot commands can be obtained by describing plots to AIs.

  • AIs can also translate from one plotting framework to another.

  • Sketching a set of plot and adding sufficient descriptions, may result in usable code.

Basic plotting

Loading...

Line plot

Loading...
Loading...
Loading...
Figure({
    'data': [{'hovertemplate': 'country=Norway<br>year=%{x}<br>lifeExp=%{y}<extra></extra>',
              'legendgroup': 'Norway',
              'line': {'color': '#636efa', 'dash': 'solid'},
              'marker': {'symbol': 'circle'},
              'mode': 'lines',
              'name': 'Norway',
              'orientation': 'v',
              'showlegend': True,
              'type': 'scatter',
              'x': {'bdata': 'oAelB6oHrwe0B7kHvgfDB8gHzQfSB9cH', 'dtype': 'i2'},
              'xaxis': 'x',
              'y': {'bdata': ('exSuR+EqUkBcj8L1KFxSQK5H4XoUXl' ... '5H4XqUU0AzMzMzM8NTQAaBlUOLDFRA'),
                    'dtype': 'f8'},
              'yaxis': 'y'},
             {'hovertemplate': 'country=Sweden<br>year=%{x}<br>lifeExp=%{y}<extra></extra>',
              'legendgroup': 'Sweden',
              'line': {'color': '#EF553B', 'dash': 'solid'},
              'marker': {'symbol': 'circle'},
              'mode': 'lines',
              'name': 'Sweden',
              'orientation': 'v',
              'showlegend': True,
              'type': 'scatter',
              'x': {'bdata': 'oAelB6oHrwe0B7kHvgfDB8gHzQfSB9cH', 'dtype': 'i2'},
              'xaxis': 'x',
              'y': {'bdata': ('16NwPQr3UUCPwvUoXB9SQEjhehSuV1' ... 'yPwvXYU0DD9ShcjwJUQH9qvHSTOFRA'),
                    'dtype': 'f8'},
              'yaxis': 'y'}],
    'layout': {'height': 400,
               'legend': {'title': {'text': 'Country'}, 'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'width': 700,
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Year'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'Life Expectancy (years)'}}}
})
#636efa

Directly editing the dictionary

Loading...

Shaded areas

  • The fill parameter can be used to fill to the next line, to zero or to itself if the series reverses.

  • The latter is most convenient for single colour background shading.

Loading...

Bar plot

Loading...
Loading...
Loading...
Figure({
    'data': [{'hovertemplate': 'variable=Europe max<br>year=%{x}<br>value=%{y}<extra></extra>',
              'legendgroup': 'Europe max',
              'marker': {'color': '#636efa', 'opacity': 0.5, 'pattern': {'shape': ''}},
              'name': 'Europe max',
              'orientation': 'v',
              'showlegend': True,
              'textposition': 'auto',
              'type': 'bar',
              'x': {'bdata': 'oAelB6oHrwe0B7kHvgfDB8gHzQfSB9cH', 'dtype': 'i2'},
              'xaxis': 'x',
              'y': {'bdata': ('exSuR+EqUkCuR+F6FF5SQOxRuB6Fa1' ... 'yPwvXYU0BI4XoUridUQJzEILBycFRA'),
                    'dtype': 'f8'},
              'yaxis': 'y'},
             {'hovertemplate': 'variable=Bulgaria<br>year=%{x}<br>value=%{y}<extra></extra>',
              'legendgroup': 'Bulgaria',
              'marker': {'color': '#EF553B', 'opacity': 0.5, 'pattern': {'shape': ''}},
              'name': 'Bulgaria',
              'orientation': 'v',
              'showlegend': True,
              'textposition': 'auto',
              'type': 'bar',
              'x': {'bdata': 'oAelB6oHrwe0B7kHvgfDB8gHzQfSB9cH', 'dtype': 'i2'},
              'xaxis': 'x',
              'y': {'bdata': ('zczMzMzMTUDXo3A9CqdQQHE9CtejYF' ... '5H4XqUUUApXI/C9QhSQLgehetRQFJA'),
                    'dtype': 'f8'},
              'yaxis': 'y'}],
    'layout': {'barmode': 'overlay',
               'height': 400,
               'legend': {'title': {'text': 'Country'}, 'tracegroupgap': 0},
               'template': '...',
               'title': {'text': 'Life Expectancy in Europe'},
               'width': 700,
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Year'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'Life Expectancy (years)'}}}
})
Loading...

Polar barplots

  • The x-axis in barplots do not have to be straight.

Loading...

Scatter plot

Loading...
Figure({
    'data': [{'hovertemplate': 'species=setosa<br>sepal_width=%{x}<br>sepal_length=%{y}<extra></extra>',
              'legendgroup': 'setosa',
              'marker': {'color': '#636efa', 'symbol': 'circle'},
              'mode': 'markers',
              'name': 'setosa',
              'orientation': 'v',
              'showlegend': True,
              'type': 'scatter',
              'x': {'bdata': ('AAAAAAAADEAAAAAAAAAIQJqZmZmZmQ' ... 'mZmQlAmpmZmZmZDUBmZmZmZmYKQA=='),
                    'dtype': 'f8'},
              'xaxis': 'x',
              'y': {'bdata': ('ZmZmZmZmFECamZmZmZkTQM3MzMzMzB' ... 'ZmZhJAMzMzMzMzFUAAAAAAAAAUQA=='),
                    'dtype': 'f8'},
              'yaxis': 'y'},
             {'hovertemplate': 'species=versicolor<br>sepal_width=%{x}<br>sepal_length=%{y}<extra></extra>',
              'legendgroup': 'versicolor',
              'marker': {'color': '#EF553B', 'symbol': 'circle'},
              'mode': 'markers',
              'name': 'versicolor',
              'orientation': 'v',
              'showlegend': True,
              'type': 'scatter',
              'x': {'bdata': ('mpmZmZmZCUCamZmZmZkJQM3MzMzMzA' ... 'MzMwdAAAAAAAAABEBmZmZmZmYGQA=='),
                    'dtype': 'f8'},
              'xaxis': 'x',
              'y': {'bdata': ('AAAAAAAAHECamZmZmZkZQJqZmZmZmR' ... 'zMzBhAZmZmZmZmFEDNzMzMzMwWQA=='),
                    'dtype': 'f8'},
              'yaxis': 'y'},
             {'hovertemplate': 'species=virginica<br>sepal_width=%{x}<br>sepal_length=%{y}<extra></extra>',
              'legendgroup': 'virginica',
              'marker': {'color': '#00cc96', 'symbol': 'circle'},
              'mode': 'markers',
              'name': 'virginica',
              'orientation': 'v',
              'showlegend': True,
              'type': 'scatter',
              'x': {'bdata': ('ZmZmZmZmCkCamZmZmZkFQAAAAAAAAA' ... 'AAAAhAMzMzMzMzC0AAAAAAAAAIQA=='),
                    'dtype': 'f8'},
              'xaxis': 'x',
              'y': {'bdata': ('MzMzMzMzGUAzMzMzMzMXQGZmZmZmZh' ... 'AAABpAzczMzMzMGECamZmZmZkXQA=='),
                    'dtype': 'f8'},
              'yaxis': 'y'}],
    'layout': {'legend': {'title': {'text': 'species'}, 'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Sepal width'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'Sepal length'}}}
})
Loading...

Boxplots and violin plots

Loading...
Loading...

Marginal plots

  • Scatter plots support simple marginal plots, e.g., histograms and similar.

Loading...

Exercise

  • Test other marginal plot types and locations.

Heatmap

Loading...

Tables

  • One can plot tables with styling.

Loading...

Layouts

  • For Plotly express there is no direct layout option, except for facets (see below).

  • Instead one need to go to the low-level graph objects.

Loading...

Facet plots

  • Facet plots are sets of plots having the same properties execpt for one categorical difference.

  • Examples can be scatter plots, line plots, histograms, etc. with one distinguishing feature.

  • Parameters for layout specifications are available.

Loading...
Loading...

Sunburst plot

  • Hierarchical data, e.g., pivoted data, can be displayed as sunbursts.

  • These are pie charts with concentric circles marking hierarchical relationships.

  • Interactivity is kind of cool here.

Loading...
Loading...
Loading...

Parallel coordinates

  • Multiple features in a parallel coordinate system.

  • Each sample is a line marking values in each feature.

  • Colours from classes or continuous feature.

  • Interactivity includes marking part of coordinate axis and rearranging coordinate axes.

Loading...

Exercise

  • Adjust the above code to include a slider for opacity.

Notebook Cell