python绘画标准流程_python3绘图示例6-2(基于matplotlib,绘图流程介绍及设置等)...

#!/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()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值