Home > Blockchain >  Matplotlib in python | connecting dots
Matplotlib in python | connecting dots

Time:10-09

What is the possible way to connect dots I needed ?

For example, my code:

x = [0, 5, 10, 15, 100, 105, 110, 115]
y = [15, 10, 5, 0, 115, 110, 105, 100]
plt.figure(figsize=(5, 5))

plt.xlim(-1, 120)
plt.ylim(-1, 120)
plt.grid()      

plt.plot(x, y, 'og-')

Have this: Screen

But I need connect only dots from 0-15 and 100-115 (for x) , so I do not need connection between for dots: (15,0) and (100, 115)enter image description here

What is solutions for me ?

CodePudding user response:

If you have a very long x-array, you can combine numpy's drawing a line in pieces

CodePudding user response:

draw the lines you want, and don't draw the ones you don't:

from matplotlib import pyplot as plt
#x = [0, 5, 10, 15, 100, 105, 110, 115]
#y = [15, 10, 5, 0, 115, 110, 105, 100]

plt.figure(figsize=(5, 5))

plt.xlim(-1, 120)
plt.ylim(-1, 120)
plt.grid()   

x1 = [0, 5, 10, 15]
y1 = [15, 10, 5, 0]

x2 = [100, 105, 110, 115]
y2 = [115, 110, 105, 100]


plt.plot(x1, y1, 'og-')
plt.plot(x2,y2, 'og-')

plt.show()

Output:

CodePudding user response:

Do 2 plots, one with the first set of points and one with the second set.

import matplotlib.pyplot as plt

x = [0, 5, 10, 15, 100, 105, 110, 115]
y = [15, 10, 5, 0, 115, 110, 105, 100]
plt.figure(figsize=(5, 5))

plt.xlim(-1, 120)
plt.ylim(-1, 120)
plt.grid()      

plt.plot(x[:4], y[:4], 'og-')
plt.plot(x[4:], y[4:], 'og-')
plt.show()
  • Related