Customizing Visualizations With Matplotlib - Lab Answer Sheet - Colab
Customizing Visualizations With Matplotlib - Lab Answer Sheet - Colab
Objectives
You will be able to:
Let's give you a head start by generating some data for you to plot:
[ 0 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 92 93 94 95
96 97 98 99] [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106
108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142
144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178
180 182 184 186 188 190 192 194 196 198] [ 0 1 4 9 16 25 36 49 64 81 100 121 144 169
196 225 256 289 324 361 400 441 484 529 576 625 676 729
784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681
1764 1849 1936 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025
3136 3249 3364 3481 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761
4900 5041 5184 5329 5476 5625 5776 5929 6084 6241 6400 6561 6724 6889
7056 7225 7396 7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409
9604 9801]
Import matplotlib.pyplot as plt and set %matplotlib inline for generating inline images in Jupyter notebooks.
x_values = np.arange(0,100)
# Create a numpy array of 25 values from 0 - 1000
y_values = x_values*2
y_values
Now that we have our data all set and Matplotlib in our Python environment, we can try some basic plotting techniques.
keyboard_arrow_down Exercise 1
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 1/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
Perform the following steps in the cell below:
x = np.arange(0,101,20)
print(x)
[ 0 20 40 60 80 100]
y = np.arange(0,201,20)
print(y)
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 2/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
This was easy, let's move on to drawing multiple plots within a figure space.
keyboard_arrow_down Exercise 2
Perform following actions:
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 3/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
keyboard_arrow_down Exercise 3
As you might have noticed, the y-axis of those graphs automatically adjusted based on the value of y_new . This creates the appearance of all
of the lines having the same slope, even though they actually have quite different slopes.
Repeat the above exercise, but standardize the axes of all of your subplots so that you can more easily compare the slopes of the lines.
Because the final graph goes up to 1200, use this as the maximum for all plots.
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 4/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 5/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
keyboard_arrow_down Exercise 4
Perform the following steps in the cell below:
Using plt.subplots , create a figure of size 8 by 6 with 2 columns, and "unpack" the 2 created axes into variables ax1 and ax2 .
Plot ( x , y ) and ( x , z ) on ax1 and ax2 respectively.
Set the line width of first axes to 3, line style as dotted and color it red.
Set the line width of second axes to 5, line style as dash-dot (-.) and color it blue.
Give the plots some labels and titles
Hints:
If y is looking "off" but your graph code seems correct, it's possible you overwrote the original values in a previous exercise. Go back to
the top of the notebook and re-run the first cell that created x , y , and z .
The label variable - z is intentionally overlapping the graph on the left. We will address that issue later in the lab.
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 6/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
x_values = np.arange(0,100)
print(x_values)
[ 0 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 92 93 94 95
96 97 98 99]
y_values = x_values*2
print(y_values)
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106
108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142
144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178
180 182 184 186 188 190 192 194 196 198]
z_values = x_values**2
print(z_values)
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 7/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
keyboard_arrow_down Exercise 5
The above figure looks fine but a bit out of proportion. Let's resize this to make the plots look more appealing by ensuring that subplots are
square in shape. Also change the line style of first plot (left) and change the type of 2nd plot (right) to a scatter plot with a ^ marker style.
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 8/9
2/4/25, 3:36 PM Customizing Visualizations with Matplotlib - Lab Answer sheet - Colab
Note: Instead of changing the plot size as you did in Exercise 5, one other technique you could have used to help with overlapping plot labels
is a "tight layout" (see Matplotlib guide).
By default, Matplotlib doesn't consider the space taken by axes labels when it determines how to draw the plots. Turning on the tight layout
setting tells Matplotlib to include the axes labels in this calculation, in order to avoid clipping or overlapping.
ax1.set_xlabel('variable - x')
ax1.set_ylabel('variable - y')
ax1.set_title ('Left Plot')
ax2.set_xlabel('variable - x')
ax2.set_ylabel('variable - z')
ax2.set_title ('Right Plot');
https://colab.research.google.com/drive/10T4J6qFRXYmZojyfBl-7ar0SyUCkvXtW#printMode=true 9/9