Customize the axis label in seaborn jointplot

狂风中的少年 提交于 2019-12-21 03:45:48

问题


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?


回答1:


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

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




回答2:


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)


来源:https://stackoverflow.com/questions/49065837/customize-the-axis-label-in-seaborn-jointplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!