[Matplotlib-users] Add disconnected lines to a plot

Jerzy Karczmarczuk jerzy.karczmarczuk at unicaen.fr
Sat May 2 19:34:03 EDT 2020


Le 03/05/2020 à 01:08, EK Esawi via Matplotlib-users a écrit :
> I am new to Matplotlib. I created a plot and I want to add a set of straight lines from a set of (x,y) coordinates (2D list). I want to have lines drown from 1st to 2nd then from 3rd to 4th and then from 5th to 6th, etc. I don’t want lines between 2nd and 3rd, 4th and 5th, etc. I think it can be done using Path and Patch, but could not get it done. I even tried to rearrange my data in a way to draw one line piece with zigzag shape but that too did not work well.

It suffices to remember that a NAN breaks a plot line. Insert NANs 
between the segments

If you try this:

*import numpy as np
import matplotlib.pyplot as plt

x=np.array(range(20))
x=x.reshape((10,2))  # two columns
h=np.array([10*[np.nan]]).T
xx=np.hstack((x,h)).reshape((30,)) # NANs form the third column; Flatten 
the result

plt.plot(xx,xx,'r',lw=2)*

you should get:

Jerzy Karczmarczuk

/Caen, France/



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20200503/ae4dec4d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pkfifgkcalndjhbp.png
Type: image/png
Size: 5970 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20200503/ae4dec4d/attachment.png>


More information about the Matplotlib-users mailing list