如何在 Matplotlib 中的给定轴上绘制颜色条?

How to plot a colorbar in a given axis in Matplotlib?

我想在 matplotlib 中使用 GridSpec 在图中绘制子图。

在第一行(行数不同)中,我想放置一个带圆圈的网格。

在倒数第二行,我想放置一个图例。

在最后一行,我想放置一个颜色条。

但我的颜色条似乎比我给的多space。

你有解决的办法吗?

这是重新生成它的代码:

from matplotlib import  pyplot as plt
import matplotlib.cm as matplotlib_cm
import numpy as np
import matplotlib as mpl
import os

def plot_legend_in_given_axis(ax, fontsize):
    diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    row_labels = ['circle']
    ax.grid(which="major", color="white", linestyle='-', linewidth=3)
    ax.set_aspect(1.0)

    for row_index, row_label in enumerate(row_labels):
        for diameter_index, diameter_label in enumerate(diameter_labels):
            circle=plt.Circle((diameter_index + 0.5, row_index + 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
            ax.add_artist(circle)

    ax.set_xlim([0, len(diameter_labels)])
    ax.set_xticklabels([])

    ax.tick_params(axis='x', which='minor', length=0, labelsize=12)
    ax.set_xticks(np.arange(0, len(diameter_labels), 1))
    ax.xaxis.set_ticks_position('bottom')
    ax.set_xlabel('Very long\nx axis label', fontsize=fontsize, labelpad=5)

    ax.set_ylim([0, len(row_labels)])
    ax.set_yticklabels([])
    ax.tick_params(axis='y', which='minor', length=0, labelsize=12)
    ax.set_yticks(np.arange(0, len(row_labels), 1))

    ax.grid(which='major', color='black', linestyle='-', linewidth=1)


def main(width, height, xaxis_labels, yaxis_labels):
    width_multiply = 1.5
    height_multiply = 1.5

    figure_width = int(width_multiply * width)
    figure_height = int(height_multiply * height)

    fig = plt.figure(figsize=(figure_width, figure_height))

    grid = plt.GridSpec(width, height, hspace=0, wspace=0)  
    ax = fig.add_subplot(grid[0:-2, :])
    ax.set_aspect(1.0)
    legend_ax = fig.add_subplot(grid[-2, :])
    plot_legend_in_given_axis(legend_ax, 20)

    color_bar_ax = fig.add_subplot(grid[-1, :])
    cmap = matplotlib_cm.get_cmap('YlOrRd')
    v_min = 2
    v_max = 20
    norm = plt.Normalize(v_min, v_max)

    bounds = np.arange(v_min, v_max + 1, 2)
    cb = mpl.colorbar.ColorbarBase(color_bar_ax, cmap=cmap, norm=norm, ticks=bounds, spacing='proportional', orientation='horizontal')
    cb.ax.tick_params(labelsize=20)
    cb.set_label("-log10 (q-value)", horizontalalignment='center', rotation=0, fontsize=20)

    plt.xlim([0, width])
    ax.set_xticks(np.arange(0, width + 1, 1))

    plt.ylim([1, height])
    ax.set_yticks(np.arange(0, height + 1, 1))

    # Plot the circles with color in the grid
    for yaxis_index, yaxis_label in enumerate(yaxis_labels):
        for xaxis_index, xaxis_label in enumerate(xaxis_labels):
            circle = plt.Circle((xaxis_index + 0.5, yaxis_index + 0.5), 0.4, color=cmap(norm(10)), fill=True)
            ax.add_artist(circle)

    ax.grid()
    for edge, spine in ax.spines.items():
        spine.set_visible(True)
        spine.set_color('black')

    figures_path = os.path.join('/Users', 'burcakotlu', 'Desktop')
    figFile = os.path.join(figures_path, 'ColorBar_Using_GridSpec.png')
    fig.savefig(figFile, dpi=100, bbox_inches="tight")

    plt.cla()
    plt.close(fig)

width = 5
height = 5
xaxis_labels = ["x%d" %i for i in range(width)]
yaxis_labels = ["y%d" %i for i in range(height)]
main(width, height, xaxis_labels, yaxis_labels)

这个问题是因为您在定义颜色条的轴 color_bar_ax。 如果您改为使用 ax.set_xlim([0, width])ax.set_ylim([1, height]),那么您指定您现在指的是您的第一个子图 (ax),并且一切正常。

我进行这些更改后得到的输出如下所示:

可选修改:

最重要的是,您可以更改在 GridSpec 上分配图的方式,使图和标签之间没有重叠。 下面,我将网格上第一个子图的位置从 grid[0:-2, :]) 更改为 grid[0:-3, :],第二个子图从 grid[-2,:] 更改为 grid[-3:-1, :]。然后输出变为: