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 draw a filled arc in Matplotlib?
To draw a filled arc in Matplotlib, you can use the fill_between() method combined with mathematical functions to create curved shapes. This technique is useful for creating semicircles, arcs, and other curved filled regions in your plots.
Steps to Create a Filled Arc
Set the figure size and adjust the padding between and around the subplots.
Create a figure and a set of subplots.
Initialize variables for radius and vertical offset.
Create x and y data points using NumPy.
Fill the area between x and y plots using
fill_between().Set the axis aspect ratio to "equal" for proper circle shape.
Display the figure using show() method.
Basic Filled Arc Example
Here's how to create a filled semicircle using the equation of a circle −
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots(1, 1)
# Define radius and vertical offset
radius = 2.0
y_offset = -1
# Create x values from -1 to 1
x = np.arange(-1.0, 1.05, 0.05)
# Calculate y values using circle equation: y = sqrt(r² - x²)
y = np.sqrt(radius - x ** 2) + y_offset
# Fill the area between the curve and y=0
ax.fill_between(x, y, 0, alpha=0.7, color='blue')
# Set axis properties
ax.axis([-2, 2, -2, 2])
ax.set_aspect("equal")
ax.grid(True, alpha=0.3)
plt.title("Filled Arc (Semicircle)")
plt.show()
Displays a blue filled semicircle with the flat edge at y=0
Customizing Arc Appearance
You can customize the arc's color, transparency, and add multiple arcs −
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
# Create multiple arcs with different parameters
x = np.linspace(-2, 2, 100)
# Upper semicircle
y1 = np.sqrt(4 - x**2)
ax.fill_between(x, y1, 0, alpha=0.6, color='red', label='Upper Arc')
# Lower semicircle
y2 = -np.sqrt(4 - x**2)
ax.fill_between(x, 0, y2, alpha=0.6, color='green', label='Lower Arc')
# Set equal aspect ratio and labels
ax.set_aspect("equal")
ax.grid(True, alpha=0.3)
ax.legend()
ax.set_title("Multiple Filled Arcs")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
plt.show()
Displays two filled semicircles - red upper arc and green lower arc forming a complete circle
Creating Partial Arcs
To create arcs that span only a portion of a circle, limit the x-range −
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
# Create a quarter-circle arc
x = np.linspace(0, 2, 50) # Only positive x values
y = np.sqrt(4 - x**2) # Upper semicircle equation
# Fill the quarter arc
ax.fill_between(x, y, 0, alpha=0.7, color='purple', label='Quarter Arc')
# Add styling
ax.set_aspect("equal")
ax.grid(True, alpha=0.3)
ax.legend()
ax.set_title("Partial Filled Arc (Quarter Circle)")
ax.set_xlim(-0.5, 2.5)
ax.set_ylim(-0.5, 2.5)
plt.show()
Displays a purple filled quarter-circle in the first quadrant
Key Parameters
| Parameter | Description | Example |
|---|---|---|
alpha |
Transparency (0-1) | alpha=0.7 |
color |
Fill color | color='blue' |
set_aspect("equal") |
Maintains circle shape | Prevents oval distortion |
Conclusion
Use fill_between() with circle equations to create filled arcs in Matplotlib. Remember to set set_aspect("equal") to maintain proper circular shapes and customize colors and transparency for better visualization.
