How to Color Scatter Plots by Column Values in Python?

DDD
Release: 2024-10-19 14:48:30
Original
349 people have browsed it

How to Color Scatter Plots by Column Values in Python?

Coloring Scatter Plots by Column Values

In Python, the Matplotlib library provides several means of customizing scatter plot aesthetics. One common task is assigning colors based on values in a specific column.

Seaborn Integration

One solution is to leverage the Seaborn library, which builds upon Matplotlib. Seaborn offers high-level functions like sns.relplot and sns.FacetGrid that allow you to easily map scatter plots onto specific columns. By specifying the hue parameter, you can color points according to a third column containing category labels.

<code class="python">import seaborn as sns

sns.relplot(data=df, x='Weight (kg)', y='Height (cm)', hue='Gender')</code>
Copy after login

Directly Using Matplotlib

Alternatively, you can directly use Matplotlib's plt.scatter function to create scatter plots and specify colors manually. This requires creating a custom color dictionary that maps category labels to colors.

<code class="python">def dfScatter(df, xcol='Height', ycol='Weight', catcol='Gender'):
    fig, ax = plt.subplots()

    categories = np.unique(df[catcol])
    colors = np.linspace(0, 1, len(categories))
    colordict = dict(zip(categories, colors))

    df['Color'] = df[catcol].apply(lambda x: colordict[x])
    ax.scatter(df[xcol], df[ycol], c=df.Color)
    return fig</code>
Copy after login

By calling this function, you can generate a scatter plot colored by the specified category column:

<code class="python">df = pd.DataFrame({'Height': np.random.normal(size=10),
                   'Weight': np.random.normal(size=10),
                   'Gender': ["Male", "Male", "Unknown", "Male", "Male",
                              "Female", "Did not respond", "Unknown", "Female", "Female"]})
fig = dfScatter(df)</code>
Copy after login

The above is the detailed content of How to Color Scatter Plots by Column Values in Python?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!