简体   繁体   English

在 matplotlib 上绘制 numpy 数组

[英]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 .这给了我一个从x-axis = 0开始的 2 条线图。

How can I concatenate 'a' and 'b' and plot the graph such that 'b' continues on the x-axis where 'a' ended?如何连接'a''b'和 plot 图形,使'b'x-axis继续'a'结束?

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.您可以添加一个x数组,然后在下一个 plot 中增加其值,以便将其附加到前一个 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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