s

Plotly_express



Plotly Express uses a more data-oriented approach that Plotly Graph Objects. Whereas plotly.go begins with an empty rectangle to be built on top of, with plotly express you begin with the columns / features of the dataset and create the visualisation based on them.

Plotly Express is a high level plotting library and handles certain defaults such as labelling legends and axes. It also allows the data to be used to express many of its attributes using visual aesthetics such as size, colour, location etc. You only need to simply declare what attribute is to be expressed with which column of data.

The knowledge required for using the more higher level plotly.express is lower than that required for the lower level plotly.graph_objects which has more complexity but allows you more freedom in customising the plots. Plotly graph_objects is recommended for non-standard visualisations and when there is a lot of customisation required. Also when you want to display different plots side by side or in a grid. Faceted subplots are easy to do with plotly.express but arranging sub-plots that are different charts can be more difficult to achieve.

Both Plotly.Expresss and Plotly Graph Objects return Figure objects which can be modified retrospectively.

Usually charts are created with plotly express by calling the type of chart as a function, with each function having its own set of parameters depending on its type.

Plotly.Express includes some datasets for practising on under it’s data module.

gapminder = px.data.gapminder()

There are several ways to pass arguments to the functions:

  • A DataFrame with column names. In most cases the first parameter is data_frame px.scatter(data_frame = dfm, x = 'dewpt', y = 'rain')

  • Instead of passing a data_frame argument you can simply pass list, tuples or any array-like structure. px.scatter(x = dfm['dewpt'], y = dfm['rain'])

  • a mix of the two

Like plotly Graph_Objects, Plotly Express functions returns a Figure object that can be customised afterwards.

fig = px.scatter(...) fig.add_annotation(x=., y= , text='')


Can easily map the colour, size and shape of a marker to feaures of the dataset by declaring which column to use for each visual attribute.

fig = px.scatter(data_frame = gapminder, 
           x = 'gdpPercap',
           y = 'lifeExp', 
           size = 'pop', 
           facet_col='continent', 
           color= 'continent',
           title = 'Gapminder: Life Expectancy vs GDP per capita from 1952 to 2007', 
           labels = {'gdpPercap': 'GDP Per Capita', 'lifeExp': 'Life Expectancy'},
          log_x=True,
          range_y = [20, 100],
          hover_name ='country',
          animation_frame = 'year',
          height = 600,
          size_max = 90)
Plotly_express screenshot

Tech used:
  • JavaScript
  • CSS
  • HTML