Customize the axis label in seaborn jointplot

2020-05-25 19:41发布

I seem to have got stuck at a relatively simple problem but couldn't fix it after searching for last hour and after lot of experimenting.

I have two numpy arrays x and y and I am using seaborn's jointplot to plot them:

sns.jointplot(x, y)

Now I want to label the xaxis and yaxis as "X-axis label" and "Y-axis label" respectively. If I use plt.xlabel, the labels goes to the marginal distribution. How can I make them appear on the joint axes?

2条回答
劫难
2楼-- · 2020-05-25 20:25

Alternatively, you can specify the axes labels in a pandas DataFrame in the call to 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)
查看更多
smile是对你的礼貌
3楼-- · 2020-05-25 20:40

sns.jointplot returns a JointGrid object, which gives you access to the matplotlib axes and you can then manipulate from there.

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

(The problem with your attempt is that functions such as plt.xlabel("text") operate on the current axis, which is not the central one in sns.jointplot; but the object-oriented interface is more specific as to what it will operate on).

查看更多
登录 后发表回答