1

I'm new to python so I hope my question is good enough, I'm trying to create two subplots based on two different data frames. My problem is that when I try to define titles and xlim it works only on one plot.

This is my script:

fig, axes = plt.subplots(1,2,figsize=(18,6))

#Original data
df_codes.loc[:,float_cols_gb].T.plot(ax=axes[0])
plt.title('Original Data', size=(20))
plt.ylabel('Reflectence', size=(14))
plt.xlabel('Wavelength', size=(14))
plt.xlim(410,1004)

#filter  data
df_bl_codes.loc[:,float_cols_bl].T.plot(ax=axes[1])
plt.title( 'Filter', size=(20))
plt.ylabel('Reflectence', size=(14))
plt.xlabel('Wavelength', size=(14))
plt.xlim(410,1004)

I cannot attach image as i'm new user here, but the result is two plots, one gets the titles and the xlim (the one in column 1) and one stays without the ttiles and xlim (the one in column 0).

My end goal: to applly the xlimand also the titles to each plot in the subplots.

Aitor Almon
  • 25
  • 1
  • 5
  • You should use a for loop over your axes to then use [`ax.plot()`](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.axes.Axes.plot.html). – swiss_knight Apr 18 '20 at 07:55
  • 2
    Does this answer your question? [How do I get multiple subplots in matplotlib?](https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib) – swiss_knight Apr 18 '20 at 07:57

2 Answers2

1

Let's try to understand what is happening, and help you improve the way you create your plots in the future.

The line

fig, axes = plt.subplots(1,2,figsize=(18,6))

creates two objects (everything in Python is an object): A matplotlib.pyplot.Figure object, and a list containing two matplotlib.pyplot.Axes objects. When you then do something like plt.title('Original Data', size=(20)), matplotlib will add this title to what it deems to be the current Axes object—as you haven't told matplotlib which object this is, it will assume it is the first one in the array it just created. Unless you tell it otherwise (with plt.sca(), but there is a better way), it will always assume this, and later calls to plt.title() will overwrite the previous value.

To fix this, use the built-in methods directly on the Axes objects instead. You can access these by indexing the 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)
Erik André
  • 528
  • 4
  • 13
0

In the case of subplots, you should work with axes instances. Trying doing the following:

fig, axes = plt.subplots(1,2,figsize=(18,6))

#Original data
df_codes.loc[:,float_cols_gb].T.plot(ax=axes[0])
ax[0].set_title('Original Data', size=(20))
ax[0].set_ylabel('Reflectence', size=(14))
ax[0].set_xlabel('Wavelength', size=(14))
ax[0].set_xlim(410,1004)

#filter  data
df_bl_codes.loc[:,float_cols_bl].T.plot(ax=axes[1])
ax[1].set_title( 'Filter', size=(20))
ax[1].set_ylabel('Reflectence', size=(14))
ax[1].set_xlabel('Wavelength', size=(14))
ax[1].set_xlim(410,1004)
tuchuk
  • 1
  • 1
  • 1