The target
- Use the basic functions of Matplotlib to implement graphical display
- Use the implementation of multi-graph display
- Use to achieve different drawing types
1.1 know Matplotlib
-
architecture
-
The container layer
-
Canvas– Sketchboard: This is the underlying implementation and doesn’t need attention
-
Figure– Canvas: Built on top of Canvas, it needs to be instantiated each time before it can be used
-
Axes — Coordinate system: Drawing area of data based on Figure
Axis: An Axis in a coordinate system containing size limits, scales, and scale labels
-
-
Auxiliary display layer
- Add coordinate descriptions, titles, etc
-
The image layer
- Set up what image to draw: plot, Scatter…
-
1.2 Basic Plot (Line Plot)
To better understand all of the base drawing capabilities, we integrate all of the base API usage by drawing weather temperature changes.
-
Matplotlib. Pyplot module
Matplotlib. pytplot contains a series of plotting functions similar to MATLAB. Its function applies to axes of the current figure.
import matplotlib.pyplot as plt Copy the code
-
Line drawing
Show the weather of Shanghai for a week, such as the temperature from Monday to Sunday as follows
# 1. Create canvas (container layer) plt.figure(figsize=(10.10)) # 2. Draw line chart (image layer) plt.plot([1.2.3.4.5.6 ,7], [17.17.18.15.11.11.13]) # 3. Display images plt.show() Copy the code
-
Set canvas properties and save images
Plt.figure (figsize=(), dpi=) figsize: specify the length and width of the graph Dpi: specify the sharpness of the image plt.savefig(path)Copy the code
Note: plt.show() frees the figure resource, and saving the image after displaying it will only save the empty image.
-
Add custom X, Y scales
plt.xticks(x, **kwargs) # x: The scale value to display plt.yticks(y, **kwargs) # y: The scale value to display Copy the code
-
Chinese display problem
Refer to the link: www.cnblogs.com/hhh5460/p/4…
-
The grid display
plt.grid(True, linestyle=The '-', alpha=0.5) # alpha for transparency Copy the code
-
Description information
Add description and title of X-axis and Y-axis
plt.xlabel("Time") plt.ylabel("Temperature") plt.title("Diagram of temperature change between 11:0pm and 12:00 PM.") Copy the code
To summarize all the above methods, the complete code is as follows:
import random from matplotlib import pyplot as plt # 1. Generate data x = range(60) y_beijing = [random.uniform(15.25) for i in x] y_shanghai = [random.uniform(20.35) for i in x] # Display global Settings in Chinese plt.rcParams['font.sans-serif'] = ['SimHei'] # 2. Create the canvas plt.figure(figsize=(20.8), dpi=100) # 3. Draw graphics # 1. Plot multiple plots in one graph, multiple times plt.plot(x,y_beijing, label='Beijing') plt.plot(x,y_shanghai, label='Shanghai') # 4. Add x and y scales x_ticks_labels = ['12 {} minutes'.format(i) for i in x] y_ticks = range(40) font = {'family': 'SimHei'.'weight': 'bold'.'size': '10' } Question # shown in Chinese: https://www.cnblogs.com/hhh5460/p/4323985.html # Note: The first argument must be a number. If it is not a number, a value replacement is required # plt.xticks(x_ticks_labels[::5]) plt.xticks(x[::5], x_ticks_labels[::5],fontproperties="SimHei") plt.yticks(y_ticks[20: :5]) # 4.1 Add a grid # properties: # alpha: Transparency # linestyle: The way to draw a grid plt.grid(True, linestyle=The '-', alpha=1) # 4.2 Add description plt.xlabel('time', **font) plt.ylabel('temperature', **font) plt.title('Temperature change in one hour', fontproperties="SimHei") # 4.3 Display legend # 1. You need to declare the actual values in plot before displaying them # Note: Be sure to set a label inside plt.plot(), otherwise it will not display plt.legend(loc=0) # 4.1 Image saving # plt.savefig('./images/02-plot') # 5. Image display plt.show() Copy the code
-
Line chart application
- Used to observe changes in data
- We can graph some mathematical functions
1.3 Common graphics drawing
Matplotlib can draw line charts, scatter charts, bar charts, histograms, pie charts. ** We need to know the meaning of different graphs in order to decide which one to choose to present our data.
-
The line chart
API: PLT. The plot (x, y)
Definition: A graph showing the increase or decrease of a statistic as a rise or fall of a broken line.
Features: can display the trend of data change, reflect the change of things. (change)
-
A scatter diagram
API: PLT. Scatter (x, y)
Definition: use two sets of data to form multiple coordinate points, inspect the distribution of coordinate points, judge whether there is some correlation between two variables or summarize the distribution pattern of coordinate points.
Features: Judge whether there is a quantitative correlation trend between variables and display outliers (distribution law)
-
A histogram
Bar (x, width, align=’center’, **kwargs)
Definition: Data arranged in columns or rows of a worksheet can be plotted on a bar chart.
Features: Draw even discrete data, can see the size of each data at a glance, compare the difference between data. (Statistics/comparison)
-
histogram
API: matplotlib. Pyplot. Hist (x, bins = None)
Definition: A distribution of data represented by a series of longitudinal stripes or line segments of varying heights. Generally, the horizontal axis represents the data range and the vertical axis represents the distribution.
Features: Plot continuous data to show the distribution of one or more sets of data (statistics)
-
The pie chart
API: PLT. Pie (= x, labels, autopct =, colors)
Definition: Used to represent the proportion of different categories and to compare various categories by radian size.
Characteristics: Proportion of classified data (proportion)
summary
Today we will summarize the use of The Jupyter Notebook and the operation of the Matplotlib drawing. Later we will summarize Numpy and Pandas. It’s something you use a lot at work.
See you next time. Bye bye