×

Bar Charts with Plotly

A bar chart is a way of summarizing a set of categorical data. The bar chart displays data using a number of bars, each representing a particular category. The height of each bar is proportional to the sum of the values in the category it represents.

The categories could be something like an age group or a geographical location. It is also possible to colour or split each bar into another categorical column in the data, which enables you to see the contribution from different categories to each bar or group of bars in the bar chart.

Submodules in Plotly to create Plots

Plotly supports two submodules for creating various plots such as line plot, bar plot etc., that are following:

  • Plotly Express
import plotly.express as px
  • Plotly Graph objects
import plotly.graph_objects as go

We can choose to use any of the modules but plotly.express is widely used, because plotly.express needs less number of lines of code in order to create any plot.

Bar Chart with Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

  • Import the plotly express module.
import plotly.express as px
  • Get the data frame to use it in the plot.
data_frame = px.data.gapminder().query("country == 'Canada')
  • With px.bar, each row of the DataFrame can be represented in a bar chart. Create the bar chart and store its object.
fig = px.bar(data_canada, x='year', y='pop)
  • Show the plot via show() – method, using object of the plot created.
fig.show()
Complete Code
import plotly.express as px

data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()
Output
Bat Chart Plotly

Horizontal Bar Chart

Exactly similar to Bar chart, a horizontal bar graph can be created by changing the orientation of the plot. px.bar() has an optional parameter to specify the orientation.

h_fig = px.bar(data_canada, x='year', y='pop', orientation='h')
h_fig.show()
Output
Horizontal Bar Chart Plotly

Bar chart with Long and Wide format data

Long format data

Long-form data has one row per observation, and one column per variable. This is suitable for storing and displaying multivariate data i.e. with dimensions greater than 2. This format is sometimes called “tidy”.

Here we are using the data frames provided by Plotly, we can read them either in long or wide format.

title – is a keyword argument to set the title of the plot.

long_df = px.data.medals_long()
long_fig = px.bar(
           long_df, 
           x="nation", 
           y="count", 
           title="Bar Chart with Long format data")
long_fig.show()
Output
Long Format Data Plotly

Difference between Long and Wide format Data

You can think of it this way!

  • When you think ‘wide’, think ‘horizontal’.
  • When you think long, think vertical

This is a long format:

Product | Attribute | Value
 --------|-----------|--------
       A | Height    | 10
       A | Width     | 5
       A | Weight    | 2
       B | Height    | 20
       B | Width     | 10

The same data is a wide format would be:

Product | Height | Width | Weight
 --------|--------|-------|--------
       A | 10     | 5     | 2
       B | 20     | 10    | NA

Wide Format Data

Wide-form data has one row per value of one of the first variables, and one column per value of the second variable. 

wide_df = px.data.medals_wide()
wide_fig = px.bar(
           wide_df, 
           x="nation", 
           y=["gold", "silver", "bronze"], 
           title="Bar Chart with Wide format data")
wide_fig.show()
Output
Wide Format Data Plotly

Setting Colour Attribute

The colour of the bar plot can be customized using the keyword argument – “color”. Pass the column name from your data frame. Colouring is done with respect to the column passed.

data_canada = px.data.gapminder().query("country == 'Canada'")
col_fig = px.bar(
          data_canada, 
          x='year', 
          y='pop', 
          color = 'lifeExp') #Color attribute
col_fig.show()
Output
Color Attribute Plotly

Setting the title to the axes

By default, the title of the axes is set to the column names passed to the ‘x’ and ‘y’ arguments of the bar() – method. “Labels” — keyword argument allows to set the titles of the axes in the plot.

Labels – is a dictionary data type, where you can name the titles for the column names.

lbl_fig = px.bar(
    data_canada, 
    x='year', 
    y='pop', 
    labels = {'pop': 'Population', 'year': 'The Year'}) #Titles
lbl_fig.show()
Output
Labels Attribute Plotly

Stacked-Up Bar’s in Bar Chart

When several rows share the same value of ‘x- axis’, the rectangles are stacked on top of one another by default. For example:

Wide Format Data Plotly 1

The default stacked bar chart behaviour can be changed to grouped (also known as clustered) using the barmode argument:

wide_df = px.data.medals_wide()
wide_fig = px.bar(wide_df, 
    x="nation", 
    y=["gold", "silver", "bronze"],
    barmode = 'group') #Barmode attribute
wide_fig.show()
Output
Barmode Attribute Plotly

Bar Chart with Plotly Graph Objects

  • Import the plotly graph objects.

plotly graph_objects module is typically imported as go

import plotly.graph_objects as go
  • Get the data frame to use it in the plot.
countries=['India', 'Australia',
           'Japan', 'America',
           'Russia']
 
values = [4500, 2500, 1053, 500,
          3200]

When using Plotly graph objects first we have to create a figure(i.e a canvas) in which we actually add the plot. For creating a figure we have to just call the Figure() – method of the graph_objects – submodule.

go_fig = go.Figure()
  • Create the plot – Plotly graph objects has Bar() – method for the Bar graph.
obj = go.Bar(x = countries, y = values)

Add the plot object to the Figure(or Canvas), for adding the plot into the figure(or canvas) created, we have to use add_trace() – method.

go_fig.add_trace(obj)
  • Show the plot via show() – method, using object of the figure created.
go_fig.show()
Complete Code
import plotly.graph_objects as go

countries=['India', 'Australia', 'Japan', 'America', 'Russia']
values = [4500, 2500, 1053, 500, 3200]

go_fig = go.Figure()

obj = go.Bar(x = countries, y = values)
go_fig.add_trace(obj)
go_fig.show()
Output
Bar Chart Graph Objects Plotly

Grouped and Stacked Bar Chart

With Graph objects first, we create the figure(i.e a Canvas) and then we add the plot to the figure. We can add one or more Plots to the figure and can customize the orientation(i.e barmode) of the plots in the figure either to ‘Group’ or ‘Stack’.

Default barmode is set to Group.

Added the same plot two times in the figure for demonstration.

go_fig = go.Figure()
obj = go.Bar(x = countries, y = values)

go_fig.add_trace(obj)
go_fig.add_trace(obj)

go_fig.show()
Output
Grouped Bar Chart Graph Objects

Stacked Barmode

go_fig = go.Figure()
obj = go.Bar(x = countries, y = values)

go_fig.add_trace(obj)
go_fig.add_trace(obj)

go_fig.update_layout(barmode='stack') #Barmode value
go_fig.show()
Output
Stacked Barmode Graph Objects Plotly

Bar Chart with Direct Labels

Direct labels mean how we can display the value of the bar in the bar chart. go.Bar() – method has an optional parameter named ‘text’ which takes the column name of the Data Frame as input to show up the value.

import plotly.graph_objects as go

countries=['India', 'Australia', 'Japan', 'America', 'Russia']
values = [4500, 2500, 1053, 500, 3200]

go_fig = go.Figure()
obj = go.Bar(x = countries, y = values, text = values) #Text attribute

go_fig.add_trace(obj)
go_fig.show()
Output
Direct Labels Graph Objects 1

Rotate Bar chart Labels

Labels mean values at X and Y axes. Countries name in this example at X axis.

go_fig = go.Figure()
obj = go.Bar(x = countries, y = values, text = values)

go_fig.add_trace(obj)
go_fig.update_layout(xaxis_tickangle=-45) #Set the angle to rotate

go_fig.show()
Output
Rotated Label Graph Objects 1