使用Matplotlib缩放绘图大小

2024-05-19 15:52:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了display colormap代码并使其更通用。问题是现在所有的彩色地图都在一起,所以图形基本上是不可读的。在

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

电流输出:

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()

Tags: textinforpltaxlistcmapgist
2条回答

如果你坚持让图看起来尽可能接近你所展示的,即一列256个子图,有大标签,那么唯一真正的解决办法就是增加数字的大小,正如@Diziet Asahi在回答中提到的那样。在

尽管如此,我有两个改进建议。在

选项1

将子图分成两列。这使得图像在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

如果你想把所有的东西都放在一列里,那么至少可以让图看起来更好一些。也就是说,在坐标轴的右侧放置其他标签。在

注意:这可能不是您想要的,但除非您使图形非常大(高),否则图像总是显得局促

改变绘图功能如下所示:

^{pr2}$

enter image description here

虽然不如第一个例子好,但还是有了改进。在

正如@DavidG评论的那样,你需要增加你身材的尺寸。在下面的代码中,用适当的值替换width和{}。因为您似乎想要一个可变的行数,height应该与nrows成比例

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

相关问题 更多 >