0

I have a list of lists, and each list has three items. The first two are numbers, those are for the x and y axis. The third one is either True or False. I need to plot those numbers, which have True next to them with blue (which is the basic color for Python), and those with False value with red. How can I make the last one happen?

So far I plotted everything with blue, I do not know how to plot certain values with red.

Vikih
  • 9
  • 2
  • 1
    Does this answer your question? [Color by Column Values in Matplotlib](https://stackoverflow.com/questions/14885895/color-by-column-values-in-matplotlib) – Redox Mar 15 '23 at 09:01
  • Or use seaborn - `seaborn.scatterplot(data=df, x='xcol', y='ycol', hue='TruFalseCol')` – Redox Mar 15 '23 at 09:02

1 Answers1

1

You could do:

import numpy as np

import matplotlib.pyplot as plt

data = [
    [1, 2, True],
    [4, 5, False],
    [7, 9, True],
    [12, 13, True],
    [15, 3, False],
    [18, 4, False],
    [20, 11, True]
]

# convert to numpy array
npdata = np.asarray(data)

fig, ax = plt.subplots()

# get True indices
trueidx = np.argwhere(npdata[:,2] == True).squeeze()

# get False indices
falseidx = np.argwhere(npdata[:,2] == False).squeeze()

ax.plot(npdata[trueidx, 0], npdata[trueidx, 1], "b", label="True")
ax.plot(npdata[falseidx, 0], npdata[falseidx, 1], "r", label="False")
ax.legend()

fig.show()

This gives:

enter image description here

If you just want to plot the points as dot rather than joined with lines, change "r" to "ro" in the plot command, and similarly for "b" to "bo".

Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32