我试图将两个子图并排展示(而不是垂直堆叠)。我期望看到[sp1] [sp2]
但实际上只有第二个图[sp2]被展示。
from matplotlib import pyplot
x = [0, 1, 2]
pyplot.figure()
# sp1
pyplot.subplot(211)
pyplot.bar(x, x)
# sp2
pyplot.subplot(221)
pyplot.plot(x, x)
pyplot.show()
我试图将两个子图并排展示(而不是垂直堆叠)。我期望看到[sp1] [sp2]
但实际上只有第二个图[sp2]被展示。
from matplotlib import pyplot
x = [0, 1, 2]
pyplot.figure()
# sp1
pyplot.subplot(211)
pyplot.bar(x, x)
# sp2
pyplot.subplot(221)
pyplot.plot(x, x)
pyplot.show()
这三个数字指的是行数、列数和图表编号。你在第二次调用subplot时重新指定了列数,这改变了配置并导致pyplot重新开始。
你想表达的意思是:
subplot(121) # 1 row, 2 columns, Plot 1
...
subplot(122) # 1 row, 2 columns, Plot 2
from matplotlib import pyplot
x = [0, 1, 2]
pyplot.figure()
# sp1
pyplot.subplot(121)
pyplot.bar(x, x)
# sp2
pyplot.subplot(122)
pyplot.plot(x, x)
pyplot.show()