gpt4 book ai didi

numpy - 自定义 seaborn 联合图中的轴标签

转载 作者:行者123 更新时间:2023-12-04 00:41:08 24 4
gpt4 key购买 nike

我似乎陷入了一个相对简单的问题,但在搜索了最后一个小时并经过大量实验后无法解决它。

我有两个 numpy 数组 xy我正在使用 seaborn 的联合图来绘制它们:

sns.jointplot(x, y)

现在我想将 xaxis 和 yaxis 分别标记为“X 轴标签”和“Y 轴标签”。如果我使用 plt.xlabel ,标签进入边缘分布。如何让它们出现在关节轴上?

最佳答案

sns.jointplot返回 JointGrid对象,它使您可以访问 matplotlib 轴,然后您可以从那里进行操作。

import seaborn as sns
import numpy as np

# example data
X = np.random.randn(1000,)
Y = 0.2 * np.random.randn(1000) + 0.5

h = sns.jointplot(X, Y)

# JointGrid has a convenience function
h.set_axis_labels('x', 'y', fontsize=16)

# or set labels via the axes objects
h.ax_joint.set_xlabel('new x label', fontweight='bold')

# also possible to manipulate the histogram plots this way, e.g.
h.ax_marg_y.grid('on') # with ugly consequences...

# labels appear outside of plot area, so auto-adjust
h.figure.tight_layout()
seaborn jointplot with custom labels
(您尝试的问题是 plt.xlabel("text") 等函数在当前轴上运行,这不是 sns.jointplot 中的中心轴;但面向对象的接口(interface)更具体地说明它将在什么上运行)。
请注意,最后一个命令使用 JointGrid 的图形属性。这个答案的初始版本通过 matplotlib.pyplot 使用了更简单但不是面向对象的方法。界面。
要使用 pyplot 界面:
import matplotlib.pyplot as plt
plt.tight_layout()

关于numpy - 自定义 seaborn 联合图中的轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com