基于MOOC嵩天《Python数据分析与展示》视频学习记录——第二周:Matplotlib

1.Matplotlib基本用法

1.1.第一个程序
import matplotlib.pyplot as plt

plt.plot([3, 1, 4, 2, 5])  # y轴变量,x轴默认显示
plt.ylabel('grade')  # y轴名字
plt.savefig('test', dpi=600)  # 保存为本地文件,默认为PNG
plt.show()  # 显示图像

在这里插入图片描述

import matplotlib.pyplot as plt

plt.plot([0, 2, 4, 6, 8], [3, 1, 4, 2, 5])  # x轴与y轴间距
plt.ylabel('grade')
plt.axis([-1, 10, 0, 6])  # x轴起始于-1,终止与10,y轴同理
plt.show()

在这里插入图片描述

2.pyplot的plot( )函数

在这里插入图片描述

2.1.format_string

在这里插入图片描述
在这里插入图片描述
注:无线条是“空”或者“空格”
在这里插入图片描述

2.2.实例
import matplotlib.pyplot as plt
import numpy as np

a = np.arange(10)
plt.plot(a, a*1.5, 'go-', a, a*2.5, 'rx', a, a*3.5, '*', a, a*4.5, 'b-.')
plt.show()

在这里插入图片描述

3.pyplot的中文显示

3.1.使用rcParams修改字体
import matplotlib. pyplot as plt
import matplotlib

matplotlib.rcParams['font.family'] = 'SimHei'  # 黑体
plt.plot([3, 1, 4, 5, 2])
plt.ylabel("纵轴(值)")
plt.savefig('test', dpi=600)
plt.show()

在这里插入图片描述

3.1.1.rcParams属性

在这里插入图片描述
在这里插入图片描述

3.2.增加fontproperties属性
import numpy as np
import matplotlib.pyplot as plt

a = np.arange(0.0, 5.0, 0.02)
plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=20)
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=20)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.show()

在这里插入图片描述

4.pyplot的文本显示

在这里插入图片描述

4.1实例
import numpy as np
import matplotlib.pyplot as plt

a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.xlabel('橫轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例$y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.text(2, 1, r'$\mu=100$', fontsize=15)  # 在(x, y)坐标处显示文本

plt.axis([-1, 6, -2, 2])
plt.grid(True)  # 网格显示
plt.show()

在这里插入图片描述

4.2.plt.annotate( )

plt.annotate(s, xy=arrow_ crd, xytext=text_ crd, arrowprops=dict)

  • s:要注释的内容
  • xy:箭头所在位置
  • xytext:文本所在的位置
  • arrowprops:定义箭头的相关属性
import numpy as np
import matplotlib.pyplot as plt

a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2 * np.pi * a), 'r--')
plt.xlabel('橫轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例$y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.annotate(r'$\mu=100$', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.1, width=2))
# 显示文本,在(2, 1)处显示文本,在(3, 1.5)处显示箭头
# facecolor:箭头颜色,shink:以此比例在箭头两侧缩进(不与坐标点重叠),width:箭头宽度

plt.axis([-1, 6, -2, 2])
plt.grid(True)
plt.show()

在这里插入图片描述

5.pyplot的子绘图区域

5.1.plt.subplot( )
plt.subplot(324)  # 将区域分成三行两列,选择第四个

在这里插入图片描述

5.2.plt.subplot2grid( )

理念:设定网格,选中网格,确定选中行列区域数量,编号从0开始。
plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1)

  • GridSpec:区域的初始划分
  • CurSpec:初始划分下选择的区域
  • colspan:区域的横向扩展
  • rowspan:区域的纵向扩展

在这里插入图片描述

5.3.GridSpec类

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值