Lesson

Bar charts

Learn Bar charts in SQLPad's Data Science in Action: Interactive Visualization with Plotly and Pandas course with practical examples and guided lessons.

In this lesson, we will learn about one of the most commonly used chart types: bar charts. Bar charts are a great way to represent and compare data across categories. We will explore how to create and customize bar charts using Plotly and Pandas. This will help you visualize your data in a clear and interactive manner. By the end of this lesson, you will be able to create your own bar charts and use them effectively in your data analysis projects.

Creating a Simple Bar Chart

In this code example, we will create a simple bar chart using Plotly and Pandas. We will use the built-in dataset 'gapminder' from Plotly Express.

First, let's import the necessary libraries and load the data into a Pandas DataFrame. We will then group the data by year and calculate the average life expectancy for each year. Finally, we will display the first few rows of the DataFrame using df.head().

import plotly.express as px
import pandas as pd

# Load the gapminder dataset
data = px.data.gapminder()

# Group the data by year and calculate the average life expectancy
df = data.groupby(['year'], as_index=False).mean()

# Display the first few rows of the DataFrame
df.head()

Now, let's create a simple bar chart to visualize the average life expectancy for each year. We will use the plotly.graph_objects library to create the chart and display it using fig.show().

import plotly.graph_objects as go

# Create a bar chart
fig = go.Figure(go.Bar(x=df['year'], y=df['lifeExp'], name='Average Life Expectancy'))

# Customize the chart's layout
fig.update_layout(
    title='Average Life Expectancy by Year',
    xaxis_title='Year',
    yaxis_title='Life Expectancy'
)

# Display the chart
fig.show()

After running these code blocks, you should see a simple bar chart displaying the average life expectancy for each year in the gapminder dataset.

Horizontal Bar Charts

In this lesson, we will learn how to create horizontal bar charts using Plotly and Pandas. We will use the built-in dataset from Plotly Express called "tips" for this example.

First, let's import the necessary libraries and load the dataset.

Code Block 1: Load the dataset and print it

import plotly.express as px
import pandas as pd

# Load the built-in 'tips' dataset from Plotly Express
df = px.data.tips()

# Print the dataset
print(df)

Now that we have our dataset, let's create a horizontal bar chart. We will plot the total_bill column on the x-axis and the day column on the y-axis. We will also group the data by the time of the day (lunch or dinner).

Code Block 2: Create a horizontal bar chart

# Create a horizontal bar chart
fig = px.bar(df, x='total_bill', y='day', color='time', orientation='h', title='Horizontal Bar Chart: Total Bill by Day and Time')

# Show the chart
fig.show()

You can now experiment with these code blocks in the provided online playground and further explore the customization options for horizontal bar charts.

Grouped Bar Charts

In this lesson, we will create a grouped bar chart using Plotly and Pandas. We will use the built-in dataset "tips" from Plotly Express.

First, let's import the necessary libraries and load the dataset.

import plotly.express as px
import pandas as pd

# Load the built-in dataset 'tips'
data = px.data.tips()

# Group the data by 'day' and 'sex', calculating the total bill amount for each group
df = data.groupby(['day', 'sex']).sum().reset_index()

# Reformat the data to have 'day' as the index and 'sex' as the columns
df = df.pivot_table(values='total_bill', index='day', columns='sex').reset_index()

print(df)

Now that we have our data prepared, let's create the grouped bar chart using Plotly.

import plotly.graph_objects as go

# Create a Grouped Bar Chart
fig = go.Figure()

# Add Male data as a bar trace
fig.add_trace(go.Bar(x=df['day'], y=df['Male'], name='Male'))

# Add Female data as a bar trace
fig.add_trace(go.Bar(x=df['day'], y=df['Female'], name='Female'))

# Customize the chart appearance
fig.update_layout(
    title='Total Bill Amount by Day and Gender',
    xaxis_title='Day of the Week',
    yaxis_title='Total Bill Amount',
    barmode='group'
)

# Show the chart
fig.show()

Stacked Bar Charts

In this example, we will create a stacked bar chart using the built-in dataset "tips" from the seaborn library. We will first import the necessary libraries and load the dataset into a pandas dataframe.

Code Block 1: Load the dataset

import pandas as pd
import plotly.express as px

# Load the tips dataset
df = px.data.tips()
print(df.head())

Now that we have our dataset loaded, let's move on to creating the stacked bar chart using Plotly.

Code Block 2: Create the stacked bar chart

import plotly.express as px

# Create the stacked bar chart
fig = px.bar(df, x='day', y='tip', color='sex', title='Stacked Bar Chart: Tips by Day and Gender', text='tip',
             labels={'tip': 'Tip Amount', 'day': 'Day', 'sex': 'Gender'}, hover_data=['total_bill'], 
             barmode='stack')

# Show the chart
fig.show()

In the above code block, we use Plotly Express to create a stacked bar chart with the 'day' column as the x-axis, the 'tip' column as the y-axis, and the 'sex' column as the color groups. We also customize the chart title, axis labels, and hover data. The 'barmode' parameter is set to 'stack' to create the stacked bar chart.

Customizing Bar Colors and Text

In this code example, we will customize the bar colors and text in a bar chart using Plotly and Pandas.

First, let's create a Pandas dataframe using the built-in dataset iris from Plotly:

import plotly.express as px
import pandas as pd

# Load the built-in iris dataset from Plotly
data = px.data.iris()

# Create a dataframe with the mean of each species' sepal width
df = data.groupby('species')['sepal_width'].mean().reset_index()

# Print the dataframe
print(df)

Now, let's create a bar chart with customized colors and text:

import plotly.graph_objects as go

# Create a bar chart with customized colors and text
fig = go.Figure()

# Add trace for each species
for index, row in df.iterrows():
    fig.add_trace(go.Bar(
        x=[row['species']],
        y=[row['sepal_width']],
        text=[f"{row['sepal_width']:.2f}"],
        textposition='auto',
        marker_color=px.colors.qualitative.Plotly[index]
    ))

# Update layout properties
fig.update_layout(title='Mean Sepal Width by Species',
                  xaxis_title='Species',
                  yaxis_title='Mean Sepal Width (cm)')

# Show the chart
fig.show()

Adding Error Bars

In this code example, we will be adding error bars to a bar chart using Plotly. We will use the built-in dataset "iris" from the Plotly library.

First, we need to import the required libraries and load the iris dataset. We will also group the data by species and calculate the mean and standard deviation for the sepal width feature.

import plotly.express as px
import pandas as pd

# Load the iris dataset
data = px.data.iris()

# Group the data by species and calculate the mean and standard deviation
df = data.groupby('species')['sepal_width'].agg(['mean', 'std']).reset_index()

print(df)

Now that we have the dataframe ready, let's create a bar chart with error bars using Plotly.

import plotly.graph_objects as go

# Create a bar chart with error bars
fig = go.Figure()

fig.add_trace(go.Bar(
    x=df['species'],
    y=df['mean'],
    error_y=dict(type='data', array=df['std'], visible=True),
    name='Sepal Width'
))

fig.update_layout(title='Average Sepal Width by Species with Error Bars')

fig.show()

Adjusting Bar Width and Spacing

In this code example, we'll adjust the bar width and spacing in a bar chart using Plotly. We'll be using the built-in dataset 'mpg' from Plotly Express.

First, let's import the necessary libraries and load the dataset.

import plotly.express as px
import pandas as pd
import pyodide.http

# Load the built-in mpg dataset
data = pd.read_csv(pyodide.http.open_url('https://gist.githubusercontent.com/omarish/5687264/raw/7e5c814ce6ef33e25d5259c1fe79463c190800d9/mpg.csv'))

# Convert the dataset into a Pandas DataFrame
df = pd.DataFrame(data)

# Display the first few rows of the DataFrame
df.head()

Now, let's create a bar chart with custom bar width and spacing. In this example, we'll be using the 'origin' column as the x-axis and the average 'mpg' for each origin as the y-axis.

# Group the data by origin and calculate the average mpg for each group
grouped_data = df.groupby('origin')['mpg'].mean().reset_index()

# Create the bar chart
fig = px.bar(grouped_data, x='origin', y='mpg', text='mpg', width=600)

# Adjust the bar width and spacing
fig.update_traces(marker=dict(line=dict(color='black', width=1)), width=0.5)

# Customize the chart appearance
fig.update_layout(title='Average MPG by Origin', xaxis_title='Origin', yaxis_title='Average MPG')

# Show the chart
fig.show()

Plotly Bar Chart Layout and Styling

In this code example, we will learn how to create a Plotly bar chart with custom layout and styling. We will use the built-in dataset 'data' from Plotly and modify the chart's layout and style according to our preferences.

First, let's import the required libraries and load the dataset.

import plotly.express as px
import pandas as pd

data = px.data.gapminder().query("year == 2007")
df = pd.DataFrame(data, columns=['continent', 'pop'])
df = df.groupby('continent').sum().reset_index()
print(df)

Now that we have our dataset, let's create a bar chart with custom layout and styling.

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Bar(
    x=df['continent'],
    y=df['pop'],
    marker_color='indianred',
    text=df['pop'],
    textposition='outside',
))

fig.update_layout(
    title='Population by Continent in 2007',
    xaxis_title='Continent',
    yaxis_title='Population',
    plot_bgcolor='rgba(245, 246, 249, 1)',
    showlegend=False,
    font=dict(
        family='Arial, sans-serif',
        size=14,
        color='black'
    ),
)

fig.show()

In this example, we created a bar chart with custom colors, text positions, and layout settings. You can further customize the chart according to your needs by exploring more options in the Plotly documentation.

Exercises

1. Creating a Customized Bar Chart

Instruction

Create a customized bar chart using the tips dataset to visualize the total bill amounts by day of the week and gender. Customize the chart by:

  1. Changing the color of the bars based on the sex column
  2. Adding the total bill amount as a label on each bar
  3. Updating the axis labels and their appearance
  4. Display the chart

My Solution

# Your solution goes here

Hint

Use the px.bar function with the color and text parameters to customize the bar chart. Update the axis labels using the update_xaxes and update_yaxes methods.

Solution

import plotly.express as px

# Load the tips dataset
data = px.data.tips()

# Create a customized bar chart
fig = px.bar(data, x='day', y='total_bill', color='sex', text='total_bill', title='Total Bill Amounts by Day of the Week and Gender',
             labels={'day': 'Day of the Week', 'total_bill': 'Total Bill Amount', 'sex': 'Gender'})

# Update the axis labels
fig.update_xaxes(title_font=dict(size=18, color='crimson'))
fig.update_yaxes(title_font=dict(size=18, color='crimson'))

# Show the chart
fig.show()