仅显示最后一个子图(其他为空白),这是错误的图

时间:2019-07-07 23:31:45

标签: python-2.7 matplotlib subplot

我在matplotlib中设置子图时遇到麻烦。以下是我尝试实现的布局的屏幕截图。我只经历了ax4,因为遇到了两个问题。代码也在下面。 (请注意,函数按预期进行绘制。)我想继续使用“ subplot2grid”而不是其他选项,并且我正在使用Python 2.7

预期的行为: 绘图ax1,ax2和ax3应该是世界阴影浮雕贴图,其位置如下图所示。 绘图ax4应该是散点图,其位置如下图所示。

实际行为: 绘图AX1,AX2和AX3均为空白 绘图ax4并不是散点图,但实际上是地图。布局也是错误的。

我以为我缺少“保留”类型的功能,但看起来像that's not how matplotlib works。 我还确保我defined the plot limits在myplotA函数中。

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
from mpl_toolkits.basemap import Basemap
from random import randint

def myplotA(plotnum, title):
    south = 34.0129656032
    north = 34.721878622
    west = -116.7615176
    east = -116.336918412
    center = [(east + west) / 2, (north + south) / 2]
    m = Basemap(llcrnrlon=west, llcrnrlat=south, urcrnrlon=east, urcrnrlat=north, resolution='c', epsg=4326, lon_0=center[0], lat_0=center[1], suppress_ticks=False)
    img = m.arcgisimage(service="World_Shaded_Relief", xpixels=2000)
    img.set_alpha(0.5)
    plt.xticks(rotation='horizontal')
    plotnum.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    plotnum.axis([west, east, south, north])
    for label in (plotnum.get_xticklabels() + plotnum.get_yticklabels()):label.set_fontsize(9)
    plt.gca().set_title(title, fontsize=12)

def myplotB(plotnum, title):
    x = [randint(0, 10) for i in range(0, 6)]
    y = [randint(0, 10) for i in range(0, 6)]
    plotnum.scatter(x, y, s=4)
    plotnum.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    plt.xlabel('xlabel', fontsize=8)
    plt.ylabel('ylabel', fontsize=8)
    plt.gca().set_title(title, fontsize=12)



fig = plt.figure(figsize=(11, 17))
ax1 = plt.subplot2grid((8, 3), (0, 0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((8, 3), (0, 1), rowspan=4, colspan=1)
ax3 = plt.subplot2grid((8, 3), (0, 2), rowspan=4, colspan=1)
ax4 = plt.subplot2grid((8, 3), (5, 0), rowspan=1, colspan=2)
myplotA(ax1, 'ax1')
myplotA(ax2, 'ax2')
myplotA(ax3, 'ax3')
myplotB(ax4, 'ax4')
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=.1, hspace=.1)
fig.savefig(outpath + '\\' + 'mytest.pdf')

所需的版式 Desired Layout Image

实际结果 Actual Result Image

2 个答案:

答案 0 :(得分:0)

您将轴传递给绘图功能,但是在其中您需要实际 使用 。其他所有plt命令将应用于当前活动的轴,这是您创建的最后一个轴。

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
from mpl_toolkits.basemap import Basemap
from random import randint

def myplotA(ax, title):
    south = 34.0129656032
    north = 34.721878622
    west = -116.7615176
    east = -116.336918412
    center = [(east + west) / 2, (north + south) / 2]
    m = Basemap(llcrnrlon=west, llcrnrlat=south, urcrnrlon=east, urcrnrlat=north, 
                resolution='c', epsg=4326, lon_0=center[0], lat_0=center[1], 
                suppress_ticks=False, ax=ax)
    img = m.arcgisimage(service="World_Shaded_Relief", xpixels=2000)
    img.set_alpha(0.5)
    plt.setp(ax.get_xticklabels(), rotation='horizontal')
    plt.setp(ax.get_xticklabels() + ax.get_yticklabels(), fontsize=9)
    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.axis([west, east, south, north])
    ax.set_title(title, fontsize=12)

def myplotB(ax, title):
    x = [randint(0, 10) for i in range(0, 6)]
    y = [randint(0, 10) for i in range(0, 6)]
    ax.scatter(x, y, s=4)
    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.set_xlabel('xlabel', fontsize=8)
    ax.set_ylabel('ylabel', fontsize=8)
    ax.set_title(title, fontsize=12)



fig = plt.figure(figsize=(11, 8))
ax1 = plt.subplot2grid((8, 3), (0, 0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((8, 3), (0, 1), rowspan=4, colspan=1)
ax3 = plt.subplot2grid((8, 3), (0, 2), rowspan=4, colspan=1)
ax4 = plt.subplot2grid((8, 3), (5, 0), rowspan=1, colspan=2)
myplotA(ax1, 'ax1')
myplotA(ax2, 'ax2')
myplotA(ax3, 'ax3')
myplotB(ax4, 'ax4')
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=.1, hspace=.1)
fig.savefig('mytest.pdf')
plt.show()

enter image description here

答案 1 :(得分:0)

感谢@ImportanceOfBeingErnest

我在上面尝试了您的代码,但仍然与OP的“实际结果”部分所示的图相同。但是,您的话描述了这个问题:我正在激活轴,然后在绘制之前激活下一个轴。下面是窍门-也许这就是您所拥有的,只是错误地复制/粘贴了原始代码。感谢您指出正确的方向。

fig = plt.figure(figsize=(11, 17))
ax1 = plt.subplot2grid((8, 3), (0, 0), rowspan=4, colspan=1)
myplotA(ax1, 'ax1')
ax2 = plt.subplot2grid((8, 3), (0, 1), rowspan=4, colspan=1)
myplotA(ax2, 'ax2')
ax3 = plt.subplot2grid((8, 3), (0, 2), rowspan=4, colspan=1)
myplotA(ax3, 'ax3')
ax4 = plt.subplot2grid((8, 3), (4, 0), rowspan=1, colspan=2)
myplotB(ax4, 'ax4')
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=.1, hspace=.1)
fig.savefig(outpath + '\\' + 'mytest.pdf')

Link to image of output plot; looks as expected