如何以下内容:’axes.yaxis.set_major_formatter(funcformatter(f));工作是因为yaxis应该是轴而不是轴对象的属性?

发布于 2025-01-23 23:14:02 字数 605 浏览 0 评论 0 原文

练习可视化作为python newbie,我遇到了这个概念上的问题,使我思考, 实际上,我设法更改了箱图的Y轴上的价格格式,从科学符号到更清晰的内容。在这里,Y轴格式之前和之后的输出

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))

问题是我意识到属性Yaxis应该指的是轴对象,与此同时,我所说的“ Boxy”是轴对象(至少来自海洋文档)

可以有人解释吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2025-01-30 23:14:02

您是正确的说,Seaborn Boxplot返回Matplotlib轴对象。并参考答案,我们看到轴和轴对象是不同的。

不需要代码检查...但是在引擎盖下,Seaborn使用Matplotlib,在GitHub 在这里用于盒子图。

当您调用 sns.boxplot 绘制图的一部分时,您的绘图会创建轴对象...它是matplotlib.axis模块的对象。

Y轴实际上是 boxy.yaxis.set_major_formatter(funcformatter(f))的第一部分
它可以使用 boxy.yaxis 访问。您正在调用函数 .set_major_formatter(funcformatter(f))

要看到这一点, yaxis = boxy.get_yaxis()将返回Boxy Axes对象的Yaxis。

对评论的回应进行编辑:
同样,您在评论中是正确的,即从我能找到的内容中记录下来...但是如果我们在matplotlib github 在这里,我们在yaxis类声明中看到:

class YAxis(Axis):
    __name__ = 'yaxis'

它只是更名的'yaxis'。除非您使用 __名称__ 重新指定,否则类将在声明行中假设他们的名字!

它存在!!!

Boxy's Yaxis继承 基础类别的方法。只是要确认此层次结构尝试使用方法解析顺序:

print(type(boxy.get_yaxis())。__mro __)

应该显示:

(<class 'matplotlib.axis.YAxis'>, <class 'matplotlib.axis.Axis'>, <class 'matplotlib.artist.Artist'>, <class 'object'>)

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'>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文