一张图片上的多个图表(python)

时间:2017-09-27 07:17:44

标签: python python-2.7 matplotlib graph

我的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)它只是从左向右移动。如何将其扩大并放在两个地方?

2 个答案:

答案 0 :(得分:1)

使用subplot2grid()

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

enter image description here

答案 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')