Python-fig,ax方式绘图-自学-不喜勿喷

Figure vs Axes
对象层次结构(object hierarchy.),是Matplotlib概念中一个重要的地方。

下图显示了这种对象层次结构,Figure相当于一张白纸——可称做画布,Axes则是画布中的一块区域。Axes下面又包括了一下绘图较小的对象,如刻度线、图例和文本等。
在这里插入图片描述
如下图所示一个Figure中包括两个Axes,每一个Axes又包括不同的曲线。
在这里插入图片描述
Matplotlib将其表现为图形解剖,而不是显式的层次结构:

image-20200830213820286
plt.figure() vs plt.axes() vs plt.subplots()
plt.figure()返回Figure实例

plt.axes()返回Axes或其子类

plt.subplots()返回Figure实例和Axes(或一组axes)

在创建一个空白的画布时,建议使用:

fig,ax = plt.subplots()
1
之后就可以使用ax下面的方法对图像进行调整

plt.xxx vs ax.xxx
plt.plot() → \rightarrow→ ax.plot()

plt.legend() → \rightarrow→ ax.legend()

plt.xlabel() → \rightarrow→ ax.set_xlabel()

plt.ylabel() → \rightarrow→ ax.set_ylabel()

plt.xlim() → \rightarrow→ ax.set_xlim()

plt.ylim() → \rightarrow→ ax.set_ylim()

plt.title() → \rightarrow→ ax.set_title()

在绘图时,使用ax.set()函数一次性设置完比较好,如:

x = np.linspace(0, 10, 1000)
ax = plt.axes()
ax.plot(x, np.sin(x))
ax.set(xlim=(0, 10), ylim=(-2, 2),
       xlabel='x', ylabel='sin(x)',
       title='A Simple Plot');

在这里插入图片描述
参考链接


import matplotlib.pyplot as plt
import numpy as np

A = np.arange(1,5)
B = A**2
C = A**3


fig,ax=plt.subplots(figsize=(14,7),dpi=72) #设置16:9画布的2x2画面,这里注意是subplots

ax.plot(A,B,label='Xlabel',alpha=100000) #x轴为A,y轴为B,设置标签为Xlabel,透明度为100000
ax.plot(B,A,label='Ylabel',alpha=100000) #x轴为B,y轴为A,设置标签为Ylabel,透明度为100000
    

ax.set_title('Test',fontsize=18) #设置画面标题为Test,大小为18 
ax.set_xlabel('Xlabel',fontsize=12,fontfamily='sans-serif',fontstyle='italic') 
#设置X轴标题为Xlabel,大小为12,字体为san-serif,字体风格为italic
ax.set_ylabel('Ylabel',fontsize='x-large',fontfamily='oblique')
#设置Y轴标题为Ylabel,大小为x-large,字体风格为oblique
ax.legend() #设置画面的legend

ax.set_aspect('equal') #x和y的縮放比例相同
ax.minorticks_on() #次坐标
ax.set_xlim(0,16) #x轴刻度范围设置
ax.grid(which='minor',axis='both') #网格线设置

ax.xaxis.set_tick_params(rotation=90,labelsize=12,colors='r') #设置刻度线参数
'''

ax.tick_params(direction='out', length=6, width=2, colors='r',
               grid_color='r', grid_alpha=0.5)
這將使所有主要刻度線變為紅色,即開即用,並且尺寸為6點乘2點。刻度線標籤也將為紅色。
網格線將是紅色且半透明的。
'''
start,end=ax.get_xlim() #获取x轴的起始点
ax.xaxis.set_ticks(np.arange(start,end,1)) #设置x轴
'''
Axis.set_ticks(self,ticks,*,minor = False)
tickslist of floats
List of tick locations.

minorbool, default: False
If False, set the major ticks; if True, the minor ticks.
'''
ax.yaxis.tick_right() #将Y坐标移至右边
plot.show()

在这里插入图片描述

  • 17
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值