使用Matplotlib缩放绘图大小

时间:2018-01-05 13:19:49

标签: python matplotlib

我已经使用了显示色图代码并使其更通用。问题是现在所有的彩色地图都被一起弄湿了,所以图形基本上是不可读的。

如何增加每个色彩图显示的大小?

当前输出:

current result

import numpy as np
import matplotlib.pyplot as plt


# Have colormaps separated into categories:
# http://matplotlib.org/examples/color/colormaps_reference.html
cmaps =  [('All Color Maps',
"Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spectral, spectral_r, spring, spring_r, summer, summer_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r".replace(" ", "").split(',')
          )]
nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_category, cmap_list, nrows):
    fig, axes = plt.subplots(nrows=nrows)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
    axes[0].set_title(cmap_category + ' colormaps', fontsize=14)

    for ax, name in zip(axes, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        pos = list(ax.get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3]/2.
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axes:
        ax.set_axis_off()


for cmap_category, cmap_list in cmaps:

    plot_color_gradients(cmap_category, cmap_list, nrows)

plt.show()

2 个答案:

答案 0 :(得分:2)

如果你坚持让情节看起来尽可能接近你所展示的情节,即一列256个带有大标签的子图,那么唯一真正的解决办法是增加图中的尺寸,如答案中所述。 @Diziet Asahi。

话虽如此,我有2项改进建议。

选项1

将子图分成2列。这使得图像更容易阅读IMO。这只需要对您的绘图功能进行一些小修改:

def plot_color_gradients(cmap_category, cmap_list, nrows):
    fig, axes = plt.subplots(nrows=int(nrows/2), ncols=2, figsize=(12,11))
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.1, right=0.98, wspace=0.25)
    fig.suptitle(cmap_category + ' colormaps', fontsize=14)

    for ax, name in zip(axes.flatten(), cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        pos = list(ax.get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3]/2.
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
        ax.set_axis_off() # Don't need a separate loop for this

给出了:

enter image description here

选项2

如果你想将所有内容保存在一列中,可能会有一个解决方法,至少可以让情节看起来更好一点。那就是将每个其他标签放在轴的右侧。

注意:这可能不是您正在寻找的内容,但除非您将图形设置得非常大(高),否则图像总是看起来很狭窄

如此更改绘图功能会显示以下图表:

def plot_color_gradients(cmap_category, cmap_list, nrows):
    fig, axes = plt.subplots(nrows=nrows)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.9)
    axes[0].set_title(cmap_category + ' colormaps', fontsize=14)

    count = 0
    for ax, name in zip(axes, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        pos = list(ax.get_position().bounds)
        ax.set_axis_off()

        if count == 1:
            count = 0
            x_text = pos[0] + 0.71
            y_text = pos[1] + pos[3] / 2.
            fig.text(x_text, y_text, name, va='center', ha='left', fontsize=10)
        else:
            count = 1
            x_text = pos[0] - 0.01
            y_text = pos[1] + pos[3]/2.
            fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)

    # Theres no need to loop through list of axes twice. Do this in the above loop!

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    #for ax in axes:
    #    ax.set_axis_off()

enter image description here

不如第一个例子好,但仍有改进。

答案 1 :(得分:1)

正如@DavidG评论的那样,你需要增加你的身材的大小。在下面的代码中,将widthheight替换为适当的值。由于您似乎需要可变数量的行,height应该与nrows成比例

def plot_color_gradients(cmap_category, cmap_list, nrows):
    height = some_value * nrows
    fig, axes = plt.subplots(nrows=nrows, figsize=(width, height))
    ...