我在一个图中构建了5个子图,这样较大的子图fig_1占据了图的[rows_0,cols_0:3],较小的子图fig_2—fig_5占据了图的[rows_1,cols_0—cols_3].
How could I apply an identical x-axis format to all of the listed figs in the Figure at once?或者也许我可以在子地段上重复一遍,一次一个地应用格式,但是我找不到这样的方法.
# creating sublots
fg = plt.figure(figsize=(9, 4), constrained_layout=True)
gs = fg.add_gridspec(ncols=4, nrows=2, width_ratios=widths, height_ratios=heights)
fig_ax_1 = fg.add_subplot(gs[0, :])
# plotting
# here I must add axis format (see below) and repeat this after each fig
fig_ax_2 = fg.add_subplot(gs[1, 0])
# plotting
# axis format (see below)
fig_ax_2 = fg.add_subplot(gs[1, 1])
# plotting
# axis format (see below)
fig_ax_2 = fg.add_subplot(gs[1, 2])
# plotting
# axis format (see below)
fig_ax_2 = fg.add_subplot(gs[1, 3])
# plotting
# axis format (see below)
#custom axis format I need to apply to all figs
fig_ax_1.xaxis.set_major_locator(mdates.DayLocator()) # fig_ax_2 for smaller figs
fig_ax_1.xaxis.set_minor_locator(mdates.HourLocator(byhour=[6,12,18]))
fig_ax_1.xaxis.set_major_formatter(mdates.DateFormatter("%d.%m"))
fig_ax_1.xaxis.set_minor_formatter(mdates.DateFormatter("%H"))
fig_ax_1.grid(which = "both",linewidth = 0.2)
fig_ax_1.tick_params(axis='x', which='minor', labelsize=8)
我试过下面的方法,但它给了我AttributeError: 'Figure' object has no attribute 'xaxis'
for fig in plt.subplots(): # also tried plt.subplots()[1:], this just creates empty Figure 2, leaving my Figure intact
fig.xaxis.set_major_locator(mdates.DayLocator())
fig.xaxis.set_minor_locator(mdates.HourLocator(byhour=[6, 12, 18]))
fig.xaxis.set_major_formatter(mdates.DateFormatter("%d.%m"))
fig.xaxis.set_minor_formatter(mdates.DateFormatter("%H"))
fig.grid(which="both", linewidth=0.2)
fig.tick_params(axis='x', which='minor', labelsize=8)