Daniel Contreras
Daniel Contreras

Reputation: 61

How to avoid joining two points with a line matplotlib?

I am working with matplotlib and want to plot a list of points, for example x=[1,2,3,4] and y=[1,4,6,8]. When I used plt.plot(x,y) I got a plot with lines joining the points in my graph. My problem is that I only want to be plotted some of these lines, for example the line joining (1,1) with (2,4) and the one joining (3,6) with (4,8), but ommiting the line between (2,4) and (3,6). I do not want to have two different lines, because my actual arrays are bigger and do not want to make a line for each pair of points. Is there a way I can tell matplotlib which points to join?

import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[1,4,6,8]
plt.plot(x,y)

Upvotes: 1

Views: 1410

Answers (1)

cfort
cfort

Reputation: 2776

Insert NaNs where you want the blanks:

x=[1,2,np.nan,3,4]
y=[1,4,np.nan,6,8]
plt.plot(x,y)

enter image description here

Upvotes: 1

Related Questions