0
# Visualization and Plotting
1
2
Plotting and visualization functions for mathematical functions, complex analysis, and numerical data. These functions provide high-quality visualizations with arbitrary precision support.
3
4
## Capabilities
5
6
### Function Plotting
7
8
Plot real-valued functions with high precision and customizable appearance.
9
10
```python { .api }
11
def plot(f, xlim=[-5, 5], ylim=None, points=200, file=None, **kwargs):
12
"""
13
Plot a real-valued function of one variable.
14
15
Args:
16
f: Function to plot (callable)
17
xlim: x-axis range as [xmin, xmax] (default: [-5, 5])
18
ylim: y-axis range as [ymin, ymax] (auto if None)
19
points: Number of sample points (default: 200)
20
file: Output file name (optional)
21
**kwargs: Additional plotting options
22
23
Returns:
24
Plot object (displays plot if in interactive mode)
25
"""
26
```
27
28
### Complex Function Plotting
29
30
Visualize complex functions using color mapping and domain coloring techniques.
31
32
```python { .api }
33
def cplot(f, re=[-2, 2], im=[-2, 2], points=300, color=None, verbose=False, file=None, **kwargs):
34
"""
35
Plot a complex function using domain coloring.
36
37
Args:
38
f: Complex function to plot (callable)
39
re: Real axis range as [re_min, re_max] (default: [-2, 2])
40
im: Imaginary axis range as [im_min, im_max] (default: [-2, 2])
41
points: Number of sample points per axis (default: 300)
42
color: Color mapping function (optional)
43
verbose: Print progress information (default: False)
44
file: Output file name (optional)
45
**kwargs: Additional plotting options
46
47
Returns:
48
Complex plot object with color-coded magnitude and phase
49
"""
50
```
51
52
### Surface Plotting
53
54
Create 3D surface plots for functions of two variables.
55
56
```python { .api }
57
def splot(f, u=[-2, 2], v=[-2, 2], points=[50, 50], keep_aspect=True, wireframe=False, file=None, **kwargs):
58
"""
59
Create 3D surface plot for function of two variables.
60
61
Args:
62
f: Function f(x,y) to plot (callable)
63
u: Range for first variable as [u_min, u_max] (default: [-2, 2])
64
v: Range for second variable as [v_min, v_max] (default: [-2, 2])
65
points: Number of sample points as [n_u, n_v] or single int (default: [50, 50])
66
keep_aspect: Maintain aspect ratio (default: True)
67
wireframe: Use wireframe rendering (default: False)
68
file: Output file name (optional)
69
**kwargs: Additional plotting options
70
71
Returns:
72
3D surface plot object
73
"""
74
```
75
76
### Usage Examples
77
78
```python
79
import mpmath
80
from mpmath import mp
81
82
# Set precision
83
mp.dps = 25
84
85
# Example 1: Plot a simple function
86
def f(x):
87
return mp.sin(x) * mp.exp(-x/5)
88
89
# Create a basic plot
90
mp.plot(f, xlim=[-2*mp.pi, 2*mp.pi])
91
92
# Plot with custom styling
93
mp.plot(f, xlim=[0, 4*mp.pi], ylim=[-0.5, 1.1], points=500,
94
file='damped_sine.png')
95
96
# Example 2: Plot special functions
97
def gamma_plot(x):
98
try:
99
return mp.gamma(x)
100
except:
101
return mp.nan
102
103
# Plot gamma function with restricted y-range
104
mp.plot(gamma_plot, xlim=[-4, 4], ylim=[-10, 10], points=1000)
105
106
# Example 3: Complex function plotting
107
def complex_sin(z):
108
return mp.sin(z)
109
110
# Domain coloring plot of sin(z)
111
mp.cplot(complex_sin, re=[-mp.pi, mp.pi], im=[-mp.pi/2, mp.pi/2],
112
points=400, file='complex_sine.png')
113
114
# Plot a more complex function
115
def rational_func(z):
116
return (z**3 - 1) / (z**2 + 1)
117
118
mp.cplot(rational_func, re=[-3, 3], im=[-3, 3], points=500)
119
120
# Example 4: Plot Riemann zeta function
121
def zeta_plot(z):
122
try:
123
return mp.zeta(z)
124
except:
125
return mp.nan
126
127
# Critical strip visualization
128
mp.cplot(zeta_plot, re=[-1, 2], im=[-20, 20], points=600,
129
file='riemann_zeta.png')
130
131
# Example 5: Surface plotting
132
def surface_func(x, y):
133
return mp.sin(mp.sqrt(x**2 + y**2)) / mp.sqrt(x**2 + y**2 + 1)
134
135
# Create 3D surface plot
136
mp.splot(surface_func, u=[-5, 5], v=[-5, 5], points=[60, 60])
137
138
# Wireframe version
139
mp.splot(surface_func, u=[-3, 3], v=[-3, 3], points=[40, 40],
140
wireframe=True, file='surface_wireframe.png')
141
142
# Example 6: Plotting Bessel functions
143
def bessel_family(x):
144
return [mp.besselj(n, x) for n in range(4)]
145
146
# Plot multiple Bessel functions
147
for n in range(4):
148
mp.plot(lambda x: mp.besselj(n, x), xlim=[0, 20], ylim=[-0.5, 0.6])
149
150
# Example 7: Plot elliptic functions
151
def jacobi_sn(x):
152
return mp.jacobi('sn', x, 0.5)
153
154
def jacobi_cn(x):
155
return mp.jacobi('cn', x, 0.5)
156
157
def jacobi_dn(x):
158
return mp.jacobi('dn', x, 0.5)
159
160
# Plot Jacobi elliptic functions
161
mp.plot(jacobi_sn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
162
mp.plot(jacobi_cn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
163
mp.plot(jacobi_dn, xlim=[0, 4*mp.ellipk(0.5)], points=400)
164
165
# Example 8: Complex analysis - visualize analytic continuation
166
def gamma_complex(z):
167
try:
168
return mp.gamma(z)
169
except:
170
return mp.inf
171
172
# Visualize gamma function in complex plane
173
mp.cplot(gamma_complex, re=[-4, 4], im=[-4, 4], points=500,
174
file='gamma_complex.png')
175
176
# Example 9: Plot eigenvalue problems
177
# Visualize characteristic polynomial
178
def char_poly(lam):
179
# Example: characteristic polynomial of 2x2 matrix
180
return lam**2 - 3*lam + 2
181
182
mp.plot(char_poly, xlim=[-1, 4], ylim=[-2, 3])
183
184
# Example 10: Advanced surface plots with mathematical functions
185
def riemann_surface(x, y):
186
z = x + 1j*y
187
try:
188
return mp.re(mp.sqrt(z)) # Real part of sqrt(z)
189
except:
190
return 0
191
192
mp.splot(riemann_surface, u=[-2, 2], v=[-2, 2], points=[50, 50],
193
file='riemann_surface_real.png')
194
195
# Imaginary part
196
def riemann_surface_imag(x, y):
197
z = x + 1j*y
198
try:
199
return mp.im(mp.sqrt(z)) # Imaginary part of sqrt(z)
200
except:
201
return 0
202
203
mp.splot(riemann_surface_imag, u=[-2, 2], v=[-2, 2], points=[50, 50],
204
file='riemann_surface_imag.png')
205
206
print("Visualization examples completed!")
207
print("Check generated PNG files for visual results.")
208
```
209
210
### Plotting Features and Options
211
212
The mpmath plotting functions provide several advanced features:
213
214
#### Color Mapping for Complex Functions
215
- **Magnitude**: Represented by brightness/saturation
216
- **Phase**: Represented by hue (color wheel mapping)
217
- **Zeros**: Appear as black points
218
- **Poles**: Appear as white points
219
- **Branch cuts**: Visible as discontinuities in color
220
221
#### Customization Options
222
- **Resolution**: Adjustable point density for smooth curves
223
- **Output formats**: PNG, SVG, PDF support
224
- **Styling**: Line width, colors, markers
225
- **Axis control**: Custom ranges and labels
226
- **3D rendering**: Surface plots with lighting and shading
227
228
#### Performance Considerations
229
- High precision computation may slow rendering
230
- Use appropriate point density for balance of quality and speed
231
- Complex function plots are more computationally intensive
232
- File output is recommended for high-resolution plots
233
234
### Mathematical Applications
235
236
These visualization tools are particularly valuable for:
237
238
- **Complex Analysis**: Domain coloring reveals function behavior
239
- **Special Functions**: Visualize oscillations, asymptotic behavior
240
- **Numerical Analysis**: Check convergence and stability
241
- **Educational Purposes**: Illustrate mathematical concepts
242
- **Research**: Explore function properties and relationships
243
- **Publication**: Generate high-quality figures for papers
244
245
The arbitrary precision support ensures that even functions with challenging numerical properties can be visualized accurately, making these tools invaluable for mathematical research and education.