簡體   English   中英

避免對Matplotlib積圖產生重大影響

[英]avoid seaborn influencing matplotlib plots

我已經找到了涉及類似主題的條目,但是該建議在這里不起作用。

如何在不更改matplotlib默認值的情況下使用seaborn?

如果我錯過了一些事情,我對每個鏈接都深表感謝。

我想用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()

海上情節總是看起來一樣: 海上情節

但是matplotlib圖的外觀不同。 前面沒有創建海洋圖的普通圖: mpl圖正常

以及如何使用顯示的代碼進行更改: mpl,前面帶有sns

我該如何停止這種行為,以免對其他地塊產生深遠的影響?

導入seaborn時,將更改默認樣式。

您可以使用plt.style.use命令更改matplotlib適用於繪圖的plt.style.use

要獲取可用樣式的列表,可以使用plt.style.available 要改回經典的matplotlib樣式,您需要使用plt.style.use('classic')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM