Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'AxesSubplot' object has no attribute 'sharex'

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?

like image 503
cpannet' Avatar asked Apr 11 '26 12:04

cpannet'


1 Answers

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)
like image 51
wovano Avatar answered Apr 14 '26 02:04

wovano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!