
matplotlib官方例程实验 一
发布日期:2021-05-06 22:01:44
浏览次数:29
分类:精选文章
本文共 4068 字,大约阅读时间需要 13 分钟。
# sphinx_gallery_thumbnail_number = 3import matplotlib.pyplot as pltimport numpy as np#实验一 空白图#最简单的figure创建方式,空白figureif 0: fig = plt.figure() #定义居中标题 fig.suptitle('No axes on this figure') plt.show()#实验二 绘制多条曲线#产生从0到2均匀分布的100个浮点nparrayif 0: x = np.linspace(0, 2, 100) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic') #设置x轴,y轴标签 plt.xlabel('x label') plt.ylabel('y label') #图表标题和曲线说明 plt.title("Simple Plot") plt.legend() plt.show()#实验三 多张图#重画一张figure,有2*2四张图表if 0: fig, ax = plt.subplots(2, 2) #产生4组满足正态分布的100长度的数组 data1, data2, data3, data4 = np.random.randn(4, 100) #第一行第一列绘制散点正态分布图,标记用x形 #第二行第一列绘制散点正态分布图,标记用o形 ax[0][0].plot(data1, data2, marker='x') ax[1][0].plot(data1, data2, marker='o') #第一行第二列绘制sin曲线图 x = np.arange(0, 10, 0.2) y = np.sin(x) ax[0][1].plot(x, y) #第二行第二列绘制红色圆点的折线 ax[1][1].plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') #横坐标0~6,纵坐标0~20 plt.axis([0, 6, 0, 20]) plt.show()#实验四 散点图if 0: data = { 'a': np.arange(50), #0~50共计50个整数 'c': np.random.randint(0, 50, 50), #50个0~50的随机数 'd': np.random.randn(50)} #50个正态分布的数值 data['b'] = data['a'] + 10 * np.random.randn(50) data['d'] = np.abs(data['d']) * 100 #数据源是字典data,目录a为横坐标,b为纵坐标,c为颜色,s为大小 plt.scatter('a', 'b', c='c', s='d', data=data) plt.xlabel('entry a') plt.ylabel('entry b') plt.show()#实验五 同一组xy数组绘制不同类型图if 0: names = ['group_a', 'group_b', 'group_c'] values = [1, 10, 100] plt.figure(1, figsize=(9, 3)) plt.subplot(131) plt.bar(names, values) plt.subplot(132) plt.scatter(names, values) plt.subplot(133) plt.plot(names, values) plt.suptitle('Categorical Plotting') plt.show()#实验六 查询图表可设置的属性if 0: lines = plt.plot([1, 2, 3]) print(plt.setp(lines)) plt.show() #plt.clf() #删除figure #plt.cla() #删除axes #plt.close() #删除图像包括内存#实验七 添加文本if 0: mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) #facecolor方块颜色,alpha透明度 n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') #在(60,0.25)坐标位置插入μ=100,σ=15 plt.text(60, .025, r'$\mu=100,\ \sigma=15$',fontsize=14, color='red') plt.axis([40, 160, 0, 0.03]) #显示格点 plt.grid(True) plt.show()#实验八 绘制标注if 0: ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) #xy表示要做标注的点,xytext表示标注的位置 plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()#实验九 对数坐标系if 0: from matplotlib.ticker import NullFormatter # useful for `logit` scale # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the interval ]0, 1[ y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure(1) # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthreshy=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Format the minor tick labels of the y-axis into empty strings with # `NullFormatter`, to avoid cumbering the axis with too many labels. plt.gca().yaxis.set_minor_formatter(NullFormatter()) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年05月02日 23时05分50秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
wgcloud运维监控系统错误:防篡改校验错误次数大于10次,不再上报数据
2021-05-14
为什么WGCLOUD安装完后,启动服务端打不开网页
2021-05-14
wgcloud网络监控出现负值
2021-05-14
ios 官方sample
2021-05-14
iOS 开发官方文档链接收集
2021-05-14
网易云面试(Android岗)之旅,差点被这些基础题绊了跟头。
2021-05-14
linux学习笔记(四)基本用户管理与帮助命令
2021-05-14
小程序:防止父方法被子方法冒泡,使用catchtap
2021-05-14
vue报错 created hook错误
2021-05-14
单选框点击文字也能选中
2021-05-14
此主机支持Intel VT-x,但Intel VT-x 处于禁用状态。
2021-05-14
06-局部变量和全局变量
2021-05-14
12-面向对象1
2021-05-14
python基础总结 异常处理
2021-05-14
解决Vue源码运行错误
2021-05-14
HDU - 4109 Instrction Arrangement
2021-05-14
Lua websocket长连接
2021-05-14
SQL 分页查询 返回总条数
2021-05-14
重写的特点
2021-05-14
计算机网络UDP协议和TCP协议
2021-05-14