让我们尝试了解正在发生的事情,并帮助您改进将来创建绘图的方式。
The line
fig, axes = plt.subplots(1,2,figsize=(18,6))
创建两个对象(Python 中的一切都是对象): Amatplotlib.pyplot.Figure对象,以及一个包含两个的列表matplotlib.pyplot.Axes对象。当你做类似的事情时plt.title('Original Data', size=(20))
, matplotlib
会将此标题添加到它认为的内容中current Axes
对象 - 因为您没有告诉 matplotlib 这是哪个对象,所以它会假设它是刚刚创建的数组中的第一个对象。除非你另有说明(与plt.sca(),但有更好的方法),它会always假设这一点,然后调用plt.title()
将覆盖之前的值。
要解决此问题,请直接使用内置方法Axes
对象代替。您可以通过索引来访问这些axes
list:
fig, axes = plt.subplots(1,2,figsize=(18,6))
#Original data
df_codes.loc[:,float_cols_gb].T.plot(ax=axes[0])
axes[0].title('Original Data', size=(20))
axes[0].set_ylabel('Reflectence', size=(14))
axes[0].set_xlabel('Wavelength', size=(14))
axes[0].set_xlim(410,1004)
#filter data
df_bl_codes.loc[:,float_cols_bl].T.plot(ax=axes[1])
axes[1].set_title( 'Filter', size=(20))
axes[1].set_ylabel('Reflectence', size=(14))
axes[1].set_xlabel('Wavelength', size=(14))
axes[1].set_xlim(410,1004)