简体   繁体   English

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

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

I create 2 figures.我创造了 2 个数字。 One is a Seaborn figure and one is a matplotlib figure.一张是Seaborn图,一张是matplotlib图。 Now I would like to combined those 2 figures into 1 combined figure.现在我想将这 2 个数字合并为 1 个组合数字。 While the matplotlib figure is being displayed on the left hand side, the seaborn figure is not displayed.当 matplotlib 数字显示在左侧时,seaborn 数字未显示。 Here is the code这是代码

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

When running the code I get the following error "AttributeError: 'numpy.ndarray' object has no attribute 'xaxis'".运行代码时出现以下错误“AttributeError:‘numpy.ndarray’object 没有属性‘xaxis’”。 The single seaborn figure is created and the combined matplotlib figure is also created.创建了单个 seaborn 图,还创建了组合图 matplotlib。 But only on the left side you can see the correct matplotlib figure while on the right side it is just empty.但只有在左侧您可以看到正确的 matplotlib 数字,而在右侧它只是空的。

Any ideas how I can do this?我有什么想法可以做到这一点?

Some hopefully clarifying comments:一些希望澄清的评论:

A figure is the top-level container for all plot elements.图是所有 plot 元素的顶级容器。 It's misleading/incorrect to refer to a matplotlib figure or seaborn figure, when really you're referring to an Axes .引用 matplotlib 图形或 seaborn 图形是误导性的/不正确的,而实际上您指的是Axes

This creates one figure with two subplots.这将创建一个带有两个子图的图形。

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

Pure matplotlib plotting:纯 matplotlib 绘图:

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

Seaborn plotting: passing an existing Axes to kdeplot : 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