"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Color Scatter Plots by Column Values in Python?

How to Color Scatter Plots by Column Values in Python?

Published on 2024-11-08
Browse:708

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.

import seaborn as sns

sns.relplot(data=df, x='Weight (kg)', y='Height (cm)', hue='Gender')

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.

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

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

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)
Release Statement This article is reprinted at: 1729320496 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3