0

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!

    1 Answer 1

    2

    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() 
    2
    • Follow up question: What if the numpy arrays are: a=np.array([[0],[1],[2]], np.int32) b=np.array([[2],[3],[4],[5]], np.int32) ? I get this error: ValueError: x and y must have same first dimension, but have shapes (3,) and (4, 1) How can i get around this?CommentedMay 12, 2020 at 6:41
    • Since the shape of a and b are different here you'll have to make two different x variable x1 = np.arange(a.shape[0]) and x2 = np.arange(a.shape[0], a.shape[0]+b.shape[0]). x1 can be used with a and x2 can be used with bCommentedMay 13, 2020 at 5:04

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.