Create lollipop charts with Pandas and Matplotlib
Last Updated :
24 Jan, 2021
In this article, we will create Lollipop Charts. They are nothing but a variation of the bar chart in which the thick bar is replaced with just a line and a dot-like “o” (o-shaped) at the end. Lollipop Charts are preferred when there are lots of data to be represented that can form a cluster when represented in the form of bars.
LINK TO CSV FILE : click here
Let’s discuss some examples with different methods to visualize the lollipop chart using pandas.
Example 1:
Using plt.stem function, it creates vertical lines at each x position covered under the graph from the baseline to y and places a marker there. But it is inflexible so you might want to try other methods also.
In this example, we will plot the lollipop chart by collecting the data from a CSV file using pandas.
At first, we will create an empty chart through plt.subplots() and using the stem plot that accepts arguments in the form of (x,y) both can be of array-like values we will plot the values into the chart. The use_line_collection=True improves the performance of the stem plot and plot the stem lines as a LineCollection instead of individual lines. The basefmt=’ ‘ removes the baseline from the stem lines.
The CSV file has column as “month_number” and “total_profit” which has been read from the CSV file and passed into the stem plot as (x,y) and some formatting and details have also been added.
The set_ylim() is set to zero so that the y-axis values start from zero.
Approach:
- Import modules (matplotlib and pandas)
- Open the CSV file and read the data
- Plot it using plt.stem
Python3
from pandas import *
import matplotlib.pyplot as plt
d = read_csv( "company_sales_data.csv" )
fig, axes = plt.subplots()
axes.stem(d[ 'month_number' ], d[ 'total_profit' ],
use_line_collection = True , basefmt = ' ' )
axes.set_ylim( 0 )
plt.title( 'Total Profit' )
plt.xlabel( 'Month' )
plt.ylabel( 'Profit' )
plt.xticks(d[ 'month_number' ])
|
Output:

Example 2:
In this example, we will create an empty graph/chart using plt.subplots() and then after reading the data from the columns of the CSV file we will draw vertical lines by using vlines() function that takes arguments as (x,ymin,ymax) where ymin is the least value and ymax is the greatest value which is to be plotted.
After drawing the vertical lines we will use the plot() function to draw the markers which will be in the shape of circles and finally the lollipop chart will be formed.
Python3
from pandas import *
from matplotlib import pyplot as plt
d = read_csv( "company_sales_data.csv" )
fig, axes = plt.subplots()
axes.vlines(d[ 'month_number' ], ymin = 0 , ymax = d[ 'total_profit' ])
axes.plot(d[ 'month_number' ], d[ 'total_profit' ], "o" )
axes.set_ylim( 0 )
plt.xlabel( 'Month' )
plt.ylabel( 'Profit' )
plt.title( 'Total Profit' )
plt.xticks(d[ 'month_number' ])
|
Output:

Example 3:
This example is same as the second method the only difference is in the plotting of the lines, here we will plot the lines horizontally. So, we will create an empty graph/chart using plt.subplots() and then after reading the data from the columns of the CSV file we will draw vertical lines by using hlines() function that takes argument as (y,xmin,xmax) where xmin is the least value and xmax is the greatest value which is to be plotted.
After drawing the vertical lines again we will use the plot() function to draw the markers which will be in the shape of circles and finally the lollipop chart will be formed. Here we will add different colors to each month so that it looks more attractive, the hlines() function as the option colors to set colors to different lines.
Python3
from pandas import *
from matplotlib import pyplot as plt
d = read_csv( "company_sales_data.csv" )
fig, axes = plt.subplots()
line_colors = [ 'blue' , 'cyan' , 'green' , 'red' ,
'skyblue' , 'brown' , 'yellow' ,
'black' , 'grey' , 'orange' , 'maroon' ,
'lightgreen' ]
axes.hlines(d[ 'month_number' ], xmin = 0 ,
xmax = d[ 'total_profit' ], colors = line_colors)
axes.plot(d[ 'total_profit' ], d[ 'month_number' ], "o" )
axes.set_xlim( 0 )
plt.xlabel( 'Profit' )
plt.ylabel( 'Month' )
plt.title( 'Total Profit' )
plt.yticks(d[ 'month_number' ])
|
Output:

Similar Reads
How to plot a Pandas Dataframe with Matplotlib?
We have a Pandas DataFrame and now we want to visualize it using Matplotlib for data visualization to understand trends, patterns and relationships in the data. In this article we will explore different ways to plot a Pandas DataFrame using Matplotlib's various charts. Before we start, ensure you ha
3 min read
How to Create a Candlestick Chart in Matplotlib?
A candlestick chart, often known as a Japanese candlestick chart, is a financial chart that shows the price movement of stocks, derivatives, and other financial instruments in real-time, there are simply four essential components that must be examined. The open, high, low, and close are the four key
4 min read
How to Create a Table with Matplotlib?
In this article, we will discuss how to create a table with Matplotlib in Python. Method 1: Create a Table using matplotlib.plyplot.table() function In this example, we create a database of average scores of subjects for 5 consecutive years. We import packages and plotline plots for each consecutive
3 min read
Create Scatter Charts in Matplotlib using Flask
In this article, we will see how to create charts in Matplotlib with Flask. We will discuss two different ways how we can create Matplotlib charts in Flask and present it on an HTML webpage with or without saving the plot using Python. File structure Create and Save the Plot in the Static Directory
4 min read
Draw a horizontal bar chart with Matplotlib
Matplotlib is the standard python library for creating visualizations in Python. Pyplot is a module of Matplotlib library which is used to plot graphs and charts and also make changes in them. In this article, we are going to see how to draw a horizontal bar chart with Matplotlib. Creating a vertica
2 min read
Create a grouped bar plot in Matplotlib
A grouped bar plot is a type of bar chart that displays multiple bars for different categories side by side within groups. It is useful for comparing values across multiple dimensions, such as tracking sales across different months for multiple products or analyzing students' performance in differen
3 min read
Hide Axis, Borders and White Spaces in Matplotlib
Matplotlib is a powerful library in Python for data visualization. By default, when we create a plot, it includes axes, labels and borders. However, for creative or minimalistic visualizations, we might want to hide these elements. This article explores various methods to hide axes, labels and white
4 min read
Creating Horizontal Bar Charts using Pandas
Prerequisites: Pandas A bar chart represents categorical data with corresponding data values as rectangular bars. Usually, the x-axis represents categorical values and the y-axis represents the data values or frequencies. This is called a vertical bar chart and the inverse is called a horizontal bar
4 min read
How to create a Scatter Plot with several colors in Matplotlib?
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. In this article, we w
3 min read
How to Create an Empty Figure with Matplotlib in Python?
Creating a figure explicitly is an object-oriented style of interfacing with matplotlib. The figure is a basic building block of creating a plot as Matplotlib graphs our data on figures. This figure keeps track of all other components such as child axes, legends, title, axis, etc. Steps to create an
2 min read