Pryda
Pryda

Reputation: 1029

Keep the gap between two datasets in matplotlib

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

Upvotes: 1

Views: 181

Answers (3)

gtancev
gtancev

Reputation: 253

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()

Upvotes: 2

wuiwuiwui
wuiwuiwui

Reputation: 547

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

Upvotes: 1

Arco Bast
Arco Bast

Reputation: 3892

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)

Upvotes: 2

Related Questions