plt 绘制多个子图只需要一个颜色条

可以通过在其自己的轴上添加colorbar来实现。 这可以通过手动创建一个附加轴并使用subplots_adjust()add_axes()根据需要平移现有图来完成。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(10, 6), dpi=300)
for i in range(1,7):
    # This simply creates some random data to populate with
    a = np.arange(10)
    x, y = np.meshgrid(a, a)
    z = np.random.randint(0, 7, (10, 10))

    plt.subplot(2,3,i)
    im = plt.contourf(x, y, z)

# Tight layout is optional
fig.tight_layout()
fig.subplots_adjust(right=0.825)
# [x1, x2,x3, x4],x1设置距离左边的距离,x2设置距离上部的距离,x3胖瘦,x4长短
cax = fig.add_axes([0.85, 0.075, 0.025, 0.82]) 
cbar = fig.colorbar(im, cax=cax)
cbar.set_ticks([0, 20, 40, 60, 80,100]) # 设置柱状图的标签刻度
cbar.set_ticklabels(['0', '20', '40', '60', '80',"100"]) # 设置柱状图的标签刻度值
cbar.ax.tick_params(labelsize=13)  # 设置colorbar刻度字体大小
plt.show()

要删除图形间的轴标签,刻度线等,需要对上述方法进行一些不平凡的修改,其中plt.subplots()用于填充子图形对象的2x3数组,然后对其进行迭代。 

import matplotlib.pyplot as plt
import numpy as np

nrows = 2
ncols = 3
# Create the subplot array
fig, (axes) = plt.subplots(nrows=nrows, ncols=ncols, figsize=(10, 6),
                           dpi=300, sharex=True, sharey=True)
for i in range(nrows):
    for j in range(ncols):
        a = np.arange(10)
        x, y = np.meshgrid(a, a)
        z = np.random.randint(0, 7, (10, 10))
        im = axes[i][j].contourf(x, y, z)
        # Remove the tick marks but leave the superleft and superbottom alone
        if i != nrows-1:
            if j != 0:
                axes[i][j].tick_params(axis='both', which='both',
                           left=False, bottom=False, top=False)
            else:
                axes[i][j].tick_params(axis='both', which='both', bottom=False, top=False)
        else:
            if j != 0:
                axes[i][j].tick_params(axis='both', which='both', left=False, top=False)
fig.tight_layout()
# Some additional whitespace adjustment is needed
fig.subplots_adjust(right=0.825, hspace=0.025, wspace=0.025)
# [x1, x2,x3, x4],x1设置距离左边的距离,x2设置距离上部的距离,x3胖瘦,x4长短
cax = fig.add_axes([0.85, 0.075, 0.025, 0.82]) 
cbar = fig.colorbar(im, cax=cax)
cbar.set_ticks([0, 20, 40, 60, 80,100]) # 设置柱状图的标签刻度
cbar.set_ticklabels(['0', '20', '40', '60', '80',"100"]) # 设置柱状图的标签刻度值
cbar.ax.tick_params(labelsize=13)  # 设置colorbar刻度字体大小
plt.show()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清纯世纪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值