Bar Charts#

We can create a bar chart using px.bar(). In the default settings, each row of the dataframe is represented as a rectangular mark.

A few examples, using the gapminder data.

import plotly.express as px # import statement
data = px.data.gapminder().query("country == 'Canada'") # subset data
data # inspect data
country continent year lifeExp pop gdpPercap iso_alpha iso_num
240 Canada Americas 1952 68.750 14785584 11367.16112 CAN 124
241 Canada Americas 1957 69.960 17010154 12489.95006 CAN 124
242 Canada Americas 1962 71.300 18985849 13462.48555 CAN 124
243 Canada Americas 1967 72.130 20819767 16076.58803 CAN 124
244 Canada Americas 1972 72.880 22284500 18970.57086 CAN 124
245 Canada Americas 1977 74.210 23796400 22090.88306 CAN 124
246 Canada Americas 1982 75.760 25201900 22898.79214 CAN 124
247 Canada Americas 1987 76.860 26549700 26626.51503 CAN 124
248 Canada Americas 1992 77.950 28523502 26342.88426 CAN 124
249 Canada Americas 1997 78.610 30305843 28954.92589 CAN 124
250 Canada Americas 2002 79.770 31902268 33328.96507 CAN 124
251 Canada Americas 2007 80.653 33390141 36319.23501 CAN 124
fig = px.bar(data, x='year', y='pop') # create figure
fig # show figure

In this example, the year column is assigned as the X axis value, and the pop column is assigned as the Y axis value.

Stacked Bar Chart#

s We can also generate a stacked bar chart using px.bar(). Let’s take a look at a stacked bar chart for sample data stored in two different formats. As we learned in the pandas lab, data can be stored in a long or wide form.

  • Long-form data has one row per observation and one column per variable. Also known as tidy data.

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

data = px.data.gapminder() # load data
data.info() # inspect data
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1704 entries, 0 to 1703
Data columns (total 8 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   country    1704 non-null   object 
 1   continent  1704 non-null   object 
 2   year       1704 non-null   int64  
 3   lifeExp    1704 non-null   float64
 4   pop        1704 non-null   int64  
 5   gdpPercap  1704 non-null   float64
 6   iso_alpha  1704 non-null   object 
 7   iso_num    1704 non-null   int64  
dtypes: float64(2), int64(3), object(3)
memory usage: 106.6+ KB
fig = px.bar(data, x='continent', y='pop', color='country') # create figure
fig # show figure

Grouped Bar Charts#

Setting barmode to group produces a grouped bar chart.

fig = px.bar(data, barmode='group', x='year', y='pop', color='continent') # create figure
fig # show figure

Horizontal Bar Charts#

We can set the orientation attribute to h to produce a horizontal bar chart.

fig = px.bar(data, orientation='h', y='year', x='pop', color='continent') # create figure
fig # show figure

Remember in a horizontal bar chart, the X and Y axis values are the inverse of a vertical bar chart.

Additional Resources#

For more on bar charts in plotly: