Introduction to Lollipop charts in Python

FeaImg Lollipop Charts

Lollipop charts are made up of a bar and a stick that look like a stick with sugar candy on top. The filled circles are positioned at the top of the bars to resemble a sweet Lollipop. Lollipop Charts, like Bar Charts, are used to compare distinct classes within a category. As a result, this is an excellent tool for visualizing class comparisons.

Lollipop Charts may be enhanced by passing options to.stem(), which improves readability and data description capabilities. A few examples of possible alterations are:

The Matplotlib module may be used to effectively plot Lollipop Charts in Python. The Matplotlib package has a useful function. stem(), which is used to create Lollipop Charts. Let us see how it goes.


Importing the Libraries

import matplotlib.pyplot as plt
import numpy as np

Creating Random Data

x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = np.linspace(1, (np.log(0.2 * np.pi)), 10)

Plotting the Lollipop Chart

We have set the use line collection option to True in plt.stem(). Individual lines will be added to the plot as a LineCollection. If we do not supply this argument, we will receive a UserWarning and be reminded to set it to True.
This improves the Stem Plot’s performance.

plt.stem(x, y, use_line_collection = True)
plt.show()
Basic Lollipop Plot
Basic Lollipop Plot

Improvise Lollipop Charts

Lollipop Charts may be enhanced by passing options to.stem(), which improves readability and data description capabilities. A few examples of possible alterations are:

Adding Additional Arguments

import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = np.linspace(1, (np.log(0.2 * np.pi)), 10)
plt.stem(x, y, markerfmt = 's', linefmt='--', basefmt = ':', use_line_collection=True)
plt.show()
Improvised Lollipop Plot 1
Improvised Lollipop Plot 1

Sorting the Lollipop Chart

import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = list(np.linspace(1, (np.log(0.2 * np.pi)), 10))
y.sort()
plt.stem(x, y, markerfmt = 's', linefmt='--', basefmt = ':', use_line_collection=True)
plt.show()
Improvised Lollipop Plot 2
Improvised Lollipop Plot 2

Conclusion

Congratulations! You just learned how to build a basic Lollipop Chart in Python. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Python Bar Plot – Visualize Categorical Data in Python
  2. How to Load and Plot the MNIST dataset in Python?
  3. Top 5 Best Python Plotting and Graph Libraries

Thank you for taking your time out! Hope you learned something new!! 😄