matplotlib 常用函数总结和读书笔记

  • 常用函数

import matplotlib.pyplot as plt
import numpy as np

plt.gcf() #获取当前Figure
plt.gca()#获取当前Axes

# 添加子图
fig = plt.figure()
ax = fig.add_subplot(numrows, numcols, fignum) # 如 plt.subplot(221) # 第一行的左图
ax = fig.add_axes([0.15, 0.1, 0.7, 0.3]) # plt.axis([0, 5, -1, 13]) [xmin,xmax, ymin, ymax]


#设置坐标轴Axes
t = ax.set_title('random numbers') #Text对象
ax.set_xlabel("time") # plt.xlabel()
ax.set_xlim([xmin, xmax]))     # plt.xlim()
ax.axis( [xmin,xmax, ymin, ymax]) #设置坐标轴范围 #plt.axis([0,1200*oneMB, 0, 1])
ax.set_xticks()      #plt.xticks(range(len(x)), ['a', 'b', 'c', 'd', 'e', 'f']);

# 绘制曲线
ax.lines //所有曲线
l, = plt.plot(x, y) #返回Line2D对象列表
lines = plt.plot(x1,y1,x2,y2)
ax.plot(y, color='blue', linestyle='dashdot', linewidth=4,marker='o', markerfacecolor='red',markeredgecolor='black',markeredgewidth=3, markersize=12,label='Normal');
line1 = plt.Line2D([0,1],[0,1], transform=fig.transFigure, figure=fig, color="r")

#调整坐标轴的标签
for label in plt.gca().xaxis.get_ticklabels():
    label.set_rotation(30)
    label. set_ha ("right") #horizontal alignment 水平对齐 legal = ('center', 'right', 'left')
fig.autofmt_xdate(bottom=0.18)    
# 图示
ax.plot(x, x*1.5, label='Normal')
ax.legend()
ax.legend(['Normal', 'Fast', 'Slow'])

#plt.yticks(range(1, 8, 2));
ax.grid(True)   #plt.grid(True) #绘制网格
plt.savefig('plot123.png',dpi=75)

#Axis
axis = plt.gca().xaxis
axis.get_ticklabels()
axis.get_ticklines()
#Text
label.set_color("red")
label.set_rotation(45)
label.set_fontsize(16)
label. set_ha ("right") #horizontal alignment 水平对齐 legal = ('center', 'right', 'left')



=================================
  • 面向对象

FigureCanvas -> Figure ->Axes->Axis(XAxis,YAxis)
fig.patch(Rectangle)
fig.axes

#Axis
ax.xaxis(XAxis).label(Text)
ax.yaxis
ax.lines

ax2 = ax1.twinx() twiny(),
ax2.semilogx(x, y2)
ax2.loglog(x,y2)

#获取和设置对象属性
#调用setp() 可以同时配置多个对象的属性,
plt.setp(lines, color="r", linewidth=2.0)
plt.getp(lines[0], "color")
============================
  • 坐标系转换

我们可以通过Axes 对象的transData 属性获得数据坐标转换对象,transAxes 属性获得子图坐标转
换对象,通过Figure 对象的transFigure 属性获得图表坐标转换对象。
通过坐标转换对象的transform() 方法,可以将此坐标系下的点转换为窗口坐标系中的坐标点,下面
我们看一个例子,首先在IPython 中运行“matplotlib_annotation.

数据坐标系 子图 (xmin,ymin)-(xmax,ymax)
Axes坐标系 子图(0,0)-(1,1)
Figure坐标系 (0,0)-(1,1)
窗口坐标系  像素 (0,0)-(width,height)
三种坐标转换对象,均有inverted()两种方法
ax.transData
ax.transAxes
fig.transFigure
* 通过transform()转换到窗口坐标系
* 通过inverted()获取逆变换对象,再通过transform从窗口坐标系转回

eg:
ax.transData.transform([(-3,-2),(3,5)]) #从子图数据坐标系-> 窗口坐标系(像素)
inv = ax.transData.inverted() #逆变换对象
inv.transform((320, 160)) #从窗口坐标系(像素)->子图数据坐标系

转载于:https://my.oschina.net/cppblog/blog/10410

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值