Lollipop graphics | Lollipop Charts in Python

Contents

This article was published as part of the Data Science Blogathon

Like the evolution of humans, data visualization techniques have also developed over time. In the last times, better visualization techniques have been introduced than ever. Every day, people invent their own visualization invention with a final GOAL: improve the story behind the data. Undoubtedly, advanced data visualization techniques require more skill and effort, but the graphics are always worth it. But thanks to the human brain, before what used to take multiple lines of code is now possible with single line function call. This better explains our claim that data visualization techniques have evolved over time..

This article will discuss one of the popular and powerful data visualization techniques widely used by organizations to describe data and how to plot it in Python.. As I said, plotting these plots is no longer a challenging task. One must know the library, the functions and arguments involved in the technique.

54792pexels-alleksana-4475278-3698816
Image by alleksana from Pexels

Table of Contents

  1. What are lollipop graphics?
  2. How to plot a lollipop graph in Python?
  3. Improvise your lollipop graphics
  4. Comparison of bar graphs and lollipop graphs
  5. Conclusions

What are lollipop graphics?

The lollipop graphics consist of a bar and a stick that resembles the stick and the sugar candy on top, respectively. Filled circles are placed on top of the bars, which gives the resemblance to the sugared lollipop. The Lollipop graphic, like the bar chart, is used to compare the different classes in a category. Therefore, this is a great tool to visualize the comparison between classes. But, What makes it different from the bar chart? First, makes it easy to identify the values ​​when we have a filled circle at the top. Secondly, when we have more classes, let's say more than 10, bar charts become cluttered and difficult for end users to read. While, Secondly, Lollipop Charts use the slim width bars, which creates a larger space for more classes. Again, like bar graphs, we can trace the lollipops horizontally when the number of classes is high.

Lollipop Charts are also demanded to show rankings between classes. For instance, if we want to compare 20 different car models based on their fuel consumption. In similar scenarios, Lollipop graphics are helpful to get the job done efficiently and provide minimal graphics.

26927pexels-fauxels-3183153-6439916

Fauxels image of Pexels

How to plot a lollipop graph in Python?

Lollipop graphs can be plotted in Python efficiently using the Matplotlib library. The Matplotlib library comes with a useful function .mother() which is intended to build Lollipop Charts. Let's see how it works.

1. Libraries import

import matplotlib.pyplot as plt
import numpy as np

2. Random data creation

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

3. Plot the lollipop chart

plt.stem(x, Y, use_line_collection = True)
plt.show()

In plt.stem (), we have set the use_line_collection argument to True. This will add individual lines on the chart as LineCollection. If we don't specify this argument, will give a UserWarning and remind us to set the argument to True. This increases the performance of the Stem Plot.

4. Putting it all together

import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = np.random.random(1, (np.log(0.2 * np.pi)), 10)
plt.stem(x, Y, use_line_collection = True)
plt.show()
42006l1-9239754

Basic lollipop chart (Source: PC)

Improvise your lollipop graphics

Lollipop graphics can be improved by adding arguments to .mother() increasing readability and data description capabilities. Few improvisations that can be done are:

A. Adding additional arguments:

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

When running this code, we obtain:

69459l2-8024298

Lollipop chart with additional arguments (source: personal computer)

Here, we made three modifications:

A. Maker format – We specify markerfmt = ‘s’, this replaced the filled circle of our Lollipop Chart to Square (s refers to square)

B. Line format – We specify linefmat = ‘-‘, this replaced the stem of our Lollipop chart with a colon line. (- refers to the double dash)

C. Baseline format – We specify basefmt = ‘:’, this replaced the baseline of our Lollipop Chart to the colon.

2. Lollipop table classification

import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = list(e.g. 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()

When running this code, we obtain:

58697l3-9213932

Ordered lollipop chart with additional arguments (source: personal computer)

Remember that here we have coded the X labels. Therefore, X labels are not associated with any value. And that is why when ordering the values, labels stay in the same place.

3. Plotting a horizontal palette chart

import matplotlib.pyplot as plt
import numpy as np
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
y = list(e.g. linspace(1, (np.log(0.2 * np.pi)), 10))
y.sort()
my_range=range(1,len(x)+1)
plt.hlines(y = x, xmin = 0 , xmax = y, color="skyblue")
plt.plot(Y, x, "O")
plt.show()

When running this code, we obtain:

21249l4-4200757

Horizontal palette chart (Source: personal computer)

Here, we use the .hlines method () to plot the horizontal sticks of the Lollipop chart, and the sugar sugar is plotted using the .plot method () specifying the marker as “O”.

Comparison of bar graphs and lollipop graphs

Let's compare the bar chart and the Lollipop with an example to determine which one fits well with our example. Here, we will plot the subplots for the horizontal bar chart and horizontal palette chart to compare the figures side by side. We have generated random data using the NumPy library. On the Y axis, we have used the distance() function and converted each label to a string.

import matplotlib.pyplot as plt
import numpy as np
x = list(map(str, range(1, 35)))
y = list(np.random.random(34))
f, axs = plt.subplots(1,2,figsize=(12,6))
axs[0].barh(x,Y)
axs[1].hlines(y = x, xmin = 0 , xmax = y, color="skyblue")
axs[1].plot(Y, x, marker = "O", linewidth = 0)
plt.show()

When running this code, we obtain:

25508l7-9466640

Comparison of horizontal bar chart and horizontal palette chart (Source: personal computer)

Therefore, reading the data is quite easier on the horizontal palette chart than on the horizontal bar chart. One can easily classify the values ​​and compare the classes within. What's more, a greater number of classes in a category can saturate the Class Axis. Therefore, one type of diagram should not be relied upon for a problem. Plotting different graphs for a problem and selecting the best one is a smarter decision.

Note: Here we are using the .random method () de NumPy random. Every time we run the code, will produce a different set of values. Therefore, you will get different graphics every time we run the code.

Conclusions

In this article, we learned how to build a basic Lollipop chart in Python. But the topic does not end here. There are a host of modifications that can be made to your lollipop graphics. The ones we learned earlier are the most basic modifications that can be done with less effort. Today, many BI tools and programming languages ​​are capable of creating advanced visualizations such as Lollipop Charts. In fact, the .mother() The matplotlib method is inspired by the MATLAB root method. We can call the Lollipop charts the special case of the bar chart. We also discuss what mistakes to avoid when building a lollipop chart.. You can try to build your own version of Lollipop Charts because you always learn by trying..

About the Author

Connect with me on LinkedIn Here.

Check out my other articles here

You can provide me with your valuable comments on LinkedIn.

Thank you for giving us your time!

The media shown in this Lollipop chart article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.