1

I am trying to plot Facet Grid, I have created whole function so it is more convenient, when I test it in my code editor it shows like it is in this

enter image description here

, but when I actually save it is shown like this:

enter image description here

, I need it to be saved exactly it is previewed not like reverted colors.

I tried changing the background color of the grid and made it black and it was terrible didn't work the way I expected

this is my current code:

import seaborn as sns
import matplotlib.pyplot as plt


#################################### FACET ####################################
def facet(data, x, y, col, hue, col_wrap=3, height=3, aspect=1.5, theme='dark',
          x_label=None, y_label=None, palette="crest", grid=True, save_path=None,
          background_opacity=0.2, back_line_color='.7'):

    # Set the theme for Seaborn plots
    sns.set_theme(style=theme)

    #### OTHER VISUALISATION CODE ###

    # Remove default titles from facets
    g.set_titles("")

    # Adjust layout to prevent overlap
    g.tight_layout()

    # Display the final plot
    plt.show()

2 Answers 2

1

I dunno how to precisely save the view, but maybe plt.style could help you.
Example code using style:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(50)

plt.style.use('Solarize_Light2')

plt.plot(data)
plt.show() 

You can use plt.style.available() to get the table of available styles, there are many great styles out there.
I also tried for myself, and the style thing shows perfectly when saved as image.

1

I have solved the problem, I had to manually adjust the color of general background and color of facets background, this is shortened code how I did it:

import seaborn as sns
import matplotlib.pyplot as plt

#################################### FACET ####################################
def facet(data, x, y, col, hue, col_wrap=3, height=3, aspect=1.5, theme='dark',
          x_label=None, y_label=None, palette="crest", grid=True, save_path=None,
          background_opacity=0.2, back_line_color='#468E98', background_color="#222222",
          facet_bg_color="#26262e", text_color="#b6b6b5", spine=False, x_rotation=0):

    # Set the theme for Seaborn plots
    sns.set_theme(style=theme)

    # Create a FacetGrid using Seaborn's `relplot`
    g = sns.relplot(
        data=data,  # Dataset
        x=data[x], y=data[y],  # X and Y variables
        col=data[col],  # Separate plots by unique values in `col`
        hue=data[hue],  # Different colors based on `hue` column
        kind="line",  # Line plot
        palette=palette,  # Color palette
        linewidth=2,  # Adjusted line width
        zorder=5,  # Ensures primary lines are drawn on top
        col_wrap=col_wrap,  # Wrap facet columns
        height=height,  # Height of each facet
        aspect=aspect,  # Aspect ratio of each facet
        legend=False,  # Hide automatic legends
    )

    # Set background color of the entire figure
    g.figure.set_facecolor(background_color)

    for category, ax in g.axes_dict.items():
        # Get dynamic limits for x and y axes
        ax.set_facecolor(facet_bg_color)  # Set facet background color
    ############## OTHER PART OF THE CODE FOR PLOT ##############

so in my code background_color is used for overall background color of the unity of facets and facet_bg_color is the color of each facet shown on the figure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.