0

Practicing on visualization as a Python newbie I have encountered this conceptual issue that got me thinking, Infact I managed to change the price format on the y axis of a boxplot , from scientific notation to something more clear. Here the outputs before and after the formatting of the y axis before

after

boxy=sns.boxplot(x="waterfront", y="price", data=df)

# my experiments success
from matplotlib.ticker import FuncFormatter
f = lambda x, pos: f'{x:.0f}'
boxy.yaxis.set_major_formatter(FuncFormatter(f))

the problem is that I realized that the attribute yaxis should refer to an AXIS object, meanwhile here what i call 'boxy' is an AXES object (at least from the seaborn documentation)

Can anyone explain it?

1 Answers1

1

You're right saying that seaborn boxplot returns a matplotlib Axes object. And referring to this answer, we see Axes and Axis objects are different.

Code inspection isn't needed... but under the hood, seaborn uses matplotlib, it is noted in the GitHub here for boxplots.

when you call sns.boxplot part of drawing your plot creates Axis objects... which are objects of the matplotlib.axis module.

The y axis is in fact the first part of boxy.yaxis.set_major_formatter(FuncFormatter(f)) it is accessed with boxy.yaxis. On which you are calling the function .set_major_formatter(FuncFormatter(f)).

To see this, yaxis = boxy.get_yaxis() will return the yaxis of the boxy axes object.

EDIT IN RESPONSE TO COMMENT: Again you're correct in the comment that this is not documented from what I could find... but if we look in the matplotlib GitHub here, we see in the YAxis class declaration:

class YAxis(Axis):
    __name__ = 'yaxis'

It is just 'YAxis' renamed. Classes will assume their name in the declarative line, unless you re-specify using __name__ which was done here!

It exists!!!

boxy's yaxis inherets the set_major_formatter method from its base class, the 'Axis' class. Just to confirm this hierarchy try looking with method resolution order:

print(type(boxy.get_yaxis()).__mro__)

Should display:

(<class 'matplotlib.axis.YAxis'>, <class 'matplotlib.axis.Axis'>, <class 'matplotlib.artist.Artist'>, <class 'object'>)
GazYah
  • 28
  • 5
  • Hi, The very aspects that you reported created the confusion on this topic to me. What I don't understand is that `boxy.yaxis` it's not ruled by any documentation on matplotlit because it is the same construction as: `matplotlib.axes.yaxis ` that doesn't exist, I couldn't find it on the documentation. Should we infere that `sns.boxplot` returns something that is treated like an AXIS object instead? (In fact matplotlib.axis.yaxis is a fair method/property). To repeat myself: How yaxis is even accessed when i call `boxy.yaxis`? this property shouldn't exist relatively to axes object – Leonardo Melia Apr 27 '22 at 09:59
  • Edited my answer to reflect the clarification in your comment. One line about this naming would be nice on the matplotlib docs part! – GazYah Apr 30 '22 at 21:21