Aitor Almon
Aitor Almon

Reputation: 25

plt. in subplot works only for one plot

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.

Upvotes: 1

Views: 4785

Answers (2)

Erik André
Erik André

Reputation: 538

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)

Upvotes: 1

tuchuk
tuchuk

Reputation: 1

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)

Upvotes: 0

Related Questions