[파이썬] `seaborn` 컬러 팔레트

Seaborn comes with a variety of pre-defined color palettes that you can easily use in your code. These palettes range from simple sequential or diverging colors to more complex qualitative palettes. Let’s see some examples of how to use seaborn color palettes:

  1. Sequential palettes: Seaborn provides several sequential color palettes that are useful for representing data that has a continuous range, such as heatmaps or line plots. Some common sequential palettes are viridis, cubehelix, and rocket. To use a sequential palette, you can use the sns.color_palette() function.

    import seaborn as sns
    
    # Set the default seaborn color palette
    sns.set()
    
    # Use the viridis sequential color palette
    colors = sns.color_palette("viridis")
    
    # Plot a line chart
    sns.lineplot(x=[1, 2, 3, 4, 5], y=[2, 4, 6, 8, 10], palette=colors)
    
  2. Diverging palettes: Diverging palettes are useful when you want to emphasize both high and low values in your data. These palettes have a middle hue that represents the neutral or average value, with different colors on either side representing high and low values. Examples of diverging palettes in seaborn are coolwarm and RdBu. To use a diverging palette, you can use the sns.diverging_palette() function.

    import seaborn as sns
    
    # Use the coolwarm diverging color palette
    colors = sns.diverging_palette(240, 10, as_cmap=True)
    
    # Plot a heatmap
    sns.heatmap(data, cmap=colors)
    
  3. Qualitative palettes: Qualitative palettes are useful for representing categorical data, where each category is represented by a different color. Examples of qualitative palettes in seaborn are Set1, Set2, and Set3. To use a qualitative palette, you can use the sns.color_palette() function.

    import seaborn as sns
    
    # Use the Set1 qualitative color palette
    colors = sns.color_palette("Set1", n_colors=5)
    
    # Plot a bar chart
    sns.barplot(x=["A", "B", "C", "D", "E"], y=[5, 8, 3, 6, 9], palette=colors)
    

Seaborn color palettes provide a wide range of options to enhance the visual appeal of your data visualizations. Experiment with different palettes and find the one that best suits your data and the message you want to convey. Happy visualizing!