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
Selected Reading
How do I put a circle with annotation in matplotlib?
To put a circle with annotation in matplotlib, we can create a visual highlight around a specific data point and add descriptive text with an arrow pointing to it. This is useful for drawing attention to important data points in plots.
Steps to Create Circle with Annotation
Here's the process to add a circled marker with annotation ?
- Set the figure size and adjust the padding between and around the subplots
- Create data points using numpy
- Get the point coordinate to put circle with annotation
- Get the current axis
- Plot the data points using plot() method
- Set X and Y axes scale
- To put a circled marker, use the plot() method with marker='o' and specific properties
- Annotate that circle with arrow style using annotate() method
- To display the figure, use show() method
Example
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data points
data = np.array([[5, 3, 4, 4, 6],
[1, 5, 3, 2, 2]])
# Select the point to highlight (index 2)
point = data[:, 2]
ax = plt.gca()
# Plot all data points
ax.plot(data[0], data[1], 'o', ms=10, color='red')
# Set axis limits
ax.set_xlim([2, 8])
ax.set_ylim([0, 6])
# Create circle around the selected point
radius = 15
ax.plot(point[0], point[1], 'o',
ms=radius * 2, mec='yellow', mfc='none', mew=2)
# Add annotation with arrow
ax.annotate('Circled Marker', xy=point, xytext=(60, 60),
textcoords='offset points',
color='green', size='large',
arrowprops=dict(
arrowstyle='simple,tail_width=0.3,head_width=0.8,head_length=0.8',
facecolor='b', shrinkB=radius * 1.2)
)
plt.show()
Key Parameters Explained
Understanding the important parameters for creating circles and annotations ?
- ms ? marker size for the circle
- mec ? marker edge color (circle outline)
- mfc ? marker face color ('none' for transparent)
- mew ? marker edge width (circle thickness)
- xy ? coordinates of the point being annotated
- xytext ? position of the text relative to the point
- textcoords ? coordinate system for text positioning
- arrowprops ? dictionary defining arrow appearance
Alternative Approach Using Circle Patch
You can also use matplotlib patches for more precise circle control ?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
# Create figure and axis
fig, ax = plt.subplots(figsize=(7, 4))
# Sample data
x_data = [1, 2, 3, 4, 5]
y_data = [2, 4, 1, 5, 3]
# Plot data points
ax.plot(x_data, y_data, 'ro', markersize=8)
# Add circle around specific point (index 2)
highlight_point = (x_data[2], y_data[2])
circle = Circle(highlight_point, radius=0.3, fill=False,
color='blue', linewidth=3)
ax.add_patch(circle)
# Add annotation
ax.annotate('Important Point',
xy=highlight_point,
xytext=(20, 20),
textcoords='offset points',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.7),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
ax.set_xlim(0, 6)
ax.set_ylim(0, 6)
plt.show()
Conclusion
Use plot() with marker properties for simple circles, or Circle patches for more control. The annotate() method provides flexible text positioning with customizable arrows for highlighting important data points.
Advertisements
