简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM