user16110072
user16110072

Reputation: 11

Problem with sns boxplot and the hatch pattern

I have this code
#Plot

    # Define a custom palette for the strains
    custom_palette = {
        'H9CK': 'green',
        'H9RT': 'blue',
        'H9TK': 'red',
        'H9WD': 'orange'
    }  
    # Define a custom hatching pattern for each strain
    hatch_patterns = {
        'H9CK': '//',    # Diagonal lines
        'H9RT': '\\\\',  # Opposite diagonal lines
        'H9TK': '--',    # Horizontal dashes
        'H9WD': 'xx'     # Cross hatching
    }
    # Plot
    plt.figure(figsize=(10, 6))
    boxplot = sns.boxplot(
        data=subset_df, 
        x='dpi', 
        y='ΔΔCt', 
        hue='strain', 
        palette=custom_palette, 
        showfliers=False
    )
    # Add hatching patterns to the boxes
    for i, patch in enumerate(boxplot.patches):
        # Determine the strain for the current box
        strain = subset_df['strain'].unique()[i % len(hatch_patterns)]
        patch.set_hatch(hatch_patterns[strain])
    # Add labels and title
    plt.title('Boxplot of ΔΔCt by dpi and strain')
    plt.xlabel('DPI (Days post-infection)')
    plt.ylabel('ΔΔCt')
    # Move the legend to the upper right corner
    plt.legend(title='Strain', loc='upper right', bbox_to_anchor=(1.05, 1), borderaxespad=0.)
    # Adjust layout
    plt.tight_layout()
    # Show the plot
    plt.show() 

which is giving me this kind of plots enter image description here Why and how do I fix that the pattern is different on day number 1?

Thank you all!

I want it to have the same patterns

Upvotes: 1

Views: 16

Answers (0)

Related Questions