matplotlib图片/子图标题置于底部 设置坐标轴位置 python

In my thesis, I need to show three activation functions’ figures in one row. There are 4 points that should be considered:

  1. show subplots
  2. move the axis to the center of the figure, like a normal coordinate system
  3. put the title at the bottom of the figure (default is at the top)
  4. only specific ticks showed on the axis

The one big figure also can be used in this way with similar functions.

Take a quick preview first on the final result:)
在这里插入图片描述

Let’s do it!

subplots

fig = plt.figure(figsize=(16, 4))
ax = fig.add_subplot(1, 3, i+1)

move the position of the axis to center

ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

set title at the bottom

The parameter y in ax.set_title means “Set the y position of the text” (same as x).

  • 1: top
  • 0: bottom of graphic part
  • <0: lower than graphic part (too low will be out of the fig.)
  • >1: higher than the top (too high will be out of the fig.)
ax.set_title('('+letter[i]+') '+functions[i].__name__, y=-0.15, fontsize=14)

show the specific ticks on the axis

Only show [-1, -0.5, 0.5, 1] of y-aixs.

ax.set_yticks([-1, -0.5, 0.5, 1])

complete code and figure

import numpy as np
import decimal
import matplotlib.pyplot as plt

from scipy import signal


def Sigmoid(x):	return 1.0/(1+np.exp(-1.0*x))
def Sigmoid2(x):return 1.0/(1+np.exp(-1.0*x))-0.5
def TanH(x):	return 2.0/(1+np.exp(-2.0*x))-1
def ReLU(x):	return np.maximum(0, x)

functions = [Sigmoid, TanH, ReLU]
x = np.arange(-6.5, 6.5, 0.01)
letter = list(map(chr, range(ord('a'), ord('z') + 1))) 

fig = plt.figure(figsize=(16, 4))
plt.subplots_adjust(wspace=0.06)#, hspace =0.1)
for i in range(0, len(functions)):
	ax = fig.add_subplot(1,3,i+1)
	ax.spines['top'].set_color('none')
	ax.spines['right'].set_color('none')
	ax.xaxis.set_ticks_position('bottom')
	ax.spines['bottom'].set_position(('data', 0))
	ax.yaxis.set_ticks_position('left')
	ax.spines['left'].set_position(('data', 0))

	# set the title and move it to the bottom
	ax.set_title('('+letter[i]+') '+functions[i].__name__, y=-0.15, fontsize=14) 

	if i != 2: ax.set_yticks([-1, -0.5, 0.5, 1]) # change axis ticks
	elif i == 2: ax.set_yticks(list(np.arange(1,7,1)))
	plt.plot(x, functions[i](x),  linestyle="-", linewidth=2)

plt.show()

在这里插入图片描述

Reference

matplotlib.axes.Axes.set_title

Mark a tutorial in Chinese

matplotlib图中的文本

Color and linestyle

python中matplotlib的颜色及线条控制

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值