写完毕业论文啦,终于可以有时间把学到的东西整理一下,今天总结一下关于图形绘制及让图好看的操作。
python 图形绘制
- 一、图形绘制
- 1.1简单图形
- 1.2 改线粗、颜色、添加图例、点形状
- 1.3 颜色设置模块
- 1.4图例设置
- 1.4.1 图例背景\位置\大小
- 1.4.1.1 图例位置
- 1.4.1.2 图例大小
- 1.5 线类型
- 1.6 点形状(marker)
- 1.5 在图中添加文字(text)
- 1.6 改刻度范围、刻度字体大小及坐标轴字体
- 1.7 改图框粗细及图像大小像素
- 1.7.1 图框粗细
- 1.7.2 去除任意坐标轴
- 1.7.3 图像大小像素设置
- 二、完整的图形实例
一、图形绘制
1.1简单图形
先从绘制一幅简单的图开始,假设已知要画的点,那么:
1 2 3 4 5 6 7 8 9 | import matplotlib #引入画图数据库 import matplotlib.pyplot as plt import numpy as np # Data for plotting x=[1,2,3,4,5] y=[1,4,9,16,25] plt.plot(x,y) #画图,自变量x放前面 plt.show() |
1.2 改线粗、颜色、添加图例、点形状
是不是感觉图不好看?接下来改线粗、颜色、线类型、添加标签
1 2 | plt.plot(x,y,,linewidth =2.0, label = r"$\mathregular{\xi_a}$=0",color='b', linestyle='-',marker='*') #功能依次为线粗、标签(图例)、颜色、线类型、点形状 plt.legend() #标签显示(一般称为图例) |
这里所涉及的相关参数在后面都会给出选择
1.3 颜色设置模块
即将color='b’中的b改成颜色模块后的字母,如r,tab:pink,black等
1.4图例设置
图例的代码与latex的是相通的
参考链接:https://blog.csdn.net/Ying_Xu/article/details/51240291
需要展示希腊字母时:
1 | label=r"$希腊字母代码$" |
展示普通字母:
1 | label="$普通字母$" |
1.4.1 图例背景\位置\大小
官方关于plt.legend()各个参数的设置:https://matplotlib.org/api/legend_api.html?highlight=legend#module-matplotlib.legend
1 | plt.legend(frameon=False,loc="upper right",fontsize='small') #分别为图例无边框、图例放在右上角、图例大小 |
1.4.1.1 图例位置
图例位置一般系统会默认最好的位置(best),不过有些情况需要自己调。将一块平面分为九个方向,分别为:
右上“upper right”,右下“lower right”,正右“right”,左上“upper left”,左下“lower left”,中央偏左“center left”,中央偏上“upper center”,中央偏下“lower center”,正中央“center”
![]() |
---|
1.4.1.2 图例大小
图例大小有几种,如果不设置系统默认偏小,自调可以用:‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’
1.5 线类型
官方参考链接:https://matplotlib.org/gallery/lines_bars_and_markers/linestyles.html#sphx-glr-gallery-lines-bars-and-markers-linestyles-py
linestyle=‘此处放左边名字的下方小字’,如:linestyle=‘solid’,linestyle=(0, (1, 10));
1.6 点形状(marker)
官方参考:https://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.html#sphx-glr-gallery-lines-bars-and-markers-marker-reference-py
https://matplotlib.org/gallery/lines_bars_and_markers/marker_fillstyle_reference.html#sphx-glr-gallery-lines-bars-and-markers-marker-fillstyle-reference-py
marker=‘形状代码’,形状大小可以用代码:markersize=4
1.5 在图中添加文字(text)
1 2 3 4 5 6 7 8 9 10 11 | import matplotlib #引入画图数据库 import matplotlib.pyplot as plt import numpy as np # Data for plotting x=[1,2,3,4,5] y=[1,4,9,16,25] plt.plot(x,y,linewidth =2.0, label = r"$\mathregular{\xi_a}$=0",color='tab:pink', linestyle=(0, (1, 10)),marker='*') #画图,自变量x放前面 plt.legend(loc="upper left") #设置图例及图中文本显示 plt.text(2, 20,'(g)',fontsize=18)# 在图中添加文字 plt.show() |
1.6 改刻度范围、刻度字体大小及坐标轴字体
1 2 3 4 5 6 7 8 9 10 | font1 = {'family' : 'Arial', 'weight' : 'normal', 'size' : 18, } #Arial是字体形式 plt.xlabel('t /s',font1) #x轴坐标名称及字体样式 plt.ylabel('j',font1) #y轴坐标名称及字体样式 plt.xticks(fontsize=13) #x轴刻度字体大小 plt.yticks(fontsize=13) #y轴刻度字体大小 plt.xlim(0,18)#X轴范围 plt.ylim(0,120)#显示y轴范围 |
关于font:https://matplotlib.org/_modules/matplotlib/font_manager.html#FontEntry
https://www.w3.org/TR/CSS2/
1.7 改图框粗细及图像大小像素
1.7.1 图框粗细
这里相当于再画一个图框覆盖在原来的图上面,去掉任意一条边并不能在图上去掉,这里去掉一条边只是让那条边不变粗而已
1 2 3 4 5 6 7 | #设置图框线粗细 bwith = 1.5 #边框宽度设置为2 TK = plt.gca()#获取边框 TK.spines['bottom'].set_linewidth(bwith)#图框下边 TK.spines['left'].set_linewidth(bwith)#图框左边 TK.spines['top'].set_linewidth(bwith)#图框上边 TK.spines['right'].set_linewidth(bwith)#图框右边 |
1.7.2 去除任意坐标轴
官方参考:https://matplotlib.org/gallery/axisartist/simple_axisline3.html#sphx-glr-gallery-axisartist-simple-axisline3-py
1 2 3 4 5 6 7 8 9 10 11 12 | import matplotlib.pyplot as plt from mpl_toolkits.axisartist.axislines import Subplot fig = plt.figure(figsize=(3, 3)) ax = Subplot(fig, 111) fig.add_subplot(ax) ax.axis["right"].set_visible(False) ax.axis["top"].set_visible(False) plt.show() |
1.7.3 图像大小像素设置
官方指导:https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=dpi#matplotlib.figure.Figure.dpi
1 2 3 4 5 6 7 | plt.rcParams['figure.figsize']=(6.0,4.0) plt.rcParams['savefig.dpi'] = 200 #图片像素 plt.rcParams['figure.dpi'] = 200 #分辨率 # 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600&400 # 指定dpi=200,图片尺寸为 1200*800 # 指定dpi=300,图片尺寸为 1800*1200 # 设置figsize可以在不改变分辨率情况下改变比例 |
二、完整的图形实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import matplotlib #引入画图数据库 import matplotlib.pyplot as plt # Data for plotting x=[1,2,3,4,5] y=[1,4,9,16,25] plt.plot(x,y,linewidth =2.0, label = r"$\mathregular{\xi_a}$=0",color='tab:pink', linestyle='solid',marker='D') #画图,自变量x放前面 #以下为图形设置参数 plt.legend(frameon=False,loc="upper left",fontsize='large') #设置图例无边框,将图例放在左上角 plt.rcParams['figure.figsize']=(6.0,4.0) #图形大小 plt.rcParams['savefig.dpi'] = 200 #图片像素 plt.rcParams['figure.dpi'] = 200 #分辨率 # 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600&400 # 指定dpi=200,图片尺寸为 1200*800 # 指定dpi=300,图片尺寸为 1800*1200 # 设置figsize可以在不改变分辨率情况下改变比例 font1 = {'family' : 'Arial', 'weight' : 'normal', 'size' : 18, } plt.xlabel('x',font1) #x轴坐标名称及字体样式 plt.ylabel('y',font1) #y轴坐标名称及字体样式 #插入文本框 plt.text(-1, 30,'(a)',fontsize=18) #在图中添加文本 plt.xticks(fontsize=13) #x轴刻度字体大小 plt.yticks(fontsize=13) #y轴刻度字体大小 plt.xlim(0,6)#X轴范围 plt.ylim(0,30)#显示y轴范围 #设置图框线粗细 bwith = 1.5 #边框宽度设置为2 TK = plt.gca()#获取边框 TK.spines['bottom'].set_linewidth(bwith) TK.spines['left'].set_linewidth(bwith) TK.spines['top'].set_linewidth(bwith) TK.spines['right'].set_linewidth(bwith) #plt.grid() #显示网格线 plt.show() |