matplotlib 如何在保持形状的情况下放大y轴

rks48beu  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(122)

我在这里做了这个脚本和一些事情,在metpy的skewT函数中改变纵横比,改变x,y值。由于一些奇怪的原因或子图,更改figsize并没有解决这个问题。

:我想放大表面并使其与这些图

的形状相同
出于某种原因,它喜欢成为一个薄片。我甚至进入了metpy源代码,并改变了一些x,y轴的限制,但最终给出了与更新它们完全相同的结果。

import matplotlib.patheffects as pe
outline = pe.withStroke(linewidth=1.3, foreground='black')
season = ['DJF', 'MAM', 'JJA', 'SON']
custom_lines = [Line2D([0], [0], color='#080D1A', lw=2),  # color for Sounding
                Line2D([0], [0], color='#F37B28', lw=2),  # color for E3SM
                Line2D([0], [0], color='#0A864E', lw=2),  # color for ERA5 Land
                Line2D([0], [0], color='#5CC0E4', lw=2)]  # color for ERA5 Ocean

fig = plt.figure(figsize=(18, 24))
season = ['DJF', 'MAM', 'JJA', 'SON']

for row, sea in enumerate(season):
    for col in range(4):
        cmean = lambda x: cluster_mean(x, col)
        subplot_args = (4, 4, row * 4 + col + 1)
        skewt = SkewT(fig=fig, subplot=subplot_args)

        if col != 0:
            skewt.ax.set_yticklabels([])

        # Set y-axis title and increase y-axis tick label size on the first column only
        else:
            skewt.ax.set_ylabel('Pressure (hPa)', fontsize=14)
            skewt.ax.tick_params(axis='y', labelsize=12)

        in_ds_Sounding = Sounding_new_ds.groupby("time.season").apply(cmean).sel(season=sea)
        in_ds_E3SM = E3SM_new_ds.groupby("time.season").apply(cmean).sel(season=sea)
        in_ds_ERA5_ocean = ERA5_ocean_new_ds.groupby("time.season").apply(cmean).sel(season=sea)
        in_ds_ERA5_land = ERA5_land_new_ds.groupby("time.season").apply(cmean).sel(season=sea)

        skewt.plot((in_ds_Sounding["bar_pres"].values) * units.kPa,
                   (in_ds_Sounding["T"].values) * units.degC, color='#080D1A', linewidth=1.75,
                   label='Sounding T', zorder=8)

        skewt.plot((in_ds_Sounding["bar_pres"].values) * units.kPa,
                   in_ds_Sounding["dp"].values * units.degC, color='#080D1A', linestyle='dotted',
                   linewidth=2.5, label='Sounding dp', zorder=1,)

        skewt.plot((in_ds_E3SM["lev"].values) * units.hPa,
                   in_ds_E3SM["T"].values * units.degC, linewidth=2,
                   color='#F37B28', label='E3SM T', zorder=9)

        skewt.plot((in_ds_E3SM["lev"].values) * units.hPa,
                   in_ds_E3SM["dp"].values * units.degC, color='#F37B28', linestyle='dotted',
                   linewidth=2.5, label='E3SM dp', zorder=4,)

        skewt.plot((in_ds_ERA5_ocean["plev"].values/100) * units.hPa,
                   in_ds_ERA5_ocean["T"].values * units.degC, color='#5CC0E4', linewidth=2,
                   label='ERA5 T', zorder=6)

        skewt.plot((in_ds_ERA5_ocean["plev"].values/100) * units.hPa,
                   in_ds_ERA5_ocean["dp"].values * units.degC, color='#5CC0E4', linestyle='dotted',
                   linewidth=2.5, label='ERA5 dp', zorder=6,)

        skewt.plot((in_ds_ERA5_land["plev"].values/100) * units.hPa,
                   in_ds_ERA5_land["T"].values * units.degC, color='#0A864E', linewidth=2,
                   label='ERA5 T', zorder=7)

        skewt.plot((in_ds_ERA5_land["plev"].values/100) * units.hPa,
                   in_ds_ERA5_land["dp"].values * units.degC, color='#0A864E', linestyle='dotted',
                   linewidth=2.5, label='ERA5 dp', zorder=5,)

        skewt.ax.set_xlim([-55, 15])
        skewt.ax.set_title("Cluster %d %s" % (col + 1, sea), fontsize=16)

        # Only show legend on the first plot
        if col == 0 and sea == 'DJF':
            skewt.ax.legend(custom_lines, ['Sounding', 'E3SM', 'ERA5 Land', 'ERA5 Ocean'],
                            frameon=True, framealpha=1)

        # If it's not the first column, remove y-axis label
        if col != 0:
            skewt.ax.set_ylabel('')
        else:
            skewt.ax.set_ylabel('Pressure (hPa)', fontsize=14)  # Set y-axis title on the first column with larger font size
            skewt.ax.tick_params(axis='y', labelsize=12)  # Increase y-axis tick label size

        # If it's not the last row, remove x-axis label and numbers
        if row != 3:
            skewt.ax.set_xlabel('')
            skewt.ax.set_xticklabels([])  # Hide x-axis labels
        else:
            skewt.ax.set_xlabel('Temperature (°C)', fontsize=14)  # Set x-axis title on the last row with larger font size
            skewt.ax.tick_params(axis='x', labelsize=12)

plt.tight_layout()
plt.show()

字符串
如上所述,我尝试改变图的大小和子图。还有纵横比。还有类似gs = gridspec.GridSpec(4,4,height_ratios=[40,40,40,40])的东西。

6rvt4ljy

6rvt4ljy1#

如果你真的想覆盖MetPy在图上的默认长宽比,你可以在底层Axes上调用set_aspect('auto'),以在创建Skewt示例后恢复matplotlib的默认行为:

skewt = SkewT(fig=fig, subplot=subplot_args)
skew.ax.set_aspect('auto')

字符串
作为一个警告,虽然,我不认为这是要产生的图像,看起来像你想要的。例如,如果我关闭固定宽高比并设置数据限制(y:(1000,700)和x:(-55,15)),我们的简单探测示例如下:
x1c 0d1x的数据
收件人:



这是Skew-T绘图转换的本质,当您在一个方向上拉伸数据维度时,事情会变得非常奇怪(有关更多讨论,请参见this issue)。现在,如果你愿意接受缩小x轴的范围,你可以用这个片段来保持更合理的事情:

skew.ax.set_adjustable('datalim')
skew.ax.set_xlim(-15, 0)
skew.ax.set_ylim(1000, 700)


其给出:


相关问题