kiran.gilvaz
kiran.gilvaz

Reputation: 7767

Plotting a numpy array on matplotlib

I have the following code:

import matplotlib.pyplot as plt
import numpy as np
a=np.array([[0],[1],[2]], np.int32)
b=np.array([[3],[4],[5]], np.int32)

plt.plot(a, color = 'red', label = 'Historical data')
plt.plot(b, color = 'blue', label='Predicted data')
plt.legend()
plt.show()

That gives me a graph of 2 lines each starting from x-axis = 0.

How can I concatenate 'a' and 'b' and plot the graph such that 'b' continues on the x-axis where 'a' ended?

Thanks!

Upvotes: 0

Views: 15822

Answers (1)

techytushar
techytushar

Reputation: 803

You can add an x array and then increase its value in the next plot so it will get appended to the previous plot.

import matplotlib.pyplot as plt
import numpy as np
a=np.array([[0],[1],[2]], np.int32)
b=np.array([[3],[4],[5]], np.int32)
x = np.arange(a.shape[0])

plt.plot(x, a, color = 'red', label = 'Historical data')
plt.plot(x+a.shape[0], b, color = 'blue', label='Predicted data')
plt.legend()
plt.show()

Upvotes: 2

Related Questions