我的python代码有问题。我想在一张图片上制作一张多图,但是顶行有2张图,底线只有1张,但是底图应该更宽(两个顶部抓图的宽度)。这是我的代码:
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import AutoMinorLocator
import numpy as np
import os
from matplotlib.image import NonUniformImage
from matplotlib.ticker import NullFormatter
plt.figure(1)
#1
plt.subplot(221)
plt.title('linear')
#2
plt.subplot(222)
plt.title('log')
#3
plt.subplot(224)
plt.title('symlog')
plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.3,
wspace=0.35)
plt.show()
如果我更改第三个图表,例如plt.subplot(223)
它只是从左向右移动。如何将其扩大并放在两个地方?
答案 0 :(得分:1)
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import AutoMinorLocator
import numpy as np
import os
from matplotlib.image import NonUniformImage
from matplotlib.ticker import NullFormatter
plt.figure(1)
#1
plt.subplot2grid((2,2), (0,0))
plt.title('linear')
#2
plt.subplot2grid((2,2), (0,1))
plt.title('log')
#3
plt.subplot2grid((2,2), (1,0), colspan = 2)
plt.title('symlog')
plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.3,
wspace=0.35)
plt.show()
答案 1 :(得分:1)
您可以使用GridSpec:
对其进行自定义from matplotlib.gridspec import GridSpec
gs=GridSpec(2,2)
#1
plt.subplot(gs[0,0])
plt.title('linear')
#2
plt.subplot(gs[0,1])
plt.title('log')
#3
plt.subplot(gs[1,:])
plt.title('symlog')