在seaborn jointplot中自定义轴标签

时间:2018-03-02 09:05:04

标签: numpy matplotlib seaborn

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

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

sns.jointplot(x, y)

现在我想分别将x轴和y轴标记为“X轴标签”和“Y轴标签”。如果我使用plt.xlabel,则标签会转到边际分布。如何让它们出现在关节轴上?

2 个答案:

答案 0 :(得分:14)

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
plt.tight_layout()

seaborn jointplot with custom labels

(你的尝试的问题是像plt.xlabel("text")这样的函数在当前轴上运行,而不是sns.jointplot中的中心轴;但是面向对象的接口更具体,它将是什么经营)。

答案 1 :(得分:0)

或者,您可以在DataFrame的调用中在pandas jointplot中指定轴标签。

import pandas as pd
import seaborn as sns

x = ...
y = ...
data = pd.DataFrame({
    'X-axis label': x,
    'Y-axis label': y,
})
sns.jointplot(x='X-axis label', y='Y-axis label', data=data)
相关问题