繁体   English   中英

如何将 matplotlib 数字和 seaborn 数字组合成一个 matplotlib 数字

[英]How to put a matplotlib figure and a seaborn figure into one combined matplotlib figure

我创造了 2 个数字。 一张是Seaborn图,一张是matplotlib图。 现在我想将这 2 个数字合并为 1 个组合数字。 当 matplotlib 数字显示在左侧时,seaborn 数字未显示。 这是代码

#Plot the seaborn figure
fig, ax = plt.subplots(figsize=(12,6))
sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax)
sns.kdeplot(data=data_valid.squeeze(),  color='orange', label='valid', fill=False, ax=ax)
sns.kdeplot(data=data_test.squeeze(),  color='green', label='test', fill=False, ax=ax)
ax.legend(loc=2, prop={'size': 20})
plt.tight_layout()
plt.xticks(fontsize =20)
plt.yticks(fontsize =20)
plt.title(f"{currentFeature}\nKernel density functions", fontsize = 20)
plt.ylabel('Density',fontsize=20)
plt.show()


# Plot a matplotlib figure in a combined plot on the left side
X1 = np.linspace(data_train.min(), data_train.max(), 1000)
X2 = np.linspace(data_valid.min(), data_valid.max(), 1000)
X3 = np.linspace(data_test.min(), data_test.max(), 1000)
fig, ax = plt.subplots(1,2, figsize=(12,6))
ax[0].plot(X1, histogram_dist_train.pdf(X1), label='train')
ax[0].plot(X2, histogram_dist_valid.pdf(X2), label='valid')
ax[0].plot(X3, histogram_dist_test.pdf(X3), label='test')
ax[0].set_title('matplotlib figure', fontsize = 14)
ax[0].legend()
#Try to plot the same seaborn figure from above on the right side of the combined figure
ax[1].plot(sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax))
ax[1].plot(sns.kdeplot(data=data_valid.squeeze(),  color='orange', label='valid', fill=False, ax=ax))
ax[1].plot(sns.kdeplot(data=data_test.squeeze(),  color='green', label='test', fill=False, ax=ax))
ax[1].set_title('seaborn figure', fontsize = 14)
ax[1].legend()

运行代码时出现以下错误“AttributeError:‘numpy.ndarray’object 没有属性‘xaxis’”。 创建了单个 seaborn 图,还创建了组合图 matplotlib。 但只有在左侧您可以看到正确的 matplotlib 数字,而在右侧它只是空的。

我有什么想法可以做到这一点?

一些希望澄清的评论:

图是所有 plot 元素的顶级容器。 引用 matplotlib 图形或 seaborn 图形是误导性的/不正确的,而实际上您指的是Axes

这将创建一个带有两个子图的图形。

fig, ax = plt.subplots(1,2, figsize=(12,6))

纯 matplotlib 绘图:

ax[0].plot(X1, histogram_dist_train.pdf(X1), label='train'))

Seaborn 绘图:将现有Axes传递给kdeplot

sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax[1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM