Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Circular (polar) histogram in Python
A circular (polar) histogram displays data in a circular format using polar coordinates. This visualization is particularly useful for directional data like wind directions, angles, or cyclic patterns.
Creating a Basic Polar Histogram
To create a polar histogram, we need three main components: angles (theta), radii (heights), and bar widths. Here's how to create one ?
import numpy as np
import matplotlib.pyplot as plt
# Set up data
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
# Create polar subplot
ax = plt.subplot(111, projection='polar')
# Create bar plot
bars = ax.bar(theta, radii, width=width, bottom=0.0)
# Customize colors and transparency
for r, bar in zip(radii, bars):
bar.set_facecolor(plt.cm.rainbow(r / 10.0))
bar.set_alpha(0.5)
plt.show()
How It Works
The key steps for creating a polar histogram are:
- theta − Angular positions (in radians) where bars are placed
- radii − Heights of the bars (distance from center)
- width − Angular width of each bar
- projection='polar' − Converts the subplot to polar coordinates
- set_facecolor() − Applies color mapping based on bar height
- set_alpha() − Controls transparency (0.0 = transparent, 1.0 = opaque)
Customizing Colors and Appearance
You can customize the appearance by using different colormaps and styling options ?
import numpy as np
import matplotlib.pyplot as plt
# Generate data
N = 16
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = np.random.uniform(5, 15, N)
width = np.full(N, 2 * np.pi / N) # Equal width bars
# Create polar plot
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
bars = ax.bar(theta, radii, width=width, bottom=0.0)
# Apply color scheme
colors = plt.cm.viridis(np.linspace(0, 1, N))
for bar, color in zip(bars, colors):
bar.set_facecolor(color)
bar.set_alpha(0.8)
bar.set_edgecolor('black')
bar.set_linewidth(0.5)
ax.set_title("Customized Polar Histogram", pad=20)
plt.show()
Common Use Cases
Polar histograms are ideal for visualizing:
- Wind direction frequency − Showing prevailing wind patterns
- Angular measurements − Distribution of angles or orientations
- Cyclic data − Time-based patterns (hours, months, seasons)
- Directional statistics − Compass bearings or navigation data
Conclusion
Polar histograms provide an effective way to visualize directional or cyclic data using matplotlib's polar projection. The combination of angles, radii, and customizable colors makes them perfect for displaying patterns that have angular components.
---