-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path_matplotlib.qmd
More file actions
91 lines (59 loc) · 2.52 KB
/
_matplotlib.qmd
File metadata and controls
91 lines (59 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
## Matplotlib (by Catherine Cheu)
The `matplotlib` library can provide methods in plotting and arranging data visually in order to help viewers understand the main concepts of the data analysis.
In this chapter, a progression of graphs will be shown to demonstrate some of the capabilities the library has to graph and plot data.
There are several types of graphs that can be used, such as:
1. Scatterplot
2. Line plot
3. 3D plot
The library can be installed using either `pip` or `conda`. For example:
```{python}
# pip install matplotlib
```
### Usage
Let's start with a simple scatter plot. We would need to import the libraries as shown. For this example, we use the pyplot submodule, abbreviated to plt.
We will use randomly generated data in 3 dimensions (x,y,z).
```{python}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(8465);
x = np.random.uniform(0, 3, 10);
y = np.random.uniform(0, 3, 10);
z = np.random.uniform(0, 3, 10);
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
```
We could start plotting another plot, but we have not saved our scatterplot as an object. Thus, it will get overridden by whatever we plot next.
If we want to keep a plot, we can save as a figure object. In addition, if we need multiple plots together, we can use a subplot shown as follows.
```{python}
figure, (fig1, fig2) = plt.subplots(1, 2, figsize = (8, 6))
fig1.scatter(y, z, marker = '^')
fig2.scatter(x, y, color = 'red')
plt.show()
```
We can also do 3d plots alongside 2d, but we need a different function in order to do so. The following uses 3d axes to plot the scatterplot.
```{python}
figure = plt.figure()
# Make 3D axes for fig1
fig2 = figure.add_subplot(122, projection = '3d')
fig1 = figure.add_subplot(121)
# Plot
fig1.plot(x, z, label = "Line Graph")
fig2.scatter(x, y, z, c = z, cmap = 'cool', label = "Scatter in 3D")
fig1.legend()
fig2.legend()
plt.show()
```
### Animation (to be completed)
Animations can also be done through matplotlib. This requires the use of the animation submodule which has a variety functions that can be used to plot animations.
Inputs required include the frames and other functions needed to update the plots per frame.
```{python}
import matplotlib.animation as animation
def updatept(self):
z = 10;
```
We can use the `FuncAnimation(args, updatept(), frames)` to update.
### Conclusion
We have demonstrated some capabilities of the matplotlib library but more complex methods of plotting and arranging visual elements can be found in the documentation.