👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page
  • 1. Univariate Lollipop
  • 2. Highlight Lollipop
  • 3. Multivariate Lollipop

Was this helpful?

  1. 3. Matplotlib

3.7 Lollipop Chart

Previous3.6 Scatter ChartNext3.8 Pie Chart

Last updated 4 years ago

Was this helpful?

A lollipop plot is a hybrid between a and a . It shows the relationship between a numerical variable and another variable, numerical or categorical. It's better to decrease the order and represent horizontal lines.

First, Let's create a dataset.

# Create a dataset
df = pd.DataFrame({'group':list(map(chr, range(70, 90))), 'values':np.random.uniform(size=20)})

ordered_df = df.sort_values(by='values')  # reorder it by 'values'   
my_range=range(1,len(df.index)+1)         # calcaulate data range

1. Univariate Lollipop

plt.style.use('dark_background')  # set the figure style 
plt.figure(figsize = (14,8))      # set the figure size

#create lollipop bar and lollipop head  
plt.hlines(y=my_range, xmin=0, xmax=ordered_df['values'], color='skyblue')
plt.plot(ordered_df['values'], my_range, "o") 

plt.yticks(my_range, ordered_df['group'])   # reset yticks 
plt.title("Lollipop Plot", loc='left')      # set title and title location
plt.xlabel('Score')
plt.ylabel('Group')

2. Highlight Lollipop

Moreover, we .can add some eye-catching features for storytelling. For example, highlight a group that specifically interests you with a bigger size and different colors.

# Create a color if the group is "A"
A_color=np.where(ordered_df ['group']=='A', 'gold', 'skyblue')
A_size=np.where(ordered_df ['group']=='A', 100, 30)

plt.hlines(y=my_range, xmin=0, xmax=ordered_df['values'], color=my_color, linewidth =2)
plt.scatter(ordered_df['values'], my_range, color=A_color, s=A_size)
 
# Add title and axis names
plt.yticks(my_range, ordered_df['group'])
plt.title("Highlight A Lollipop", loc='left')
plt.xlabel('Score')
plt.ylabel('Group')

3. Multivariate Lollipop

We can use multi-variables lollipop if you have more than one observation for each group. Instead of displaying the values of both groups one beside each other, show them on the same line, and represent the difference.

Now we need to create a new dataset with two variables.

# Create a dataframe
var1 =np.random.uniform(size=15)
var2=value1+np.random.uniform(size=15)/ 3
df = pd.DataFrame({'group':list(map(chr, range(70, 80))), 
                'value1':var1 , 'value2':var2 })
 
ordered_df = df.sort_values(by='value1')
my_range=range(1,len(df.index)+1)
# create white color lollipop bar
plt.hlines(y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'], color='w',alpha = 0.6)

# create lollipop head1 in magnet color and set size =80
plt.scatter(ordered_df['value1'], my_range, color='m', s = 80,label='var 1',marker='^')

# create lollipop head2 in cyan color and set size = 100 
plt.scatter(ordered_df['value2'], my_range, color='c',s = 100, label='var 2')

plt.legend() # show legend 
 
# Add title and axis names
plt.yticks(my_range, ordered_df['group'])
plt.title("Multivariable Lollipop", loc='left')
plt.xlabel('Score')
plt.ylabel('Group')

scatter plot
barplot
Figure: Univariate Lollipop
Figure: Highlight Lollipop
Figure: Multivariate Lollipop