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