Home > Back-end >  AttributeError: 'AxesSubplot' object has no attribute 'sharex'
AttributeError: 'AxesSubplot' object has no attribute 'sharex'

Time:04-21

I have some code that works perfectly on my local machine with Python 3.7 but fails on a distant server with Python 3.7 also. What is weird is that the fail error is AttributeError: 'AxesSubplot' object has no attribute 'sharex'. Yet, AxesSubplot should have a sharex as an attribute so I don't understand where it can come from. For debugging, here is a very short piece of code that doesn't work on my distant server:

import numpy as np ; import matplotlib.pyplot as plt

x=np.arange(50) ; y = x

title='test'
plt.close(title)
fig=plt.figure(title, clear=True)
fig.suptitle(title)
ax1,ax2=fig.subplots(nrows=2)
ax1.sharex(ax2)

Do you see what can be the origin of the problem?

CodePudding user response:

Since the code is running correctly on one of your computers, the problem does not seem to be in the code itself. So the first thing that comes to mind is checking the versions of your installed matplotlib libraries. It could be that this method does not exist in all versions.

Looking at the documentation of the Axes class, you can see that the sharex() method is documented in version 3.3, but not in version 3.2. So one of the computers is probably running an older version and upgrading to version 3.3.0 or higher should solve the problem.

You already found out that for versions lower than 3.3 it can also be solved by using:

ax1.get_shared_x_axes().join(ax1,ax2)

CodePudding user response:

I think the error is coming from this line here:

fig=plt.figure(title, clear=True)

Remove the clear=True bit and try again.

  • Related