Circular Bar Plot in Python
Last Updated :
26 Mar, 2024
In this guide, we'll dive into the world of circular bar plots in Python. We'll explore what they are, why they're useful, and how you can create them to add a stylish touch to your data visualization projects.
What is a Circular Bar Plot?
Circular bar plots, also known as radial bar charts or circular stacked bar plots, are a variation of traditional bar plots. Instead of representing data along a horizontal or vertical axis, circular bar plots display data along a circular or radial axis. Each bar extends from the center of the plot outward, with the length of the bar representing the value of the corresponding data point.
Step to Create Circular Bar Plot in Python
Creating a circular bar plot in Python is easier than you might think. Here are the steps you need to follow:
- Step 1: Import libraries: Import the necessary libraries, such as matplotlib, which is a popular plotting library in Python.
- Step 2: Prepare your data: Organize your data into categories and corresponding values that you want to visualize.
- Step 3: Create the plot: Use matplotlib to create a polar plot (subplot_kw=dict(polar=True)) and add bars using the bar() function. Make sure to adjust the angles of the bars appropriately to create a circular layout.
- Step 4: Customize the plot (optional): You can further customize the plot by adding labels, legends, and titles, or changing the color scheme to suit your preferences.
And that's it! With just a few lines of code, you can create stunning circular bar plots to showcase your data in a visually appealing manner.
Circular Bar Plot in Python
Below, are the code examples, of a circular ar plot in Python.
Example 1: Circular Bar Plot Creation Using Matplotlib
In this example, below Python code uses the Matplotlib library to generate a circular bar plot. It imports necessary modules, defines sample data with categories and values, and then creates a subplot with polar projection. Finally, it generates bars on the polar axes corresponding to each category's value, resulting in a circular bar plot visualization.
Python3
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Category 1', 'Category 2', 'Category 3', 'Category 4']
values = [20, 35, 30, 25]
# Create a circular bar plot
fig, ax = plt.subplots(subplot_kw=dict(polar=True))
bars = ax.bar(np.arange(len(categories)) * 2 * np.pi / len(categories), values)
plt.show()
Output
fig 1. Output of the above code.Example 2: Customizing a Circular Bar Plot in Matplotlib
In this example, below code uses Matplotlib to create a circular bar plot with customizations. It includes sample data, custom colors, and adds labels to each bar. Additional plot adjustments such as starting angle, direction, and axis limits are applied for improved visualization before displaying the final circular bar plot.
Python3
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Category 1', 'Category 2', 'Category 3', 'Category 4']
values = [20, 35, 30, 25]
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'] # Custom colors for bars
# Create a circular bar plot
fig, ax = plt.subplots(subplot_kw=dict(polar=True))
bars = ax.bar(np.arange(len(categories)) * 2 * np.pi /
len(categories), values, color=colors, alpha=1)
# Add labels to each bar
for i, (label, angle) in enumerate(zip(categories, np.arange(len(categories)) * 2 * np.pi / len(categories))):
x = angle
y = values[i] + 1 # Adjust height for label placement
ax.text(x, y, label, ha='center', va='center', fontsize=8, color='black')
# Customizing the plot
ax.set_theta_offset(np.pi / 2) # Offset to start the bars from the top
ax.set_theta_direction(-1) # Clockwise direction for bars
# Adjust the y-axis limit for better visualization
ax.set_ylim(0, max(values) + 5)
ax.set_yticks([]) # Hide the y-axis ticks
ax.spines['polar'].set_visible(False) # Hide the polar axis line
plt.show()
Output
fig 2. Creative way of circular bar
Similar Reads
Circular Bar Plot in seaborn Circular bar plots, also known as radial bar charts or circular histograms, are a visually appealing way to display data. In this article, we'll explore how to create circular bar plots using the Seaborn library in Python. What are circular bar plots?In contrast to conventional bar charts, which dis
5 min read
Circular Linked List in Python A Circular Linked List is a variation of the standard linked list. In a standard linked list, the last element points to null, indicating the end of the list. However, in a circular linked list, the last element points back to the first element, forming a loop. In this article, we will discuss the c
13 min read
Line chart in Matplotlib - Python Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
Bubble chart using Plotly in Python Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
3 min read
Graph Plotting in Python | Set 1 This series will introduce you to graphing in Python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.InstallationThe easiest way to install matplotlib is to use pip. Type the following command in the terminal:Â pip install matplotlibOR, you can d
9 min read
How to Draw a Circle Using Matplotlib in Python? A Circle is a mathematical figure formed by joining all points lying on the same plane and are at equal distance from a given point. We can plot a circle in python using Matplotlib. There are multiple ways to plot a Circle in python using Matplotlib. Method 1: Using matplotlib.patches.Circle() func
3 min read