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
Plot a circle with an edgecolor in Matplotlib
To plot a circle with an edgecolor in Matplotlib, you can use the Circle class from matplotlib.patches. This allows you to customize the circle's appearance including edge color and line width.
Steps to Create a Circle with Edgecolor
Create a new figure using
figure()methodAdd a subplot to the current axis
Create a
Circleinstance with specifiededgecolorandlinewidthAdd the circle patch to the plot
Optionally add text using
text()methodSet axis limits and display the figure
Example
Here's how to create a circle with an orange edge ?
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create figure and axis
fig = plt.figure()
ax = fig.add_subplot(111)
# Create circle with orange edge
circle = patches.Circle((0, 0), radius=1, edgecolor="orange", linewidth=7, fill=False)
ax.add_patch(circle)
# Add text
plt.text(-0.25, 0, "Circle")
# Set axis limits and make axes equal
plt.xlim([-4, 4])
plt.ylim([-4, 4])
plt.axis('equal')
plt.show()
Customizing Circle Properties
You can customize various properties of the circle ?
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
# Circle 1: Thick red edge
circle1 = patches.Circle((0, 0), radius=1, edgecolor="red", linewidth=5, fill=False)
axes[0].add_patch(circle1)
axes[0].set_title("Thick Red Edge")
axes[0].set_xlim([-2, 2])
axes[0].set_ylim([-2, 2])
axes[0].axis('equal')
# Circle 2: Filled with blue edge
circle2 = patches.Circle((0, 0), radius=1, edgecolor="blue", facecolor="lightblue", linewidth=3)
axes[1].add_patch(circle2)
axes[1].set_title("Filled with Blue Edge")
axes[1].set_xlim([-2, 2])
axes[1].set_ylim([-2, 2])
axes[1].axis('equal')
# Circle 3: Dashed green edge
circle3 = patches.Circle((0, 0), radius=1, edgecolor="green", linewidth=4, fill=False, linestyle='--')
axes[2].add_patch(circle3)
axes[2].set_title("Dashed Green Edge")
axes[2].set_xlim([-2, 2])
axes[2].set_ylim([-2, 2])
axes[2].axis('equal')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
edgecolor |
Color of the circle's edge | 'red', 'blue', '#FF5733' |
linewidth |
Thickness of the edge | 1, 5, 10 |
fill |
Whether to fill the circle | True, False |
facecolor |
Fill color of the circle | 'lightblue', 'yellow' |
linestyle |
Style of the edge line | '-', '--', ':' |
Conclusion
Use matplotlib.patches.Circle with the edgecolor parameter to create circles with colored edges. Combine with linewidth and other properties to customize the appearance for your visualization needs.
