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 的字体文件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值