0

I would like to plot as lines and also as points a list of tuples so I have

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

lista=[(0,1,3),(1,4,5),(2,0,2),(3,5,10),(4,3,7)]
#lista=[(0,1),(1,4),(2,0),(3,5),(4,3)]

plt.scatter(*zip(*lista))
#plt.plot(*zip(*lista))  #<--- this works
plt.show()

I followed the advice on this answer

However, when I plot as a line it works but when I do as scatter, it shows only the first values (0,1) and not the second value (0,3)

How can the scatter also work to plot two series of data

Note: The first serie being the first and second value in the tuple and the second being the first and third in the tuple

This works works

This does not work

donotwork

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

0

The easiest way is to split your list into two datasets and plot them individually

import matplotlib.pyplot as plt

lista = [(0, 1, 3), (1, 4, 5), (2, 0, 2), (3, 5, 10), (4, 3, 7)]
xes = [t[0] for t in lista]
data_a = [t[1] for t in lista]
data_b = [t[2] for t in lista]

plt.scatter(xes, data_a, s=10, c='green', marker='s', label='Set A')
plt.scatter(xes, data_b, s=10, c='orange', marker='o', label='Set B')
plt.legend()
plt.show()

enter image description here

See documentation for further input variants of scatter.

Cpt.Hook
  • 590
  • 13