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 complex numbers (Argand Diagram) using Matplotlib?
Complex numbers can be visualized using an Argand diagram, where the real part is plotted on the x-axis and the imaginary part on the y-axis. Matplotlib provides an excellent way to create these plots using scatter plots.
Basic Argand Diagram
Let's start by plotting a simple set of complex numbers ?
import numpy as np
import matplotlib.pyplot as plt
# Create complex numbers
complex_numbers = [1+2j, 3+1j, 2-1j, -1+3j, -2-2j]
# Extract real and imaginary parts
real_parts = [z.real for z in complex_numbers]
imag_parts = [z.imag for z in complex_numbers]
# Create the plot
plt.figure(figsize=(8, 6))
plt.scatter(real_parts, imag_parts, c='red', s=100)
# Add labels and grid
plt.xlabel('Real Part')
plt.ylabel('Imaginary Part')
plt.title('Argand Diagram')
plt.grid(True)
plt.axhline(y=0, color='k', linewidth=0.5)
plt.axvline(x=0, color='k', linewidth=0.5)
plt.show()
Advanced Argand Diagram with Color Mapping
We can enhance the visualization by color-coding points based on their magnitude or phase ?
import numpy as np
import matplotlib.pyplot as plt
# Generate random complex numbers
np.random.seed(42)
data = np.random.rand(20) * 4 - 2 + 1j * (np.random.rand(20) * 4 - 2)
# Create the plot
plt.figure(figsize=(10, 8))
# Plot with color based on magnitude
magnitudes = np.abs(data)
scatter = plt.scatter(data.real, data.imag, c=magnitudes,
cmap='viridis', s=100, alpha=0.7)
# Add colorbar
plt.colorbar(scatter, label='Magnitude |z|')
# Customize the plot
plt.xlabel('Real Part', fontsize=12)
plt.ylabel('Imaginary Part', fontsize=12)
plt.title('Argand Diagram with Color-coded Magnitudes', fontsize=14)
plt.grid(True, alpha=0.3)
plt.axhline(y=0, color='black', linewidth=0.8)
plt.axvline(x=0, color='black', linewidth=0.8)
# Make axes equal for proper circle representation
plt.axis('equal')
plt.show()
Plotting Complex Functions
We can visualize complex functions by plotting multiple complex values ?
import numpy as np
import matplotlib.pyplot as plt
# Create points on unit circle
theta = np.linspace(0, 2*np.pi, 20)
unit_circle = np.exp(1j * theta)
# Apply a function z^2
squared = unit_circle ** 2
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Original unit circle
ax1.scatter(unit_circle.real, unit_circle.imag, c='blue', s=50)
ax1.set_title('Unit Circle: z = e^(i?)')
ax1.set_xlabel('Real Part')
ax1.set_ylabel('Imaginary Part')
ax1.grid(True, alpha=0.3)
ax1.axis('equal')
# Squared values
ax2.scatter(squared.real, squared.imag, c='red', s=50)
ax2.set_title('After Transformation: z² = e^(2i?)')
ax2.set_xlabel('Real Part')
ax2.set_ylabel('Imaginary Part')
ax2.grid(True, alpha=0.3)
ax2.axis('equal')
plt.tight_layout()
plt.show()
Key Features of Argand Diagrams
| Component | Representation | Purpose |
|---|---|---|
| X-axis | Real part | Horizontal position |
| Y-axis | Imaginary part | Vertical position |
| Distance from origin | Magnitude |z| | Absolute value |
| Angle from x-axis | Phase/Argument | Complex angle |
Conclusion
Argand diagrams provide an intuitive way to visualize complex numbers in the complex plane. Use scatter() plots with real and imaginary parts as coordinates, and enhance with color mapping for additional insights into magnitude or phase relationships.
Advertisements
