【问题标题】:How to plot a colorbar in a given axis in Matplotlib?如何在 Matplotlib 中的给定轴上绘制颜色条?
【发布时间】:2022-01-05 07:10:22
【问题描述】:

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

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

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

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

但似乎我的颜色条占用的空间比我给的要多。

你有什么解决办法吗?

这是重新生成它的代码:

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)

【问题讨论】:

    标签: matplotlib colorbar


    【解决方案1】:

    这个问题源于您在定义颜色条的坐标轴color_bar_ax 后立即使用plt.xlim([0, width]) plt.ylim([1, height]) 设置第一个子图(坐标轴ax)的坐标轴范围。 相反,如果您使用 ax.set_xlim([0, width])ax.set_ylim([1, height]),那么您指定您现在指的是您的第一个子图 (ax),并且一切正常。

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

    可选修改:

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

    【讨论】:

    • 感谢您指出命令的顺序。我在 matplotlib 子图中有一个普遍问题,其中一个子图具有不同的宽度和高度,而其他子图必须具有固定大小。在这种情况下,将其他子图保持在固定大小似乎是不可能的。
    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2013-07-19
    相关资源
    最近更新 更多