博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3绘图示例6-2(基于matplotlib,绘图流程介绍及设置等)
阅读量:2241 次
发布时间:2019-05-09

本文共 4960 字,大约阅读时间需要 16 分钟。

#!/usr/bin/env python # -*- coding:utf-8 -*- import os import numpy as np import matplotlib as mplt from matplotlib import pyplot as plt from matplotlib.ticker import * # 整个图像为1个figure对象,figure对象包含多个Axes对象,每个Axes对象都拥有自己坐标轴的绘图区域 # 调用figure时,则调用plot,然后plot调用gca,获取axes绘图区域 # 然后gca调用gcf,获取当前figure,如为空则自动生成 figure,相当于调用 subplots # title-图像标题 Axis-坐标轴 Axis Label-坐标轴标注 Tick-刻度线 Tick Label-刻度注释 # 关系如下 # fig---ax #          ---title #          ---data #          ---xaxis #                 ---tick #                         ---tick label #                 ---label #          ---yaxis #                 ---tick #                         ---tick label #                 ---label # matplotlib.get_config() 获取当前配置 # 用户matplotlib配置文件路径 path=mplt.get_configdir() print(path) # 当前matplotlib配置文件路径 path2=mplt.matplotlib_fname() print(path2) # 系统配置文件存放路径 path3=os.getcwd() print(path3) # 读取配置文件内容 p=mplt.rcParams print(p) # 中文乱码处理 正常显示中文标签 及正负号 plt.rcParams['font.sans-serif']=['Microsoft YaHei'] plt.rcParams['axes.unicode_minus']=False # 全局关闭 # plt.rcParams.update({'axes.formatter.useoffset':False}) # 画图流程:创建Figure对象->1个或多个Axes或Subplot对象->调用Axies创建各类Artists来画图 # 这里使用的是matplotlib.pyplot 去画图 # 图像 指整个窗口内容 子图值图像中的各个图 # 图1 # 步骤1-创建一个 2*2 的点图像 分辨率为 80 # 参数说明 # 图像数量      num=None,  # autoincrement if None, else integer from 1-N # 图像的长和宽  figsize=None,  # defaults to rc figure.figsize # 分辨率       dpi=None,  # defaults to rc figure.dpi # 区域背景色    facecolor=None,  # defaults to rc figure.facecolor # 区域边缘色    edgecolor=None,  # defaults to rc figure.edgecolor # 是否绘制图像边缘 # frameon=True, # FigureClass=Figure, # clear=False, # **kwargs # f,axs=plt.subplots(2,2,figsize=(15,15)) fig=plt.figure(20*20,dpi=80) # 步骤2-设置子图位置 几行几列的网格 第1个参数:1行 第2个参数:1列 第3个参数:图形在网格的位置 # 多个子图组成大图 # fig=plt.figure()->plt.subplot()->plot.plot()->plot.show() # 子图悬浮在大图上 # fig=plt.figure()->ax=fig.add_axes(位置列表)或ax=fig.axes()->ax.plot()->plt.show() # fig=plt.figure()->fig.add_subplot()->p=plt.Rectangle()多个->fig.add_subplot().add_patch(p)->fig.canvas.draw()->plot.show() # plt.subplot(1,1,1) plt.subplot(111) # 步骤3-设置线图属性-自定义x y轴 # 坐标轴对象 axes 可放置在图像的任意位置 # 记号位置设置 Tick Locators  记号格式化操作 Tick Formatters # 方式1 生成数据的方法 定义新刻度的范围和个数 x=np.linspace(-np.pi,np.pi,256,endpoint=True) c,s=np.cos(x),np.sin(x) # 绘制曲线 颜色 线宽 线的风格 大图对应的小图标签 # 线的风格 实线 - 破折线 -- 点线 -. 虚线 : 不显示 None '' ' ' # 线条标记 # 圆圈 o 小菱形 d 菱形 D # 正方形 s 五边形 p 六边形1 h 六边形2 H 八边形 8 # 水平线 _ 竖线 | 加号 + 点 . 像素 ,  星号 * x X 无 None '' ' ' # 1角朝上三角形 ^ 1角朝下三角形 v 1角朝左三角形 < 1角朝右三角形 > # 线的颜色 红 r 黄 y 白 w 绿 g 蓝 b 青 c 洋红 m 黑 k 支持16进制'#eeefff'或3元色组(0.3,0.3,0.3) # 颜色 线宽 线的风格(颜色+线型) 大图对应的小图标签 可用$$包裹,如$sin(x)$ plt.plot(x,c,color='blue',linewidth=2.5,linestyle='-',label='cosine') plt.plot(x,s,color='red',linewidth=2.5,linestyle='-',label='sine') # 属性设置使用set_属性 pyplot.setp()函数 属性获取使用  get_属性 pyplot.getp() # 方式2 # lines,=plt.plot(1,6,'-') # lines.set_antialiased(False) # 坐标取值范围 # plt.axis([xmin,xmax,ymin,ymax]) # 步骤4-1-设置x轴 y轴范围 # 获得当前x轴 y轴范围值 xmin,xmax=plt.xlim() ymin,ymax=plt.ylim() # 横轴的上下限 plt.xlim(xmin*1.5,xmax*1.5) # 纵轴的上下限 plt.ylim(ymin*1.5,ymax*1.5) # 步骤4-2-设置刻度 刻度位置 间隔 格式 ax=plt.gca() # 关闭简略的间隔标注 ax.get_xaxis().get_major_formatter().set_useOffset(False) # 设置横轴标记号 # py.xticks(np.linspace(-4,4,9,endpoint=True)) # py.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi]) # 设置横轴标号标签 plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$']) # 设置纵轴标记号 # plt.yticks(np.linspace(-1,1,5,endpoint=True)) # plt.yticks([-1,0,+1]) # 设置纵轴标号标签 plt.yticks([-1,0,+1],[r'$-1$',r'$0$',r'$+1$']) # 返回一个fig图像 和 一个 ax的 array列表 # fig,ax=plt.subplots(2,2) # ax=plt.gca() # 步骤4-3-设置主 次刻度 及注释 2.5的倍数 5的倍数 0.5的倍数 1的倍数 xmin=MultipleLocator(2.5) xmax=MultipleLocator(5) xformat=FormatStrFormatter('%5.1f') ymin=MultipleLocator(0.5) ymax=MultipleLocator(1) yformat=FormatStrFormatter('%1.1f') # x轴 主刻度 次刻度 ax.xaxis.set_major_locator(xmax) ax.xaxis.set_major_formatter(xformat) ax.xaxis.set_minor_locator(xmin) # y轴 主刻度 次刻度 ax.yaxis.set_major_locator(ymax) ax.yaxis.set_major_formatter(yformat) ax.yaxis.set_minor_locator(ymin) # 网格刻度类型which major minor both # 绘制哪个网格线xaxis yaxis x y both ax.xaxis.grid(True, which='major') ax.yaxis.grid(True,which='minor') # 步骤5-移动坐标 剩下下面和左边的坐标-看实际需要配置 # 设置对应的边框是否显示 及边框颜色 边框位置:left right bottom top none ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # x轴 设置刻度 top bottom both default none 位置  data axes outward ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) # y轴 ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0)) # 图像上的数据显示更清晰 for label in ax.get_xticklabels() + ax.get_yticklabels():     label.set_fontsize(16)     label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65)) # 步骤6-增加图例 plt.legend(loc='upper left') # 设置图像外侧与图像间隔距离 plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8, top=0.8,hspace=0.2,wspace=0.3) # 图像标题 plt.title('图1') # 显示图像 plt.show()

转载于:https://www.cnblogs.com/NiceTime/p/10129128.html

你可能感兴趣的文章
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>