Wie erstelle ich ein Streudiagramm mit verschiedenen Farben für kategoriale Ebenen in Matplotlib, Seaborn und Pandas?

Barbara Streisand
Freigeben: 2024-10-17 16:34:02
Original
324 Leute haben es durchsucht

How to Create a Scatter Plot with Different Colors for Categorical Levels in Matplotlib, Seaborn, and Pandas?

Scatter Plot with Different Colors for Categorical Levels

Using Matplotlib

To create a scatter plot where different categorical levels are represented by different colors using Matplotlib, follow these steps:

  1. Import Matplotlib and the data frame you want to plot.
  2. Define a dictionary that maps the categorical levels to plotting colors.
  3. Use plt.scatter, passing in the x and y values and the c argument to specify the colors.
<code class="python">import matplotlib.pyplot as plt
import pandas as pd

colors = {'D':'tab:blue', 'E':'tab:orange', 'F':'tab:green', 'G':'tab:red', 'H':'tab:purple', 'I':'tab:brown', 'J':'tab:pink'}

df.scatter(df['carat'], df['price'], c=df['color'].map(colors))

plt.show()</code>
Nach dem Login kopieren

Using Seaborn

Seaborn is a wrapper around Matplotlib that provides a more user-friendly interface. To create a scatter plot with different colors for categorical levels using Seaborn, follow these steps:

  1. Import Seaborn and the data frame you want to plot.
  2. Use seaborn.scatterplot, passing in the x and y values and the hue parameter to specify the categorical level.
<code class="python">import seaborn as sns

sns.scatterplot(x='carat', y='price', data=df, hue='color')

plt.show()</code>
Nach dem Login kopieren

Using pandas.groupby & pandas.DataFrame.plot

You can also use pandas.groupby and pandas.DataFrame.plot to create a scatter plot with different colors for categorical levels. This method requires more manual work, but it gives you more control over the plot's appearance.

  1. Import pandas and the data frame you want to plot.
  2. Group the data frame by the categorical level.
  3. Iterate over the groups and plot each one with a different color.
<code class="python">import pandas as pd

fig, ax = plt.subplots(figsize=(6, 6))

grouped = df.groupby('color')
for key, group in grouped:
    group.plot(ax=ax, kind='scatter', x='carat', y='price', label=key, color=colors[key])

plt.show()</code>
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonWie erstelle ich ein Streudiagramm mit verschiedenen Farben für kategoriale Ebenen in Matplotlib, Seaborn und Pandas?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!