SoFunction
Updated on 2024-11-12

python data analysis matplotlib basic plotting usage

preamble

Hello, I'm Su Liang, in the previous we have learned the web crawler and get the data, the next step is of course to analyze the data ah, this post takes you into the new module: pyhon data analysis basics matplotlib basic plotting.

(i) What is matplotlib?

1.why learn matplotlib

Can visualize and present data more intuitively. Make the data more objective and persuasive.

2. What is matplotlib

matplotlib: the most popular python underlying plotting library, mainly do data visualization charts, the name taken from MATLAB, imitation MATLAB build.

3. Installation of matplotlib

Enter the following commands in the terminal for a quick installation

pip install matplotlib -i /simple

4. Import matplotlib library

Importing the module pyplot in matplotlib

from matplotlib import pylot as plt

(ii) Basic points of matplotlib

Basic use

The horizontal axis in the graph below represents the x-axis and the vertical axis represents the y-axis.

And axis axis refers to coordinate axes like x or y.

Note: The values of x and y here should correspond one to one

So what is every red dot on it?

Each red point is a coordinate, and the coordinates of the five points are connected to a line to form a line graph.

So how exactly do you draw it through code? See how simple matplotlib should be with a small example below!

Question: Assume that the temperatures at 2-hour intervals during the day are (15, 13, 14, 15, 17, 24, 26, 25.5, 26, 28, 14, 13)

Code Implementation:

from matplotlib import pyplot as plt
# Every two hours of the day range(2,26,2)
x = range(2,26,2)
y = [15,13,14,15,17,24,26,25.5,26,28,14,13]
# Where the values of x and y correspond one to the other #
# Pass in the values of x and y to plot a line graph.
(x,y)
()

Results.

2. Refine the graph drawn by matplotlib

Setting the image size

To set the image size, you need to call figure, and then you can set the size of the image inside. When the picture is blurred, you can pass in the dpi parameter, which can make the picture clearer.

fig = (figsize=(10,10),dpi=100)

Save to local

('./picture/')

Here you can also save it in svg vector format so that zooming in won't distort it. Run the result:

Add description information (x-axis, y-axis...)

('time') # Set x-axis description information
('temp')# Set y-axis description information
('total')# Setting the title

Adjusting the scale spacing of the x and y axes

The xticks method is required.

(x)

Use the list's infrequent (spaced fetch) for when the scale is too dense.

(x[::2])

Setting the Chinese display

First import font_manager

from matplotlib import font_manager

Next, find the local fonts on your system and drag them to the current directory where you passed the fonts to fname.

my_font = font_manager.FontProperties(fname="./")

fontproperties set Chinese display

('Time',fontproperties = my_font)
('Temperature',fontproperties = my_font)
('24-hour real-time temperature' ,fontproperties = my_font

Run results:

Line styles

It can be specified at the time of drawing:

color = 'r' #Set line color linestyle = '-' #Set line style linewidth = 2 #Set line thickness alpha = 0.5 #Set line Transparency

Below are some of the color abbreviations:

Some of the line styles are below:

(x,y,color = 'r' ,linestyle = '--',linewidth =3 ,alpha = 0.2)

Run results:

Add Watermark

(x=0.45, 
	y=0.45, 
	s= 'Su Liang.py',
	fontproperties = my_font,
    fontsize=40, color='b',
    ha='center', va='center', alpha=0.2)

Run results:

Above is the detailed content of python data analysis matplotlib basic plotting use, more information about python data analysis matplotlib plotting please pay attention to my other related articles!