


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>
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>
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Fastapi ...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...
