matplotlib Notes on Machine Learning 1

Posted by stringfield on Tue, 08 Oct 2019 11:42:28 +0200

Scatter plot-scatter

The scatter plot shows the values of two sets of data. The coordinate positions of each point are determined by the values of variables and are completed by a set of disconnected points, which are used to observe the correlation between the two variables.

 1 import numpy as np
 2 import matplotlib.pyplot as plt  #Import Drawing Module
 3 
 4 height = [161, 170, 182, 175, 173, 165]
 5 weight = [50, 58, 80, 70, 69, 55]
 6 
 7 plt.scatter(height, weight)
 8 N = 1000
 9  x = np.random.randn(N)
10 # y1 = np.random.randn(N)
11 y1 = -x + np.random.randn(N)*0.5
12 
13 
14 # The command line for plotting scatterplots
15 plt.scatter(x, y1, s=100, c='r', marker='o', alpha=0.5)
16 # s Represents the area of a point. c Represents the color of a point. marker Represents the shape of a point. alpha Transparency of Representation Points
17 
18 plt.show()

Broken line diagram

A polyline graph is a graph consisting of straight lines connecting data. It is often used to observe the trend of data over time.

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 import matplotlib.dates as mdates
 4 
 5 x = np.linspace(-10, 10, 100)
 6 
 7 # y = x**2
 8 y = np.sin(x)
 9 
10 plt.plot(x, y, linestyle='-.', color='g', marker='^')
11 # The basic drawing command line of the polygraph. linestyle For the lines to be drawn, color For the color of the line, marker The shape of a point
12 # stay matplotlib In the official website, there is a comprehensive introduction about line type, color and point shape.
13 
14 plt.show()

Bar chart

A statistical chart with the length of a rectangle as a variable, used to compare the size of data classified by multiple items, usually for smaller data set analysis.

Bar graphs can be drawn in a single column, side by side, and cascade manner.

 

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 # Single row mode
 5 N = 5
 6 y = [20, 10, 30, 25, 15]
 7 index = np.arange(N)
 8 
 9 # plt.bar(x=index, height=y, width=0.5, color='r')
10 # x It means that x The number of bars corresponding to the axis, height Expressed y The height of the corresponding bar on the axis,
11 # width Represents the width of a bar
12 plt.bar(index, y, 0.5, color='r')  # x=,height=,width=,It can be omitted.
13 
14 
15 # Bar charts can also be placed horizontally
16 # x You need to assign a value of 0. bottom Represents the coordinates on the vertical axis corresponding to the bottom of the bar.
17 # width Represents the horizontal height of the bar, which corresponds to the width of the horizontal axis.
18 # height Represents the longitudinal width of a bar block. orientation='horizontal'Represents drawing a horizontal bar graph.
19 pl = plt.bar(x=0, bottom=index, color='red', width=y, height=0.5, 
20                  orientation='horizontal')
21 # There's a second way for horizontal bar graphs, here's the y Shaft should be assigned the number of bar fast index
22 # pl = plt.barh(y=index, color='red', width=y,)
23 
24 
25 plt.show()

 

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 # Parallel drawing of bar graphs, two bar graphs sharing a coordinate axis, parallel drawing together
 5 index = np.arange(4)
 6 sales_BJ = [52,55,63,53]
 7 sales_SH = [44,66,55,41]
 8 
 9 bar_width = 0.3
10 plt.bar(index,sales_BJ,bar_width,color='b')
11 
12 # The coordinates on the horizontal axis of this parallel bar graph can be used index+bar_width To represent
13 # The aim is not to overlap with the first
14 plt.bar(index+bar_width,sales_SH,bar_width,color='r')
15 
16 plt.show()
 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 
 4 
 5 # Cascading drawing
 6 index = np.arange(4)
 7 sales_BJ = [52,55,63,53]
 8 sales_SH = [44,66,55,41]
 9 
10 bar_width = 0.3
11 plt.bar(index,sales_BJ,bar_width,color='b')
12 
13 # The second object to be cascaded needs to be added bottom=sales_BJ,The object representing the bottom of the cascade is the previous one.
14 plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ)
15 
16 plt.show()

Topics: Python