0

I'm trying to left align ticks of the y axis of a plot and copied the code on the matplotlib documentation but I get an error message AttributeError: 'AxesSubplot' object has no property 'axes_class' with matplotlib==3.3.3 and Python 3.8.7

import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist


def setup_axes(fig, pos):
    ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
    ax.set_yticks([0.2, 0.8], labels=["short", "loooong"])
    ax.set_xticks([0.2, 0.8], labels=[r"$\frac{1}{2}\pi$", r"$\pi$"])
    return ax


fig = plt.figure(figsize=(3, 5))
fig.subplots_adjust(left=0.5, hspace=0.7)

ax = setup_axes(fig, 311)
ax.set_ylabel("ha=right")
ax.set_xlabel("va=baseline")

ax = setup_axes(fig, 312)
ax.axis["left"].major_ticklabels.set_ha("center")
ax.axis["bottom"].major_ticklabels.set_va("top")
ax.set_ylabel("ha=center")
ax.set_xlabel("va=top")

ax = setup_axes(fig, 313)
ax.axis["left"].major_ticklabels.set_ha("left")
ax.axis["bottom"].major_ticklabels.set_va("bottom")
ax.set_ylabel("ha=left")
ax.set_xlabel("va=bottom")

plt.show()

Edit

I tried to remove the mpl_toolkits.axisartist and changed the line ax.axis["left"].major_ticklabels.set_ha("left" to ax.yaxis.major_ticklabels.set_ha("left") as mentioned in the comment but got the error message:

AttributeError: 'YAxis' object has no attribute 'major_ticklabels'

import matplotlib.pyplot as plt
#import mpl_toolkits.axisartist as axisartist


def setup_axes(fig, pos):
  #    ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
    ax = fig.add_subplot(pos)
    ax.set_yticks([0.2, 0.8], labels=["short", "loooong"])
    ax.set_xticks([0.2, 0.8], labels=[r"$\frac{1}{2}\pi$", r"$\pi$"])
    return ax


fig = plt.figure(figsize=(3, 5))
fig.subplots_adjust(left=0.5, hspace=0.7)


ax = setup_axes(fig, 311)
#ax.axis["left"].major_ticklabels.set_ha("left")
#ax.axis["bottom"].major_ticklabels.set_va("bottom")
ax.yaxis.major_ticklabels.set_ha("left")
#ax.axis.major_ticklabels.set_va("bottom")
ax.set_ylabel("ha=left")
ax.set_xlabel("va=bottom")

plt.show()

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • 1
    This feature was added in 3.4.0. See [here](https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.4.0.html#add-subplot-add-axes-gained-an-axes-class-parameter). – r-beginners Jan 10 '22 at 14:42
  • yes @r-beginners. I updated to matplotlib 3.5.0 and it worked perfectly. Many thanks – ecjb Jan 10 '22 at 15:00
  • But to be clear, you don’t need to use `axisartist` here, except instead of ax.axis[‘left’] you would say ax.yaxis, (and bottom->ax.xaxis). Anything you find in the examples that uses `mpl_toolkits` can usually be done using the main library. – Jody Klymak Jan 10 '22 at 15:06
  • Many thanks for your comment @JodyKlymak. I think I just posted another question related to this actually: https://stackoverflow.com/questions/70654534/matplotlib-2-sets-of-xticks-labelling-obtained-a-character-type-one-wanted-a – ecjb Jan 10 '22 at 15:09
  • @JodyKlymak. I tried to follow your advice by implementing a code not using `axisartist` (and posted the code in the edited question) but got an error message. What am I doing wrong? – ecjb Jan 10 '22 at 16:33
  • Many thanks @JohanC! Lots of improvement: Thanks to you I could remove the `axisartist` dependency, left align the yticks and set the font of both xticks and yticks. The only remaining problem is that the yticks are inside the plots. I added a screenshot of the output as well as the latest code in the edited question – ecjb Jan 10 '22 at 17:11

0 Answers0