
上QQ阅读APP看书,第一时间看更新
How to do it...
This section walks through the steps to visualize an array through a scatterplot.
- Import the matplotlib library and configure the library to visualize plots inside of the Jupyter notebook using the following script:
import matplotlib.pyplot as plt
%matplotlib inline
- Next, determine the minimum and maximum values of the x and y-axes of the scatterplot using the min() and max() functions from numpy, as seen in the following script:
min_x = data_array.min(axis=0)[0]-10
max_x = data_array.max(axis=0)[0]+10
min_y = data_array.min(axis=0)[1]-10
max_y = data_array.max(axis=0)[1]+10
- Execute the following script to plot the height and weight for each gender:
# formatting the plot grid, scales, and figure size
plt.figure(figsize=(9, 4), dpi= 75)
plt.axis([min_x,max_x,min_y,max_y])
plt.grid()
for i in range(len(data_array)):
value = data_array[i]
# assign labels values to specific matrix elements
gender = value[2]
height = value[0]
weight = value[1]
# filter data points by gender
a = plt.scatter(height[gender==0],weight[gender==0], marker
= 'x', c= 'b', label = 'Female')
b = plt.scatter(height[gender==1],weight[gender==1], marker
= 'o', c= 'b', label = 'Male')
# plot values, title, legend, x and y axis
plt.title('Weight vs Height by Gender')
plt.xlabel('Height (in)')
plt.ylabel('Weight (lbs)')
plt.legend(handles=[a,b])