matplotlib添加行列标题、axes.axis以及axes.xaxis.set_ticklabels相关使用方法

利用matplotlib的axes.set_title添加子图标题

今天同学问我matplotlib进行可视化,如何添加列标题,以下是我写的相关脚本:

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [3,6,7,9,2]

fig,axes = plt.subplots(2, 2, figsize=(10,6))
for i in range(2):
    # axes[0,i]表示子图第1行第i列
    axes[0,i].plot(x, y)
    # 添加标题
    axes[0,i].set_title('xx_{}'.format(i))
    # axes[1, i]2行第i列
    axes[1, i].plot(x, y)
plt.show()

结果如下:
在这里插入图片描述
利用axes.axis(“off”)关闭轴刻度:

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [3,6,7,9,2]

fig,axes = plt.subplots(2, 2, figsize=(10,6))
for i in range(2):
    # 关闭轴刻度
    axes[0, i].axis("off")
    # axes[0,i]表示子图第1行第i列
    axes[0,i].plot(x, y)
    # 添加标题
    axes[0,i].set_title('xx_{}'.format(i))

    axes[1,i].axis("off")
    # axes[1, i]2行第i列
    axes[1, i].plot(x, y)
plt.show()

结果如下:
在这里插入图片描述设置列标题:

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [3,6,7,9,2]

fig,axes = plt.subplots(2, 2, figsize=(10,6))
for i in range(2):
    # 关闭轴刻度
    # axes[0, i].axis("off")
    # axes[0,i]表示子图第1行第i列
    axes[0,i].plot(x, y)
    # 添加标题
    # axes[0,i].set_title('xx_{}'.format(i))
    axes[0,0].set_ylabel("x\nx\nx", rotation=0)

    # axes[1,i].axis("off")
    # axes[1, i]2行第i列
    axes[1, i].plot(x, y)
    axes[1,0].set_ylabel("t\nt\nt", rotation=0)
plt.show()

结果如下:
在这里插入图片描述
使用axes.xaxis.set_ticklabels([])和axes.yaxis.set_ticklabels([])隐藏刻度值,但是保留坐标轴标签:

fig,axes = plt.subplots(2, 2, figsize=(10,6))
for i in range(2):
    # 关闭轴刻度
    # axes[0, i].axis("off")
    # axes[0,i]表示子图第1行第i列
    axes[0,i].plot(x, y)
    # 添加标题
    # axes[0,i].set_title('xx_{}'.format(i))
    axes[0,0].set_ylabel("x\nx\nx", rotation=0)
    # 隐藏刻度值,但是保留坐标轴标签
    axes[0, i].xaxis.set_ticklabels([])
    axes[0, i].yaxis.set_ticklabels([])

    # axes[1,i].axis("off")
    # axes[1, i]2行第i列
    axes[1, i].plot(x, y)
    axes[1, 0].set_ylabel("t\nt\nt", rotation=0)
    # 隐藏刻度值,但是保留坐标轴标签
    axes[1, i].xaxis.set_ticklabels([])
    axes[1, i].yaxis.set_ticklabels([])

plt.show()

结果如下:
在这里插入图片描述
以下是参考链接:https://www.cnpython.com/qa/39920 的方法,感觉这个方法更方便,这里贴出代码:

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [3,6,7,9,2]
cols = ['Column {}'.format(col) for col in range(1, 4)]
rows = ['Row {}'.format(row) for row in ['A', 'B', 'C', 'D']]

# 初始化
fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(12, 8))

axes[0, 0].plot(x, y)

plt.setp(axes.flat, xlabel='X-label', ylabel='Y-label')

pad = 5    # in points

for ax, col in zip(axes[0], cols):
    ax.annotate(col, xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                size='large', ha='center', va='baseline')

for ax, row in zip(axes[:, 0], rows):
    ax.annotate(row, xy=(0, 0.5), xytext=(-ax.yaxis.labelpad - pad, 0),
                xycoords=ax.yaxis.label, textcoords='offset points',
                size='large', ha='right', va='center')

fig.tight_layout()
fig.subplots_adjust(left=0.15, top=0.95)

plt.show()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值