Table of contents
Lesson 2 Introduction to Matplotlib and drawing simple line graphs
The drawing method of the line plt.plot
, providing x-axis coordinates and y-axis coordinates at the same time
Lesson 3 Legend and title
The x-axis data is available by default, as shown below
What does the x-axis represent? What does the y-axis represent?
Distance:
Adjust font size:
Draw 2 images on the same canvas
We can’t tell which is data1 and which is data2, add legend label+legend, must have to plt.legend()
display the label.
Adjust the position of the legend: by default, it will be placed in the most reasonable position plt.legend(loc=?)
plt.legend(loc=0)
, and it will be placed in the best position
. What should I do if there are two black and white lines? next class
Lesson 4 Custom graphic style
What if there are two black and white lines? We have a dotted line and a solid line.
The color we usually use is color=rgb, r is red, g is green, b is black and drawn
as a dotted line, linestyle='–'
color=k, k means black,
highlighting data points ( The actual point) o and ^
add grids, which is conducive to qualitative analysis, and
the method of looking at function parameters: just press Enter
Generate a data2, draw the line thicker linewidth
, you can abbreviate lw, color can be abbreviated as c, linestyle can be abbreviated as ls
linewidth
, you can abbreviate lw, color can be abbreviated as c, linestyle can be abbreviated as ls
and more abbreviated:
Lesson 4 Draw a bar graph
Compare the size of different data, observe the overall distribution of data
Modify the color, add grid,
Horizontal display (x-axis and y-axis exchange), color is changed to blue
stacked bar chart, bottom=y means that the bottom is y as the bottom
, there are two sets of data, all drawn with bar charts, and matplotlib will automatically Choose a different color for us
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
y1 = [5, 2, 4, 7]
x1 = [2, 4, 6, 8]
plt.bar(x, y)
plt.bar(x, y1)
plt.show()
Lesson 6 Histogram and setting image size
It looks similar to a bar chart. In order to show the distribution of data, for example, output 1000 random numbers and want to know their distribution
data = np.random.randn(1000)
plt.hist(data)
plt.show()
It can be seen from the figure that the data distribution is between [-3, 3], and the position distribution of 0 is the most.
want to see carefully
plt.hist(data, bins=30)
plt.figure()
: Create a canvas and initialize a canvas.
Why can it be drawn without creating a canvas just now? When we directly use plt to draw, for example, use plt.hist(), the default matplotlib in the background runs commands for us plt.figure()
, and the size of the displayed pictures is the default size of plt. When we want to artificially specify the size of the picture you display , it can figsize
be defined with
plt.figure(figsize=(8, 6)) # 8 * 6 的图片
plt.hist(data, bins=30)
As can be seen in the figure below, the graph becomes larger and clearer.
What segments do you want to divide bins into, and then check the distribution in these segments, and artificially stipulate bins
x= np.random.randint(1,100, 100) # 0~100 之间选取100个整数
bins = [0, 10, 20, 30, 50, 70, 80, 100]
plt.hist(x, bins)