Matplotlib基本方法一览总结
前言
提示:本文章适合用于快速复习:
提示:以下是本篇文章正文内容,下面案例可供参考
---------基本格式
创建画布
.figure
创建分配绘图区域
.add_axes([,])
分别装入x, y, 线的三个属性
.plot(,)
一个区域多个图使用方法
l1 = ax.plot(x1,y1,'rd--')
l2 = ax.plot(x2,y2,'rd--')
l3 = ax.plot(x3,y3,'rd--')
ax.legend(labels = ('Smartphone', 'tv','test'), loc = 'lower right')
set_title set_xlabel set_ylabel 分别设置头 x轴 y轴 注释
.grid(color=‘b’, ls = ‘-.’, lw = 0.25) 设置网格颜色 线型2属性 宽度’’’
‘’’
--------画布区域划分方法
1.
axes
2.
对画布分配也可以使用.subplot(nrows, ncols, index)
若想重叠显示 可以add_subplot(111) add_subplot(221,facecolor=‘y’)
创建画布时同时 分配子图 fig,a = plt.subplots(2,2) a[0][0].plot(x,x*x)
3.
plt.subplot2grid(shape, location, rowspan, colspan)
shape:把该参数值规定的网格区域作为绘图区域;
location:在给定的位置绘制图形,初始位置 (0,0) 表示第1行第1列;
rowsapan/colspan:这两个参数用来设置让子区跨越几行几列。
---------坐标轴设置
1.刻度
#.set_yscale("")设置坐标y轴刻度类型 .set_xlabel(“x axis”)设置x轴刻度类型
2.颜色
# ax.spines['bottom'].set_color('blue') 设置坐标轴颜色
# ax.spines['left'].set_color('red')
# ax.spines['left'].set_linewidth(2)
3.取值范围
1.默认自动取值
2.#设置y轴 a1.set_ylim(0,10000) #设置x轴 a1.set_xlim(0,10) .set_zlim(,)
-------- 刻度样式
.set_xticks([0,2,4])
.set_xtickslabels(['zero','two','four',six])
.set_yticks([-1,0,1])
---------使用中文
法一 一弊端是每编写一个绘图程序就要添加一次相同的代码。
import matplotlib.pyplot as plt
plt.rcParams[“font.sans-serif”]=[“SimHei”] #设置字体
plt.rcParams[“axes.unicode_minus”]=False #该语句解决图像中的“-”负号的乱码问题
法二
1.查看 matplotlibrc 所在的目录 import matplotlib matplotlib.matplotlib_fname()
2.#font.serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century…
去除#
3.复制 C:\Windows\Fonts\Microsoft YaHei UI
粘贴到 D:\python\python37\lib\site-packages\matplotlib\mpl-data\fonts\ttf
粘贴后会出现一个 MSYH.ttc 的字体文件