避免seaborn影响matplotlib情节

问题描述 投票:1回答:1

我已经找到了一个处理类似主题的条目,但这个建议在这里不起作用。

How can I use seaborn without changing the matplotlib defaults?

如果我错过了什么,我很感激每一个环节。

我想用seaborn创建一个情节后用matplotlib创建一个情节。然而,seaborn的设置似乎影响matplotlib外观(我意识到seaborn是matplotlib的扩展)。即使我清除,关闭情节等,也会发生这种情况。

    sns.reset_orig()
    plt.clf()
    plt.close()

完整的示例代码:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# data
df = pd.DataFrame(np.array([[1, 1], [2, 2], [3, 3]]),columns=['x', 'y'])

###### seaborn plot #######
fig=sns.JointGrid(x=df['x'],
                  y=df['y'],
                  )
#fill with scatter and distribution plot
fig = fig.plot_joint(plt.scatter, color="b")                            
fig = fig.plot_marginals(sns.distplot, kde=False, color="b")

#axis labels
fig.set_axis_labels('x','y')        

#set title
plt.subplots_adjust(top=0.92)
title='some title'
fig.fig.suptitle(title)

#clear and close figure
sns.reset_orig()
plt.clf()
plt.close()        

###### matplotlib plot #######
#define data to plot
x = df['x']
y = df['y']

#create figure and plot
fig_mpl, ax = plt.subplots()
ax.plot(x,y,'.')
ax.grid(True)
ax.set_xlabel('x')
ax.set_ylabel('y')
title='some title'
ax.set_title(title)
plt.close()

seaborn情节总是看起来一样:seaborn plot

但matplotlib图的外观不同。正常的一个没有在前面创造一个seaborn情节:qazxsw poi

如果使用显示的代码,它会如何变化:mpl plot normal

我该如何阻止这种行为,避免seaborn影响其他情节?

python matplotlib plot seaborn
1个回答
0
投票

导入seaborn时,默认样式会更改。

您可以使用mpl with sns in front命令更改matplotlib应用于绘图的样式。

要获得可用样式列表,您可以使用plt.style.use。要更改回经典的matplotlib样式,您需要使用plt.style.available

© www.soinside.com 2019 - 2024. All rights reserved.