Home >Backend Development >Python Tutorial >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?

Susan Sarandon
Susan SarandonOriginal
2024-10-19 14:49:02800browse

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:

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

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:

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

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

The above is the detailed content of How to Assign Colors to Points in Scatter Plots Based on Column Values in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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