【python绘图】Matplotlib绘图及设置(使⽤python制图)⽬录
# matplotlib提供快速绘图模块pyplot,它模仿了MATLAB的部分功能
import matplotlib.pyplot as plt        #导⼊绘图模块
from matplotlib import pyplot as plt          #两种导⼊⽅法都可
第⼀节内容的精简版总结:
1. 绘制折线图(plt.plot)
2. 设置图⽚⼤⼩和分辨率(plt.figure)
3. 保存图⽚到本地(plt.savefig)
4. 设置xy轴刻度和字符串(xticks、yticks)
5. 设置标题、xy轴标签(title、xlable、ylable)
6. 设置字体(font_manager.)
7. 同⼀张图绘制多线条(plt多次plot)
8. 添加图例、绘制⽹格
9. 其他图像类型(散点图plt.scatter,条形图plt.bar,横向plt.barh,直⽅图plt.hist(bin.width组距、num_bins分多少组、))⼀、初识matplotlib.pyplot
准备好制图数据,传⼊参数。即可使⽤plt.plot(参数)、plt.show()⼀键出图!
import matplotlib.pyplot as plt
x = [......]
y = [......]
plt.plot(x,y,label='图例')        #绘图,并且标注图例
matplotlib中subplotplt.show()        #显⽰
plot.legend(prop=my_font)        #设置显⽰图例,括号中意思为显⽰中⽂(后⾯讲解)
1.绘制图像
plt.plot() 参数设置:
color=’ ‘        线条颜⾊
linestyle=’‘        线条风格
linewidth=        线条粗细
alpha=0.5        透明度        (对照表见常见绘图属性设置附表)
⼀个实例:假设⼀天中每隔两个⼩时(range(2,26,2))的⽓温(℃)分别是[15,13,14.5,17,20,25,26,26,27,22,18,15]
import matplotlib.pyplot as plt
x = range(2,26,2)
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]
# 绘图
plt.plot(x,y)
# 显⽰
plt.show()
绘制出如下图⽚:
2.设置图⽚⼤⼩
在绘制图⽚之前,使⽤plt.figure函数设置图⽚⼤⼩,其中figsize为元组,分别代表长宽,dpi(Dot Per Inch)为分辨率表⽰的单位之⼀。
plt.figure(figsize=(20,8),dpi=150)        #图⽚⼤⼩为20*8,每英⼨150个像素点
3.保存图⽚⽂件
plt.savefig("./t1.png")        #将图⽚保存到本地
引号⾥为⽂件路径和⽂件名( ./ 代表当前路径,png为⽂件后缀/格式)
4.设置X,Y轴刻度范围
设置x,y轴的范围可以使⽤多种⽅法
_xticks_lables = [i/2 for i in range(4,49)]                # ⽣成更复杂的数列
⾃定义刻度内容
_x =list(x) [::3]
_xticks_labels = ["10点{ }分".format(i) for i in _x]
5.添加描述信息(标题、轴标签)
plt.title("折线图")    #设置标题
plt.xlabel("时间")    #设置x轴标注
plt.ylabel("⽓温")    #设置y轴标注
6.设置显⽰中⽂(导⼊字体模块)
from matplotlib import font_manager        #导⼊字体管理模块
my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF") #定义中⽂字体属性,⽂字储存路径可以在C:/WINDOWS/Fonts/到,这⾥设置为宋体
plt.xlabel("时间",fontproperties = my_font,fontsize = 18)
#在设置x坐标中⽂标注,令fontproperties = my_font,fontsize令字体为18号
#plt.title,plt.ylabel,icks,icks设置中⽂标注类似
7.绘制⽹格
绘制⼀个温度随时间变化的折线图实例
import matplotlib.pyplot as plt
import random    #导⼊随机⽣成模块
from matplotlib import font_manager#导⼊字体管理模块
my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF")
#定义中⽂字体属性,⽂字储存路径可以在C:/WINDOWS/Fonts/到,本次设置为宋体
x = range(0,120)    #x值为0-120
y = [random.randint(20,35) for i in range(120)]    #y值为120个在20-35之间的随机数
plt.figure(figsize=(15,10),dpi = 80)    #图⽚⼤⼩为15*10,每英⼨80个像素点
'''调整x轴刻度'''
_xticks_labels = ["10点{}分".format(i) for i in range(60)]
_xticks_labels += ["11点{}分".format(i) for i in range(60,120)]
#取步长5,数字和字符串⼀⼀对应,保证数据的长度⼀样
'''设置标注'''
plt.title("10点到12点每分钟温度变化图",fontproperties = my_font,fontsize = 24)    #设置标题
plt.xlabel("时间",fontproperties = my_font,fontsize = 18)    #设置x坐标标注,字体为18号
plt.ylabel("每分钟对应的温度",fontproperties = my_font,fontsize = 18)    #设置y坐标标注
plt.plot(x,y)  #绘图
plt.show()  #显⽰
⼆、常见绘图属性设置
1.绘图符号(Makers)
符号中⽂说明英⽂说明
'.'圆点point marker
','像素点pixel marker
'o'圆圈circle marker
'v'向下三⾓形triangle_down marker
'^'向上三⾓形triangle_up marker
'<'向左三⾓形triangle_left marker
'>'向右三⾓形triangle_right marker
'1'向下Y形tri_down marker
'2'向上Y形tri_up marker
'3'向左Y形tri_left marker
'4'向右Y形tri_right marker
's'⽅形square marker
'p'五边形pentagon marker
'*'星形star marker
'h'六⾓形1hexagon1 marker
'H'六⾓形2hexagon2 marker
'+'加号plus marker
'x'叉号x marker
'D'钻⽯形diamond marker
'd'钻⽯形(⼩)thin_diamond marker
'|'竖线vline marker
'_'横线hline marker
2.线型(Line Styles)
符号中⽂说明英⽂说明
'-'实线solid line style
'--'虚线dashed line style
'-.'点划线dash-dot line style
':'点线dotted line style 3.颜⾊缩写(Colors)
多种丰富的颜⾊对照代码参见:
符号中⽂说明英⽂说明
'b'蓝blue
'g'绿green
'r'红red
'c'青cyan
'm'紫magenta
'y'黄yellow
'k'⿊black
'w'⽩white
4.Windows字体中英⽂名称对照
中⽂名称英⽂名称
⿊体SimHei
微软雅⿊Microsoft YaHei
微软正⿊体Microsoft JhengHei