"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 Assign Colors to Points in Scatter Plots Based on Column Values in Python?

How to Assign Colors to Points in Scatter Plots Based on Column Values in Python?

Published on 2024-11-10
Browse:935

How to Assign Colors to Points in Scatter Plots Based on Column Values in Python?

Coloring Scatter Plots by Column Values in Python

The versatility of ggplot2 in R allows for seamless assignment of colors to data points based on column values. This feature can also be replicated in Python using pandas dataframes and Matplotlib.

Using Pandas and Matplotlib

To map colors to values in Matplotlib, consider the following steps:

  1. Create a color dictionary: Define a dictionary that maps unique values in the categorical column to a corresponding color. This ensures consistent color assignment across data points.
  2. Add a Color column: Create a new column in the dataframe that assigns the corresponding color to each value in the categorical column.
  3. Plot the scatter plot: Use the c parameter in matplotlib.pyplot.scatter to specify the color column as the color argument.

Here's an example implementation:

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

Example Usage

Consider a dataframe with Height, Weight, and Gender columns. To create a scatter plot where colors are assigned based on the Gender 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)

This will generate a scatter plot where the Gender column determines the color of each data point.

Release Statement This article is reprinted at: 1729320437 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