python3绘图示例6-1(基于matplotlib,绘图流程介绍及设置等)

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os

import pylab as py
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mplt

# 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

# 画图流程:创建Figure对象->1个或多个Axes或Subplot对象->调用Axies创建各类Artists来画图

# # 图1-正弦图 余弦图
# x=np.linspace(-np.pi,np.pi,256,endpoint=True)
# c,s=np.cos(x),np.sin(x)
#
#
# # matplotlib中可 设置图片的大小 分辨率 线宽 颜色 风格 坐标轴 网格属性 文字属性 字体属性等
#
# py.plot(x,c)
# py.plot(x,s)
#
# py.show()

# 这里使用的是matplotlib.pylab 去画图
# 图像 指整个窗口内容 子图值图像中的各个图
# 图2
# 步骤1-创建一个 8*6 的点图像 分辨率为 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
py.figure(8*6,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()

# py.subplot(1,1,1)
py.subplot(111)


# 步骤3-自定义x y轴
# 坐标轴对象 axes 可放置在图像的任意位置
# 记号位置设置 Tick Locators 记号格式化操作 Tick Formatters
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
c,s=np.cos(x),np.sin(x)

# 步骤4-1-绘制曲线 颜色 线宽 线的风格(颜色+线型) 大图对应的小图标签 可用$$包裹,如$sin(x)$
py.plot(x,c,color='blue',linewidth=2.5,linestyle='-',label='cosine')
py.plot(x,s,color='red',linewidth=2.5,linestyle='-',label='sine')

# 步骤4-1-给曲线图上的对应数据点加注释
# t=2*np.pi/3
# py.plot([t,t],[0,np.cos(t)],color='blue',linewidth=2.5,linestyle='--')
# py.scatter([t,],[np.cos(t),],50,color='blue')
# py.annotate(r'$sin(frac{2\pi}{3}=frac{sqrt{3}{2}$',
# xy=(t,np.sin(t)),xycoords='data',
# xytext=(+10,+30),textcoords='offset points',fontsize=16,
# arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))
#
#
# py.plot([t,t],[0,np.sin(t)],color='red',linewidth=2.5,linestyle='--')
# py.scatter([t,],[np.sin(t),],50,color='red')
# py.annotate(r'$sin(frac{2\pi}{3}=frac{sqrt{1}{2}$',
# xy=(t,np.cos(t)),xycoords='data',
# xytext=(-90,-50),textcoords='offset points',fontsize=16,
# arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))

# 注解设置-注解和数据使用相同坐标 被注解数据的位置 终点坐标 xycoords='data' 注解文字位置,起点坐标 xytext=(5,38) 箭头属性和风格
# py.annotate('import value',(55,22),xycoords='data',xytext=(5,38),arrowprops=dict(arrowstyle='->'))
# 线的风格 实线 - 破折线 -- 点线 -. 虚线 : 不显示 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)$
# py.plot(x,c,color='blue',linewidth=2.5,linestyle='-',label='cosine')
# py.plot(x,s,color='red',linewidth=2.5,linestyle='-',label='sine')

# 属性设置使用set_属性 pyplot.setp()函数 属性获取使用 get_属性 pyplot.getp()

# 步骤4-2-设置显示大图对应的小图标签位置
# 小图标签设置-起始位置 宽度 高度 图例位置(左:3-6-2,中:8-10-9,右:4-7-1) 列数 图例扩展至整个坐标轴 坐标轴和图例距离
# py.legend(bbox_to_anchor=(0.,1.02,1.,.102),loc=3,ncol=3,mode='expand',borderaxespad=0.)
# py.legend(loc=3)
py.legend(loc='upper left')

# 坐标取值范围
# plt.axis([xmin,xmax,ymin,ymax])

# 步骤5-1-设置横轴的上下限
py.xlim(-4.0,4.0)

# 步骤5-2-横轴标记号
# py.xticks(np.linspace(-4,4,9,endpoint=True))
# py.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi])

# 步骤5-3-设置横轴标号标签
py.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$'])

# 步骤5-4-设置纵轴的上下限
py.ylim(-1.0,1.0)

# 步骤5-5-设置纵轴标记号
# py.yticks(np.linspace(-1,1,5,endpoint=True))
# py.yticks([-1,0,+1])

# 步骤5-6-设置纵轴标号标签
py.yticks([-1,0,+1],[r'$-1$',r'$0$',r'$+1$'])


# 步骤6-1设置图上文字浮在最上层,方便查看
# 获取坐标轴对象
ax=py.gca()
for label in ax.get_xticklabels()+ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white',edgecolor='None',alpha=0.65,zorder=2))

# 步骤6-2移动脊柱(可省略-移动x y轴 这个看需求,默认的话为常见的x轴和y轴图)
# 设置对应的边框是否显示 及边框颜色 边框位置: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))


# 图像标题
py.title('双弦图')

# 步骤7-设置在屏幕上显示图像
py.show()


# 关闭窗口用 默认无参-关闭当前窗口 all-关闭所有窗口 窗口编号或实例-则关闭指定窗口
# py.close(1)

猜你喜欢

转载自www.cnblogs.com/NiceTime/p/10129127.html