python - Matplotlib,所有子图的一个颜色条,覆盖在纸张大小上

标签 python matplotlib overlay subplot colorbar

<分区>

我遇到了以下问题,我的颜色条覆盖了我以 pdf 格式“打印”的纸张大小的子图。

代码示例:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

if __name__ == '__main__':

    titles = np.random.uniform(low=1, high=11, size=(1,10))
    temp = np.random.uniform(low=23, high=200, size=(10,10))
    ctf0102 = np.random.uniform(low=23, high=200, size=(10,10))
    ctf03 = np.random.uniform(low=23, high=200, size=(10,10))

    fig = plt.figure(figsize=(11.69,8.27), dpi=100)

    for num in range(len(titles)):

        ax = fig.add_subplot(3,4,num)

        im = ax.scatter(ctf03[1:,num], ctf0102[:,num], 12, temp[:,num], cmap='magma')

        ax.set_title(titles[num])


    fig.text(0.5, -0.00, '...', ha='center', fontsize=16)

    fig.text(-0.00, 0.5, '...', va='center', rotation='vertical', fontsize=16)

    cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
    fig.colorbar(im, cax=cbar_ax)

    plt.tight_layout(pad=2)

    fig.savefig("example.pdf", dpi=fig.dpi, bbox_inches='tight', pad_inches=0.3)

enter image description here

知道如何通过调整所有子图的空间大小将颜色条向右移动吗?除此之外,我想扩大颜色条,使其与所有子图的顶部和底部平滑闭合。

感谢您的帮助和想法。

最佳答案

使用fig.subplots_adjust设置子图的底部、顶部、左侧和右侧位置。然后使用相同的 bottomtop 来定义颜色条垂直放置的位置。并确保颜色条的左边缘位于子图的右侧。

另外,这里不能使用tight_layout,否则会改变位置。

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

fig = plt.figure(figsize=(11.69,8.27), dpi=100)

bottom, top = 0.1, 0.9
left, right = 0.1, 0.8

fig.subplots_adjust(top=top, bottom=bottom, left=left, right=right, hspace=0.15, wspace=0.25)

for num in range(12):

    ax = fig.add_subplot(3,4,num+1)
    im = ax.scatter(np.random.rand(10), np.random.rand(10), 12, np.random.rand(10), cmap='magma')

fig.text(0.5, 0.02, 'x label', ha='center', fontsize=16)

fig.text(0.02, 0.5, 'ylabel ', va='center', rotation='vertical', fontsize=16)

cbar_ax = fig.add_axes([0.85, bottom, 0.05, top-bottom])
fig.colorbar(im, cax=cbar_ax)

fig.savefig("example.pdf", dpi=fig.dpi, pad_inches=0.3)

enter image description here

关于python - Matplotlib,所有子图的一个颜色条,覆盖在纸张大小上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51303380/

相关文章:

python - Savefig as eps 产生不可使用的 eps

python - 为什么保存按钮在 matplotlib 图上不起作用?

android - 在 android 中按下 mapView 之前,我的叠加层不会显示

html - 叠加背景颜色和图像

Swift:仅删除多个 MKPolyline

python - 循环通过 2 个列表

python - 如何在 Django 中选择不同的值?

python - 如何将字典列表与内部列表连接起来

python - 我该如何修复这个错误?坏键错误 : Name must be string type

python - 当标记设置为像素时如何更改图例中的标记大小/比例