matplotlib如何在同一张图上显示多个子图

it2022-05-05  231

一、

这个不是在一张折线图上显示多个折线,是一个图上放许多子图。

避免你搞不明白我在说什么,先放图。

plt.figure() plt.subplot(221) plt.plot([1,2,3],[2,2,2]) plt.subplot(222) plt.plot([1, 2, 3], [3,3,3]) plt.subplot(223) plt.plot([1, 2, 3], [4,4,4]) plt.subplot(224) plt.plot([1, 2, 3], [5,5,5]) plt.show()

subplot的参数221意思就是,把整个大的画板分成2*2,这个子图在第1个位置。

因为分成了2*2,那么还可以有222,223,224.

或者你想分成3*3,那么可以有331,332,333,……

教程在这儿:https://morvanzhou.github.io/tutorials/data-manipulation/plt/4-1-subpot1/

如果你想在其中一个话多个折线,那么还是用subplot来控制,让他们的subplot的参数一样就行了。

plt.figure() plt.subplot(221) plt.plot([1,2,3],[2,2,2]) plt.subplot(222) plt.plot([1, 2, 3], [3,3,3]) # 这样第二个子图就可由两条线在同一图上了 plt.plot([1,2,3],[3,3,3]) # 这样可以在第二个子图上有标题 plt.title(" ") plt.subplot(223) plt.plot([1, 2, 3], [4,4,4]) plt.subplot(224) plt.plot([1, 2, 3], [5,5,5]) plt.show()


最新回复(0)