Comprehensive Python library for creating static, animated, and interactive visualizations
npx @tessl/cli install tessl/pypi-matplotlib@3.10.00
# Matplotlib
1
2
A comprehensive Python library for creating static, animated, and interactive visualizations. Matplotlib provides publication-quality figures across various output formats and interactive environments, serving as the foundational plotting library for the Python scientific ecosystem.
3
4
## Package Information
5
6
- **Package Name**: matplotlib
7
- **Language**: Python
8
- **Installation**: `pip install matplotlib`
9
10
## Core Imports
11
12
```python
13
import matplotlib
14
```
15
16
Common for working with plots:
17
18
```python
19
import matplotlib.pyplot as plt
20
```
21
22
Object-oriented interface:
23
24
```python
25
from matplotlib.figure import Figure
26
from matplotlib.axes import Axes
27
import matplotlib.patches as mpatches
28
import matplotlib.colors as mcolors
29
```
30
31
## Basic Usage
32
33
```python
34
import matplotlib.pyplot as plt
35
import numpy as np
36
37
# Create sample data
38
x = np.linspace(0, 10, 100)
39
y = np.sin(x)
40
41
# Create a simple line plot
42
plt.figure(figsize=(10, 6))
43
plt.plot(x, y, 'b-', linewidth=2, label='sin(x)')
44
plt.xlabel('X values')
45
plt.ylabel('Y values')
46
plt.title('Simple Sine Wave Plot')
47
plt.legend()
48
plt.grid(True)
49
plt.show()
50
51
# Create a scatter plot
52
plt.figure(figsize=(8, 6))
53
plt.scatter(x[::10], y[::10], c='red', s=50, alpha=0.7)
54
plt.xlabel('X values')
55
plt.ylabel('Y values')
56
plt.title('Scatter Plot')
57
plt.show()
58
```
59
60
## Architecture
61
62
The Artist hierarchy enables matplotlib's flexibility and extensibility:
63
64
- **Figure**: Top-level container managing canvas, subplots, and overall layout
65
- **Axes**: Individual plot areas containing data graphics and formatting
66
- **Axis**: X and Y coordinate system components with ticks and labels
67
- **Primitives**: Drawable elements (Line2D, Text, Rectangle, PathCollection, etc.)
68
69
This design allows every visual element to be customized and provides the foundation for matplotlib's role as the base plotting library for the Python scientific ecosystem, including integration with NumPy, pandas, Jupyter notebooks, and hundreds of domain-specific visualization libraries.
70
71
## Capabilities
72
73
### Pyplot Interface
74
75
State-based MATLAB-like interface providing simple plotting functions for quick visualizations. Includes figure management, basic plots, statistical plots, and 2D visualizations with automatic state handling.
76
77
```python { .api }
78
def plot(*args, **kwargs): ...
79
def scatter(x, y, **kwargs): ...
80
def bar(x, height, **kwargs): ...
81
def hist(x, **kwargs): ...
82
def imshow(X, **kwargs): ...
83
def figure(num=None, figsize=None, **kwargs): ...
84
def show(**kwargs): ...
85
def savefig(fname, **kwargs): ...
86
```
87
88
[Pyplot Interface](./pyplot.md)
89
90
### Object-Oriented API
91
92
Direct manipulation of Figure and Axes objects for complex layouts and fine control. This approach provides maximum flexibility for custom visualizations and programmatic plot generation.
93
94
```python { .api }
95
class Figure:
96
def add_subplot(self, *args, **kwargs): ...
97
def add_axes(self, rect, **kwargs): ...
98
def savefig(self, fname, **kwargs): ...
99
100
class Axes:
101
def plot(self, *args, **kwargs): ...
102
def scatter(self, x, y, **kwargs): ...
103
def set_xlabel(self, xlabel, **kwargs): ...
104
def set_ylabel(self, ylabel, **kwargs): ...
105
def legend(self, **kwargs): ...
106
```
107
108
[Object-Oriented API](./object-oriented.md)
109
110
### Color and Styling
111
112
Comprehensive color management with built-in colormaps, color conversion utilities, and styling systems. Supports custom colormaps, normalization, and global style configuration.
113
114
```python { .api }
115
# Color conversion
116
def to_rgba(c, alpha=None): ...
117
def to_hex(c, keep_alpha=False): ...
118
119
# Colormaps
120
class Colormap:
121
def __call__(self, X, alpha=None, bytes=False): ...
122
123
# Configuration
124
rcParams: dict
125
def rc(group, **kwargs): ...
126
def use(style): ...
127
```
128
129
[Color and Styling](./colors-styling.md)
130
131
### Geometric Shapes
132
133
Drawing primitive shapes and complex geometric objects. Includes rectangles, circles, polygons, arrows, and custom patches with styling options.
134
135
```python { .api }
136
class Rectangle:
137
def __init__(self, xy, width, height, **kwargs): ...
138
139
class Circle:
140
def __init__(self, xy, radius, **kwargs): ...
141
142
class Polygon:
143
def __init__(self, xy, **kwargs): ...
144
145
class Arrow:
146
def __init__(self, x, y, dx, dy, **kwargs): ...
147
```
148
149
[Geometric Shapes](./shapes.md)
150
151
### Animation and Interactivity
152
153
Creating animated plots and interactive visualizations. Supports function-based and artist-based animations with multiple export formats.
154
155
```python { .api }
156
class FuncAnimation:
157
def __init__(self, fig, func, frames=None, **kwargs): ...
158
159
class ArtistAnimation:
160
def __init__(self, fig, artists, **kwargs): ...
161
162
def save(self, filename, writer=None, **kwargs): ...
163
```
164
165
[Animation](./animation.md)
166
167
### 3D Plotting
168
169
Three-dimensional visualization capabilities through the mplot3d toolkit. Supports 3D line plots, surface plots, scatter plots, and wireframes.
170
171
```python { .api }
172
from mpl_toolkits.mplot3d import Axes3D
173
174
class Axes3D:
175
def plot(self, xs, ys, zs, **kwargs): ...
176
def scatter(self, xs, ys, zs, **kwargs): ...
177
def plot_surface(self, X, Y, Z, **kwargs): ...
178
def plot_wireframe(self, X, Y, Z, **kwargs): ...
179
```
180
181
[3D Plotting](./3d-plotting.md)
182
183
### Backends and Export
184
185
Output format control and GUI integration. Supports multiple backends for different platforms and export formats including PNG, PDF, SVG, and EPS.
186
187
```python { .api }
188
def use(backend): ...
189
def get_backend(): ...
190
def switch_backend(newbackend): ...
191
192
# Export formats
193
def savefig(fname, dpi=None, format=None, bbox_inches=None): ...
194
```
195
196
[Backends and Export](./backends.md)
197
198
## Global Configuration
199
200
```python { .api }
201
# Main configuration dictionary
202
rcParams: dict
203
204
# Configuration functions
205
def rc(group, **kwargs): ...
206
def rcdefaults(): ...
207
def rc_context(rc=None, fname=None): ...
208
209
# Backend management
210
def use(backend): ...
211
def get_backend(): ...
212
def interactive(b=None): ...
213
```
214
215
## Core Types
216
217
```python { .api }
218
from typing import Union, Optional, Sequence, Any
219
from numpy.typing import ArrayLike
220
221
# Common type aliases
222
ColorType = Union[str, tuple, list]
223
LineStyleType = Union[str, tuple]
224
MarkerType = Union[str, int]
225
ArrayLike = Union[list, tuple, 'numpy.ndarray']
226
```