在堆叠图中对齐 yaxis 上的标签

Align label on yaxis in stacked plots

我使用 jupyter 绘制了堆叠图像。情节很好,但 y 轴上的标签我无法与中心对齐。

下面给出了 MWE

import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import matplotlib.gridspec as gridspec
import pylab 
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt 
from matplotlib.lines import Line2D
import matplotlib.lines as mlines

def fit_data():

    fig = plt.figure(1,figsize=(15,15))
    ax1= fig.add_subplot(511,)
    ax1.scatter(data4['MJD'], data4['Favg'],  marker='o', color='red', s=15)
    ax1.errorbar(data4['MJD'], data4['Favg'], data4['Flux-err'], fmt='.', ecolor='black',color='red', elinewidth=1,capsize=3)
    ax1.set_title('NGC 4151 - Year - 1993', size = 18)

    ax2 = fig.add_subplot(512, sharex=ax1 )
    ax2.scatter(data5['MJD'], data5['Favg'],  marker='o', color='blue', s=15)
    ax2.errorbar(data5['MJD'], data5['Favg'], data5['Flux-err'], fmt='.', ecolor='black',color='blue', elinewidth=1,capsize=3)

    ax3 = fig.add_subplot(513, sharex=ax1 )
    ax3.scatter(data6['MJD'], data6['Favg'],  marker='o', color='green', s=15)
    ax3.errorbar(data6['MJD'], data6['Favg'], data6['Flux-err'], fmt='.', ecolor='black',color='green', elinewidth=1,capsize=3)

    ax4 = fig.add_subplot(514, sharex=ax1 )
    ax4.scatter(data7['MJD'], data7['Favg'],  marker='o', color='orange', s=15)
    ax4.errorbar(data7['MJD'], data7['Favg'], data7['Flux-err'], fmt='.', ecolor='black',color='orange', elinewidth=1,capsize=3)

    ax5 = fig.add_subplot(515, sharex=ax1 )
    ax5.scatter(data8['MJD'], data8['Favg'],  marker='o', color='sienna', s=15)
    ax5.errorbar(data8['MJD'], data8['Favg'], data8['Flux-err'], fmt='.', ecolor='black',color='sienna', elinewidth=1,capsize=3)

    plt.setp(ax1.get_xticklabels(), visible=False) # hide labels
    fig.subplots_adjust(hspace=0) # remove vertical space between subplots

    red_line = mlines.Line2D([], [], color='red', marker='o', markersize=5, label='NGC 4151 - 1325 $\AA$')
    ax1.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0, handles=[red_line])
    red_line = mlines.Line2D([], [], color='blue', marker='o', markersize=5, label='NGC 4151 - 1425 $\AA$')
    ax2.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0, handles=[red_line])
    red_line = mlines.Line2D([], [], color='green', marker='o', markersize=5, label='NGC 4151 - 1655 $\AA$')
    ax3.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0, handles=[red_line])
    red_line = mlines.Line2D([], [], color='orange', marker='o', markersize=5, label='NGC 4151 - 2475 $\AA$')
    ax4.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0, handles=[red_line])
    red_line = mlines.Line2D([], [], color='sienna', marker='o', markersize=5, label='NGC 4151 - 2725 $\AA$')
    ax5.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0, handles=[red_line])


    plt.xlabel('MJD $=$ 2985000 $-$ JD', ha='center',size=10)
    plt.ylabel('Continuum Flux (E$- erg s$^{-1}$ cm$^{-2}$ $\AA^{-1}$)',ha = 'left', va= 'bottom', size=10)


    plt.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
    plt.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)
    plt.tick_params(direction='out', length=8, width=3)
    plt.tick_params(labelsize=7.5)


    plt.savefig("1993.jpeg")
    fig.set_size_inches(w=13,h=10)
    plt.show()
    plt.close()


fit_data()

如果我增加字体大小,y 标签会与 y 轴刻度重叠。放样图像的底部也有粗体勾号,这不是必需的。

您混合了 pyplot 和 OO API,这总是会导致问题。请参阅 here in the doc 以获得简洁的解释。

您在代码中做了很多必要的事情(例如创建一个 15x15 的图形,然后将其调整为 13x10),并且当您说您不想要他们...

为了使 ylabel 居中,我将 plt.ylabel() 替换为 fig.text(),这样您就可以将文本放置在图形坐标中,因此使用 y=0.5 可确保文本位于图的中间。您需要调整 x 位置,使其不会干扰您的 yticks。

这是一个简化的代码:

fig = plt.figure(1,figsize=(13,10))

ax1 = fig.add_subplot(511)
ax2 = fig.add_subplot(512, sharex=ax1)
ax3 = fig.add_subplot(513, sharex=ax1)
ax4 = fig.add_subplot(514, sharex=ax1)
ax5 = fig.add_subplot(515, sharex=ax1)
fig.subplots_adjust(hspace=0)

for ax in [ax1,ax2,ax3,ax4]:
    ax.tick_params('x', bottom=False, labelbottom=False)

ax1.set_title('NGC 4151 - Year - 1993', size = 18)
ax5.set_xlabel('MJD $=$ 2985000 $-$ JD', size=10)
fig.text(0.075,0.5,'Continuum Flux (E$- erg s$^{-1}$ cm$^{-2}$ $\AA^{-1}$)', ha='center', va='center', size=10, rotation=90)

plt.show()