当前位置:网站首页>Matplotlip basic drawing learning of data analysis - 01

Matplotlip basic drawing learning of data analysis - 01

2022-06-12 00:13:00 Little apes are not bald

Yes matplotlip The understanding of the :

matplotlip yes Pychon An open source project in , by Pychon Provides a powerful data mapping package .

Let's draw a legend to learn matplotlip Basic knowledge related to

example : draw 10—12 Point temperature over time

#  list a Express 10 Point to 12 The temperature of every minute of the hour , How to draw a broken line statistical chart to observe the temperature change every minute 
# a = [random.randint(20,35) for i in range(120)]
# random.randint( Parameter one , Parameter two ), Parameters 1、 Parameters 2 It has to be an integer 
#  This function returns an argument 1 And parameters 2 Any integer between ,  Closed interval 
# Pilot Kit ,
from matplotlip import plot as plt
# Pass in x Axis y Axis data 
x = range(0,120)
y = [random.randint(20,35) for i in range(120)]
# Draw , Pass in x,y data , call plot() function 
plt.plot(x,y)
# Show the image 
plt.show()

The drawn image is as follows , But we found some problems , Such as x Axis y No prompt for axis ,x The coordinate time format of the axis is not equal . Let's revise it

 

#  list a Express 10 Point to 12 The temperature of every minute of the hour , How to draw a broken line statistical chart to observe the temperature change every minute 
# a = [random.randint(20,35) for i in range(120)]
# random.randint( Parameter one , Parameter two ), Parameters 1、 Parameters 2 It has to be an integer 
#  This function returns an argument 1 And parameters 2 Any integer between ,  Closed interval 
#  Pilot Kit ,
import random
from matplotlib import font_manager
from matplotlib import pyplot as plt

#  Pass in x Axis y Axis data 
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
#  Make some settings for the drawn image , Parameters figure(a,b) Is the size of the image ,dpi(a) Equivalent to the pixels of the image 
plt.figure(figsize=(20, 8), dpi=88)

#  Draw , Pass in x,y data , call plot() function 
plt.plot(x, y)

#  adjustment x Axis scale 
#  After converting to a list, perform the step size operation 
_x = list(x)[::3]
#  Here are the settings x Format of axis data 
_xtick_lables = ["10 spot {} branch ".format(i) for i in range(60)]
_xtick_lables = ["11 spot {} branch ".format(i - 60) for i in range(60, 120)]
#  The next step is to x Axis y The axes correspond one by one 
#  In addition, we set up a prompt message to x Axis y Shaft description 

plt.xticks(_x[::3], _xtick_lables[::3], rotation=45, fontproperties=my_font)
my_font = font_manager.FontProperties(fname="msyh.ttc")
#  because matplotlib Chinese fonts are not displayed by default 
#  to x Axis y Add description information to axis , We used xlable,ylabel
plt.xlabel(" Time ", fontproperties=my_font)
plt.ylabel(" temperature   Company : Centigrade ", fontproperties=my_font)
#  The following is the title of the image 
plt.title("10 spot ——12 Line chart of temperature change with time ", fontproperties=my_font, size="larger")
# matplotlib Chinese will not be displayed by default , So we need to set it ourselves , Import fonts from your computer before : Setting the font format and syntax is fixed , In addition, there are other ways to display Chinese, which is relatively simple 
# my_font = font_manager.FontProperties(fname="msyh.ttc")
#  Show the image 
plt.show()

 

explain plt.xticks(_x[::3], _xtick_lables[::3], rotation=45, fontproperties=my_font) In the parameter rotation=45

Set up x The inclination of the shaft

When importing fonts, both relative and absolute paths can be used :

 

原网站

版权声明
本文为[Little apes are not bald]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011529350952.html