【问题标题】:avoid seaborn influencing matplotlib plots避免 seaborn 影响 matplotlib 图
【发布时间】:2019-02-26 12:30:25
【问题描述】:

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

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 情节的正常人: mpl plot normal

如果使用显示的代码,它会如何变化: mpl with sns in front

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

【问题讨论】:

    标签: python matplotlib plot seaborn


    【解决方案1】:

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

    您可以使用 plt.style.use 命令更改 matplotlib 应用于绘图的样式。

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

    【讨论】:

    • 我不敢相信!没有赞成票的公认答案。 '投票赞成'
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2023-03-30
    • 2023-03-21
    • 2020-08-07
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多