1

I have two datasets

firstX = [0, 1, 2, 3, 4, 5, 6] # X Axis 
firstY = [10, 10, 20, 30, 40, 60, 70] # Y Axis
secondX = [9, 10, 11, 12, 13, 14, 15] # X Axis
secondY = [40, 20, 60, 11, 77, 12, 54] # Y Axis

I want to plot these two datasets in the same chart but without connecting them together. As you can see, there is a disconnection between them (in X axis, 7 and 8 are missing). When I concat them, matplotlib will try to connect the last point of the first dataset (6, 70) with the first point of the second dataset (9, 40). I would like to know how to avoid this behavior

Pryda
  • 899
  • 5
  • 14
  • 37
  • 1
    Related: https://stackoverflow.com/q/38178587/1025391 and https://stackoverflow.com/q/18430921/1025391 – moooeeeep Mar 23 '20 at 10:02

3 Answers3

2

Instead of concatenating the datasets, you can call the plot command two times, plotting two times to the same axes:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(firstX, firstY)
ax.plot(secondX, secondY)
Arco Bast
  • 3,595
  • 2
  • 26
  • 53
2

You can just plot them individually. If they're sublists of a list, e.g. X = [[X1], [X2]], Y = [[Y1], [Y2]], you can loop over them.

import matplotlib.pyplot as plt

fig = plt.figure()
for i in range(len(X)):
    plt.plot(X[i], Y[i])
plt.show()
gtancev
  • 243
  • 1
  • 10
1

From what I understand your question, this should work:

import matplotlib.pyplot as plt

plt.figure()
plt.plot(firstX, firstY, c='b')
plt.plot(secondX, secondY, c='b')
plt.show
wuiwuiwui
  • 499
  • 4
  • 13