Customized barplot with plotnine

logo of a chart:Bar

In a previous post we saw how to create a basic barplot using the plotnine library.

In this step-by-step post, we will see how to customize a bar plot, such as change colors according to a variable, flip the bars and change the opacity.

Libraries

For creating this chart, we will need to load the following libraries:

import pandas as pd
from plotnine import *

Dataset

Since bar plots are a type of chart that displays the counts or values of different categories in a dataset, we will need a dataset that contains the categories we want to compare.

For instance, let's consider a dataset that contains the sales data for three different products: Product A, Product B, and Product C. In our case, we can plot the names of the products on the x-axis and their corresponding sales figures on the y-axis. You can learn more about bar plots by reading this section of the Python Graph Gallery.

sales_data = {
    'Product': ['Product A', 'Product B', 'Product C'],
    'Sales': [150, 220, 180]
}

# Convert the dictionary to a pandas DataFrame
df = pd.DataFrame(sales_data)

One color per bar

If we manually set the color of each bar, we can add the fill argument and give it a list of colors of the same length as the number of bars. This way, each bar will have a different color.

colors = ['darkred', 'lightblue', 'purple']

(
ggplot(df, aes(x='Product', y='Sales')) +
    geom_bar(stat='identity', fill=colors)
)

Color according to a variable

If we want to color the bars according to a variable, we can use the fill argument and set it to the name of the variable.

Moreover, it will automatically create a legend that shows the color scale and the corresponding values.

(
ggplot(df, aes(x='Product', y='Sales', fill='Product')) +
    geom_bar(stat='identity')
)

Change opacity

If we want to change the opacity of the bars, we can use the alpha argument and set it to a value between 0 and 1. This way, we can make the bars more transparent.

(
ggplot(df, aes(x='Product', y='Sales')) +
    geom_bar(stat='identity', alpha=0.4)
)

Flip the bars

If we want to flip the bars so that the categories are displayed horizontally, we can use the coord_flip() function. This way, the categories will be displayed on the y-axis and the values on the x-axis.

(
ggplot(df, aes(x='Product', y='Sales')) +
    geom_bar(stat='identity') +
    coord_flip()
)

Going further

This article explains how to create and customize a bar plot with plotnine.

You might be interested in this post where we explain how to change width of the bars and to change the order of the bars

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!