Search code examples
matplotlibseaborn

Avoid seaborn influencing matplotlib plots, none of the solutions work


I know this question was already asked and answered (see here and here), but the answers are not working for me.

I'm first plotting some figure with matplotlib and then one with seaborn. After I imported seaborn in the cell I'm plotting something with it, it changes the color of all following matplotlib plot, and the previous plots too if I'm rerunning the cell. This is very annoying. See the color difference.

enter image description here

enter image description here

  • I tried plt.style.use('default') at the beginning of my notebook, it doesn't work.
  • I tried # reset RC params to original sns.reset_orig() at the beginning and after my cell, doesn't work either.
  • I tried import seaborn.apionly as sns, and there are no such module...
  • I tried:# Now, reset Matplotlib color cycle to default plt.rcParams['axes.prop_cycle'] = plt.rcParamsDefault['axes.prop_cycle'], same.

What can I do ?

I'm plotting the matplotlib figure with the base color (b, m, c, k, ...). This is my code for my seaborn figure:

    # When you import seaborn the default styling is changed.
import seaborn as sns

## Reset Seaborn to default Matplotlib settings
sns.reset_orig()

# transform the numpy array into panda data frame
df = pd.DataFrame(data_T)

# Replace the name of the column with labels defines above
df.columns = labels

# Create pairplot with only the lower triangular part
g = sns.set(style="ticks")
g = sns.pairplot(df,
                 corner=True,
                 diag_kind='hist',
                 plot_kws = dict(marker="+", linewidth=1),
                 vars = labels, # labels
                 hue = "pwv [mm]") # variable in 3rd dimension
                # , palette = colors) # varslist of variable names



# remove the 3rd dimension in the histogram
g.map_diag(sns.histplot, hue=None, color = '0.4')

# palette dict or seaborn color palette. Set of colors for mapping the hue variable. If a dict, keys should be values in the hue variable.

plt.savefig('triqngle_plot02.png')

Solution

  • I'll add my remarks with #--> into the code

    # When you import seaborn the default styling is changed.
    #--> Well, no, importing seaborn doesn't change the default styling
    import seaborn as sns
    
    ## Reset Seaborn to default Matplotlib settings
    sns.reset_orig()
    #--> Not necessary, as the default styling didn't change
    
    # transform the numpy array into panda data frame
    #--> (data_T wasn't definded...)
    df = pd.DataFrame(data_T)
    
    # Replace the name of the column with labels defines above
    #--> (the labels weren't definded...)
    #--> (you can also add the column names in `df = pd.DataFrame(data_T, columns=labels))`)
    df.columns = labels
    
    
    # Create pairplot with only the lower triangular part
    g = sns.set(style="ticks")
    #--> Well, here you ask seaborn to change the default styling
    #--> You can use `sns.set(style="ticks", color_codes=False)` if you
    #        don't want to change matplotlib's standard colors
    #--> (You shouldn't assign the result of `sns.set`, it has no return value)
    
    
    g = sns.pairplot(df,
                     corner=True,
                     diag_kind='hist',
                     plot_kws = dict(marker="+", linewidth=1),
                     vars = labels, # labels
                     hue = "pwv [mm]") # variable in 3rd dimension
                    # , palette = colors) # varslist of variable names
    
    
    # remove the 3rd dimension in the histogram
    g.map_diag(sns.histplot, hue=None, color='0.4')
    
    #--> here you could call `sns.reset_orig()`
    
    plt.savefig('triqngle_plot02.png')