避免对Matplotlib积图产生重大影响

时间:2019-02-26 12:30:26

标签: python matplotlib plot seaborn

我已经找到一个涉及类似主题的条目,但是该建议在这里不起作用。

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 plot

但是matplotlib图的外观不同。正常情况下,不会在前面造成积压的情况: mpl plot normal

以及如何使用显示的代码进行更改: mpl with sns in front

我该如何停止这种行为,避免先天因素影响其他地块?

1 个答案:

答案 0 :(得分:0)

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

您可以使用$.getJSON( "ajax/test.php", function( data ) { console.log(data); } 命令更改matplotlib适用于绘图的样式。

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