问题描述
我似乎陷入了一个相对简单的问题,但在搜索了最后一个小时并且经过大量实验后无法解决.
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.
我有两个numpy数组x
和y
,我正在使用seaborn的关节图来绘制它们:
I have two numpy arrays x
and y
and I am using seaborn's jointplot to plot them:
sns.jointplot(x, y)
现在,我想将x轴和y轴分别标记为"X轴标签"和"Y轴标签".如果使用plt.xlabel
,则标签将转到边际分布.如何使它们出现在关节轴上?
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?
推荐答案
sns.jointplot
返回 JointGrid 对象,它使您可以访问matplotlib轴,然后可以从那里进行操作.
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()
(您尝试的问题是,像plt.xlabel("text")
这样的函数在当前轴上运行,而不是sns.jointplot
中的中心轴;但是面向对象的接口更具体地说明了它将在什么轴上运行).
(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).
这篇关于自定义Seaborn关节图中的轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!