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 to plot a half-black and half-white circle using Matplotlib?
To create a half-black and half-white circle using Matplotlib, we use the Wedge patch class to draw two semicircular wedges with different colors. This technique is useful for creating yin-yang symbols, pie charts, or visual demonstrations.
Basic Half-Black Half-White Circle
Here's how to create a simple half-black and half-white circle using two wedges ?
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create figure and axes
fig, ax = plt.subplots()
# Define parameters
theta1, theta2 = 0, 180 # Angles for semicircles
radius = 2
center = (0, 0)
# Create two wedges
w1 = Wedge(center, radius, theta1, theta2, fc='black', edgecolor='black')
w2 = Wedge(center, radius, theta2, theta1, fc='white', edgecolor='black')
# Add wedges to the plot
for wedge in [w1, w2]:
ax.add_artist(wedge)
# Set equal scaling and limits
ax.axis('equal')
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
plt.show()
How It Works
The code creates two Wedge objects:
- w1: Black semicircle from 0° to 180° (top half)
- w2: White semicircle from 180° to 0° (bottom half)
- fc: Face color (fill color)
- edgecolor: Border color
Vertical Split Version
To create a vertically split circle instead ?
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
fig, ax = plt.subplots(figsize=(6, 6))
# Vertical split: left and right halves
theta1, theta2 = 90, 270 # Left half
theta3, theta4 = 270, 90 # Right half
radius = 2
center = (0, 0)
# Left half - black
w1 = Wedge(center, radius, theta1, theta2, fc='black', edgecolor='black')
# Right half - white
w2 = Wedge(center, radius, theta3, theta4, fc='white', edgecolor='black')
ax.add_artist(w1)
ax.add_artist(w2)
ax.axis('equal')
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
ax.set_title('Vertically Split Circle')
plt.show()
Parameters
| Parameter | Description | Example |
|---|---|---|
center |
Center coordinates (x, y) | (0, 0) |
radius |
Radius of the circle | 2 |
theta1, theta2 |
Start and end angles in degrees | 0, 180 |
fc |
Face color (fill color) | 'black' |
edgecolor |
Border color | 'black' |
Conclusion
Use Wedge patches with complementary angle ranges to create half-black and half-white circles. The key is setting theta1 and theta2 to cover 180° each, ensuring the two wedges form a complete circle with different colors.
Advertisements
