[파이썬] Matplotlib 팔레트 설정

Matplotlib is a powerful and widely used data visualization library in Python. It provides a wide range of customizable styles and colors for plots and charts. One of the key features of Matplotlib is the ability to set the color palette for your plots, which can greatly enhance the visual appeal and effectiveness of your data visualizations.

In this blog post, we will explore different ways to set the color palette in Matplotlib and create appealing and informative plots.

Default Color Palette

By default, Matplotlib uses a set of colors from the ‘tab10’ color palette. This palette consists of 10 distinct colors that can be used to differentiate between different plot elements. However, you can easily change the default color palette to suit your preferences or the requirements of your data.

To change the default color palette, you can use the set_palette function from the seaborn library, which is built on top of Matplotlib and provides additional functionality for statistical data visualization.

import seaborn as sns

# Set the default color palette
sns.set_palette("husl")

In the example above, we set the default color palette to ‘husl’ which stands for Hue-Saturation-Lightness. This palette is often preferred for visualizations as it provides a good range of colors that are visually distinct and appealing.

Choosing a Color Palette

In addition to the default color palette, Matplotlib provides several other built-in palettes that you can choose from. Some of the commonly used palettes include ‘viridis’, ‘plasma’, ‘magma’, ‘inferno’, ‘coolwarm’, and ‘Set1’. These palettes offer different variations of colors and can be selected based on the type of data you are visualizing and the effect you want to achieve.

You can set a specific color palette using the set_palette function from the seaborn library.

import seaborn as sns

# Set a specific color palette
sns.set_palette("viridis")

The example above sets the color palette to ‘viridis’, which is a perceptually uniform and visually appealing palette that is widely used in scientific visualizations.

Custom Color Palettes

If the built-in palettes do not meet your requirements, you can create your own custom color palette by specifying a list of colors. Matplotlib provides the ListedColormap class from the matplotlib.colors module, which allows you to create a colormap from any list of colors.

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

# Create a custom color palette
colors = ["#FF0000", "#00FF00", "#0000FF"]  # Red, Green, Blue
cmap = ListedColormap(colors)

# Use the custom color palette
plt.scatter(x, y, c=z, cmap=cmap)

In the example above, we create a custom color palette with three colors: red, green, and blue. We then use this custom color palette in a scatter plot by setting the cmap parameter to cmap, which is the custom color palette.

Conclusion

Setting the color palette in Matplotlib allows you to create visually appealing and informative plots. Whether you choose a built-in palette or create your own custom palette, exploring different colors can greatly enhance the overall visual impact of your data visualizations.

By using the techniques outlined in this blog post, you can easily experiment with different color palettes and find the one that best suits your data and visual requirements. So go ahead and unleash your creativity with Matplotlib!