Wie verwende ich Matplotlib, um unterschiedliche Farben für verschiedene kategoriale Ebenen darzustellen?

DDD
Freigeben: 2024-10-17 16:40:03
Original
324 Leute haben es durchsucht

How Do I Use Matplotlib to Plot Distinct Colors for Various Categorical Levels?

How to Plot Different Colors for Different Categorical Levels in Python Using Only Matplotlib

Introduction

This article addresses how to create a scatter plot in Python using matplotlib, where each color represents a different categorical level. This approach avoids using auxiliary plotting packages like seaborn and ggplot for Python.

With Matplotlib

Matplotlib provides the c argument in plt.scatter, which allows color customization. Here's an example:

<code class="python">import matplotlib.pyplot as plt
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({'carat': [0.23, 0.21, 0.23],
                    'price': [326, 326, 327],
                    'color': ['E', 'E', 'E']})

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

# Scatter plot with colors
plt.scatter(df['carat'], df['price'], c=df['color'].map(colors))
plt.show()</code>
Nach dem Login kopieren

The map(colors) function maps the "diamond" colors to the "plotting" colors.

With seaborn

Although this article focuses on matplotlib, it's worth mentioning that seaborn also offers a convenient solution:

<code class="python">import seaborn as sns

# Scatter plot with colors
sns.lmplot(x='carat', y='price', data=df, hue='color', fit_reg=False)</code>
Nach dem Login kopieren

With pandas.DataFrame.groupby & pandas.DataFrame.plot

For a manual approach, you can use pandas to group by color and plot each group separately:

<code class="python">import matplotlib.pyplot as plt
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({'carat': [0.23, 0.21, 0.23],
                    'price': [326, 326, 327],
                    'color': ['E', 'E', 'E']})

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

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

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

This assumes the same DataFrame as before and manually assigns colors during the plotting process.

Conclusion

This article has demonstrated how to plot different colors for different categorical levels in Python using matplotlib, along with additional options using seaborn and a manual approach with pandas.

Das obige ist der detaillierte Inhalt vonWie verwende ich Matplotlib, um unterschiedliche Farben für verschiedene kategoriale Ebenen darzustellen?. 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
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!