Home > Backend Development > Python Tutorial > How to Plot Different Data Categories with Colors in Matplotlib and Seaborn?

How to Plot Different Data Categories with Colors in Matplotlib and Seaborn?

Mary-Kate Olsen
Release: 2024-10-17 16:36:03
Original
756 people have browsed it

How to Plot Different Data Categories with Colors in Matplotlib and Seaborn?

Plotting Different Colors for Different Categorical Levels

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.

Using matplotlib

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>
Copy after login

Using seaborn

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>
Copy after login

Using pandas.DataFrame.groupby & pandas.DataFrame.plot

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template