Matplotlib Series 10: Lollipop plot

This blog specifies how to create/custom basic lollipop plot and vertical lollipop plot with matplotlib in Python and their use cases.

This blog is part of Matplotlib Series:

Lollipop plot

A lollipop plot is an hybrid between a scatter plot and a barplot.

When to use it ?

  • Showing the relationship between a numerical variable and another numerical or categorical variable.

Example 1

example dataframe

basic lollipop plot

import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

(markerline, stemlines, baseline) = plt.stem(df['Product'],
                                             df['Turnover'])
plt.setp(markerline, marker='*', markersize=15,
         markeredgewidth=2, color='gold')
plt.setp(stemlines, color='gold')
plt.setp(baseline, visible=False)

plt.tick_params(labelsize=12)
plt.xlabel('Product', size=12)
plt.ylabel('Turnover(k euros)', size=12)
plt.ylim(bottom=0)

plt.show()

This plot describes turnovers(k euros) for each product. Among eight products, cheese’s sales bring the largest turnover (123k euros), however, it seems that consumers don’t like apple that much.

Example 2

vertical lollipop plot

ordered_df = df.sort_values(by='Turnover').reset_index(drop=True)
my_range = range(1, len(df.index) + 1)

plt.hlines(y=my_range, xmin=0, xmax=ordered_df['Turnover'],
           color='skyblue')
plt.plot(ordered_df['Turnover'], my_range, 'o', markersize=11)
plt.yticks(ordered_df.index+1, ordered_df['Product'])

plt.show()

This vertical lollipop plot describes clearly turnover for each product. Obviously, cheese is prefered by clients.

You can click here to check this example in jupyter notebook.

Reference