Home > Article > Backend Development > How to Plot Different Data Categories with Colors in Matplotlib and Seaborn?
In this article, we explore various methods for creating a scatter plot in Python's matplotlib library, where data points are color-coded based on different categorical levels.
matplotlib provides a c parameter for plt.scatter(), which allows for color customization. This parameter can be set to a list or dictionary that maps category values to colors.
<code class="python">import matplotlib.pyplot as plt import pandas as pd # Load data df = pd.read_csv("diamonds.csv") # Create a color map colors = {'D':'tab:blue', 'E':'tab:orange', 'F':'tab:green', 'G':'tab:red', 'H':'tab:purple', 'I':'tab:brown', 'J':'tab:pink'} # Plot data with color mapping plt.scatter(df['carat'], df['price'], c=df['color'].map(colors)) plt.show()</code>
Seaborn is a library that provides a concise API for creating statistical graphics with matplotlib. To create a scatter plot with color-coded data points using seaborn, use the sns.lmplot() function with fit_reg=False to disable regression.
<code class="python">import seaborn as sns # Plot data with color-coding sns.lmplot(x='carat', y='price', data=df, hue='color', fit_reg=False)</code>
If you prefer not to use seaborn, you can achieve the same result manually using pandas.groupby() and pandas.DataFrame.plot(). This method involves grouping the data by color and then plotting each group individually with a specified color.
<code class="python">fig, ax = plt.subplots() grouped = df.groupby('color') for key, group in grouped: group.plot(ax=ax, kind='scatter', x='carat', y='price', label=key, color=colors[key])</code>
By implementing these techniques, you can create informative scatter plots that visually represent relationships between different categorical levels.
The above is the detailed content of How to Plot Different Data Categories with Colors in Matplotlib and Seaborn?. For more information, please follow other related articles on the PHP Chinese website!