Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Color a Scatter Plot by Category using Matplotlib in Python

#1
How to Color a Scatter Plot by Category using Matplotlib in Python

Problem Formulation


Given three arrays:

  • The first two arrays x and y of length n contain the (x_i, y_i) data of a 2D coordinate system.
  • The third array c provides categorical label information so we essentially get n data bundles (x_i, y_i, c_i) for an arbitrary number of categories c_i.

? Question: How to plot the data so that (x_i, y_i) and (x_j, y_j) with the same category c_i == c_j have the same color?

Solution: Use Pandas groupby() and Call plt.plot() Separately for Each Group


To plot data by category, you iterate over all groups separately by using the data.groupby() operation. For each group, you execute the plt.plot() operation to plot only the data in the group.

In particular, you perform the following steps:

  1. Use the data.groupby("Category") function assuming that data is a Pandas DataFrame containing the x, y, and category columns for n data points (rows).
  2. Iterate over all (name, group) tuples in the grouping operation result obtained from step one.
  3. Use plt.plot(group["X"], group["Y"], marker="o", linestyle="", label=name) to plot each group separately using the x, y data and name as a label.

Here’s what that looks like in code:

import pandas as pd
import matplotlib.pyplot as plt # Generate the categorical data
x = [1, 2, 3, 4, 5, 6]
y = [42, 41, 40, 39, 38, 37]
c = ['a', 'b', 'a', 'b', 'b', 'a'] data = pd.DataFrame({"X": x, "Y": y, "Category": c})
print(data) # Plot data by category
groups = data.groupby("Category")
for name, group in groups: plt.plot(group["X"], group["Y"], marker="o", linestyle="", label=name) plt.legend()
plt.show()

Before I show you how the resulting plot looks, allow me to show you the data output from the print() function. Here’s the output of the categorical data:

 X Y Category
0 1 42 a
1 2 41 b
2 3 40 a
3 4 39 b
4 5 38 b
5 6 37 a

Now, how does the colored category plot look like? Here’s how:


If you want to learn more about Matplotlib, feel free to check out our full blog tutorial series:



https://www.sickgaming.net/blog/2022/04/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python ? Put Legend Outside Plot ? – Easy Guide xSicKxBot 0 1,460 04-22-2023, 11:08 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable xSicKxBot 0 1,302 09-05-2022, 09:33 AM
Last Post: xSicKxBot
  [Tut] Is There A List Of Line Styles In Matplotlib? xSicKxBot 0 1,190 03-28-2022, 07:29 AM
Last Post: xSicKxBot
  [Tut] Matplotlib Scatter Plot – Simple Illustrated Guide xSicKxBot 0 1,099 11-24-2020, 10:23 PM
Last Post: xSicKxBot
  [Tut] Matplotlib Animation – A Helpful Illustrated Guide xSicKxBot 0 1,371 05-13-2020, 09:25 PM
Last Post: xSicKxBot
  [Tut] Matplotlib 3D Plot Advanced xSicKxBot 0 1,425 04-20-2020, 08:42 PM
Last Post: xSicKxBot
  [Tut] Matplotlib 3D Plot – A Helpful Illustrated Guide xSicKxBot 0 1,487 04-03-2020, 04:47 PM
Last Post: xSicKxBot
  [Tut] Matplotlib Boxplot – A Helpful Illustrated Guide xSicKxBot 0 1,431 03-25-2020, 07:22 PM
Last Post: xSicKxBot
  [Tut] Matplotlib Legend – A Helpful Illustrated Guide xSicKxBot 0 1,533 03-17-2020, 01:33 PM
Last Post: xSicKxBot
  [Tut] Matplotlib Imshow — A Helpful Illustrated Guide xSicKxBot 0 1,835 03-03-2020, 03:30 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016