Chapter 2 Matplotlib Drawing Basics

Lesson 2 Introduction to Matplotlib and drawing simple line graphs

insert image description here
The drawing method of the line plt.plot, providing x-axis coordinates and y-axis coordinates at the same time

insert image description here
insert image description here

Lesson 3 Legend and title

The x-axis data is available by default, as shown below
insert image description here
insert image description here

What does the x-axis represent? What does the y-axis represent?
insert image description here
Distance:
insert image description here
Adjust font size:
insert image description here
Draw 2 images on the same canvas
insert image description here
We can’t tell which is data1 and which is data2, add legend label+legend, must have to plt.legend()display the label.
insert image description here
Adjust the position of the legend: by default, it will be placed in the most reasonable position plt.legend(loc=?)
insert image description here
plt.legend(loc=0), and it will be placed in the best position
insert image description here
. 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='–'
insert image description here
color=k, k means black,
insert image description here
highlighting data points ( The actual point) o and ^
insert image description here
add grids, which is conducive to qualitative analysis, and
insert image description here
the method of looking at function parameters: just press Enter
insert image description here
insert image description here

Generate a data2, draw the line thicker linewidth, you can abbreviate lw, color can be abbreviated as c, linestyle can be abbreviated as ls
insert image description here
linewidth, you can abbreviate lw, color can be abbreviated as c, linestyle can be abbreviated as ls
insert image description here
and more abbreviated:
insert image description here
insert image description here

Lesson 4 Draw a bar graph

Compare the size of different data, observe the overall distribution of data
insert image description here
Modify the color, add grid,
insert image description here

Horizontal display (x-axis and y-axis exchange), color is changed to blue
insert image description here
stacked bar chart, bottom=y means that the bottom is y as the bottom
insert image description here
, 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()

insert image description here

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()

insert image description here
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)

insert image description here
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 figsizebe 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.
insert image description here

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(1100100)  # 0~100 之间选取100个整数
bins = [0, 10, 20, 30, 50, 70, 80, 100]
plt.hist(x, bins)

insert image description here

Guess you like

Origin blog.csdn.net/weixin_46713695/article/details/130788782