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
How to create minor ticks for a polar plot in matplotlib?
To create minor ticks for a polar plot in matplotlib, you can manually draw tick marks at specified angular positions. This technique is useful when you need more granular control over tick positioning than the default matplotlib settings provide.
Basic Approach
The process involves creating radial lines at specific angles to simulate minor ticks. Here's how to implement it ?
import numpy as np
import matplotlib.pyplot as plt
# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create radius and theta data points
r = np.arange(0, 5, 0.1)
theta = 2 * np.pi * r
# Add a polar subplot
ax = plt.subplot(111, projection='polar')
# Define tick positions (inner and outer radius)
tick_inner = ax.get_rmax() * 0.97
tick_outer = ax.get_rmax()
# Create minor ticks every 10 degrees
for angle in np.deg2rad(np.arange(0, 360, 10)):
ax.plot([angle, angle], [tick_inner, tick_outer], lw=1, color="red")
# Display the plot
plt.show()
Enhanced Version with Data Plot
Here's a more complete example that includes both the polar plot data and minor ticks ?
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
r = np.arange(0, 5, 0.1)
theta = 2 * np.pi * r
# Create polar plot
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection='polar'))
# Plot the main data
ax.plot(theta, r, 'b-', linewidth=2, label='Data')
# Add minor ticks every 15 degrees
tick_positions = np.arange(0, 360, 15)
for angle in np.deg2rad(tick_positions):
ax.plot([angle, angle], [ax.get_rmax() * 0.95, ax.get_rmax()],
lw=0.8, color="gray", alpha=0.7)
# Customize the plot
ax.set_title("Polar Plot with Minor Ticks", fontsize=14)
ax.legend()
ax.grid(True)
plt.show()
Key Parameters
| Parameter | Purpose | Example Value |
|---|---|---|
np.arange(0, 360, 10) |
Defines tick spacing | Every 10 degrees |
ax.get_rmax() |
Gets maximum radius | Outer tick position |
lw=1 |
Line width | Tick thickness |
color="red" |
Tick color | Visual appearance |
Alternative Using Set_Thetagrids
You can also use matplotlib's built-in method for more standard tick control ?
import numpy as np import matplotlib.pyplot as plt # Create polar plot fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(projection='polar')) # Sample data theta = np.linspace(0, 2*np.pi, 100) r = 1 + 0.3 * np.cos(5*theta) ax.plot(theta, r, 'b-', linewidth=2) # Set major ticks every 30 degrees and minor ticks every 10 degrees ax.set_thetagrids(range(0, 360, 30)) # Major ticks ax.set_thetagrids(range(0, 360, 10), minor=True) # Minor ticks plt.show()
Conclusion
Use manual line plotting for complete control over minor tick appearance and positioning. For standard applications, set_thetagrids() with the minor=True parameter provides a cleaner approach.
