Python 在matplotlib中执行多个子绘图时,轴上会显示额外的数字

Python 在matplotlib中执行多个子绘图时,轴上会显示额外的数字,python,matplotlib,pandas,Python,Matplotlib,Pandas,大家好,这里是第一个python问题 所以大画面,我有一个面板,其中有4个数据帧。我想要一个函数,只需将面板绘制成4个子图,并在其上进行少量数据处理 当我尝试设置子地块时,我在子地块2和更高的两个轴上都会得到奇怪的数字。我不知道它们来自哪里,简单明了,似乎没有人有过同样的问题。朋友们,有什么建议吗 def plotAlexaPanel(getWebsiteListStatisticsOutput, selection = 'Aggregate', defaultlookback = -200):

大家好,这里是第一个python问题

所以大画面,我有一个面板,其中有4个数据帧。我想要一个函数,只需将面板绘制成4个子图,并在其上进行少量数据处理

当我尝试设置子地块时,我在子地块2和更高的两个轴上都会得到奇怪的数字。我不知道它们来自哪里,简单明了,似乎没有人有过同样的问题。朋友们,有什么建议吗

def plotAlexaPanel(getWebsiteListStatisticsOutput, selection = 'Aggregate', defaultlookback = -200):
    '''nicely formatted plots from getWebsiteListStatistics'''
    fig, axes = plt.subplots(2, 2) #, sharex=True)
    fig.tight_layout()
    ax1 = fig.add_subplot(2, 2, 1)
    plotAlexaPanelSubPlot(ax1, "viewsPerU",getWebsiteListStatisticsOutput, selection, defaultlookback)
    ax2 = fig.add_subplot(2, 2, 2)
    plotAlexaPanelSubPlot(ax2, "viewsPerM",getWebsiteListStatisticsOutput, selection, defaultlookback)
    ax3 = fig.add_subplot(2, 2, 3)
    plotAlexaPanelSubPlot(ax3, "reachPerM",getWebsiteListStatisticsOutput, selection, defaultlookback)
    ax4 = fig.add_subplot(2, 2, 4)
    plotAlexaPanelSubPlot(ax4, "pageRank",getWebsiteListStatisticsOutput, selection, defaultlookback)
    #plt.subplots_adjust(wspace=0, hspace=0)
    suptitle(selection, fontsize=14, fontweight='bold')
    fig.set_size_inches(18.5,10.5)
    return

def plotAlexaPanelSubPlot(subplotaxis, subplotmetric,getWebsiteListStatisticsOutput, selection = 'Aggregate', defaultlookback = -200):
    if selection == 'Aggregate':
        if subplotmetric == 'pageRank':
            subplotmetric2 = 'minRank'
        elif subplotmetric == 'viewsPerU':
            subplotmetric2 = 'aggregateViewsU'
        elif subplotmetric == 'viewsPerM':
            subplotmetric2 = 'aggregateViews'
        elif subplotmetric == 'reachPerM':
            subplotmetric2 = 'aggregateReach'
        else:
            subplotmetric2 = subplotmetric
        getWebsiteListStatisticsOutput['aggregate'][subplotmetric2][defaultlookback:].plot(legend=False,ax=subplotaxis)
        yoygrowth(getWebsiteListStatisticsOutput['aggregate'][subplotmetric2])[defaultlookback:].plot(legend=False,ax=subplotaxis, secondary_y=True)
    else:
        getWebsiteListStatisticsOutput['rawOutput'][subplotmetric][selection][defaultlookback:].plot(legend=False,ax=subplotaxis)
        yoygrowth(getWebsiteListStatisticsOutput['rawOutput'][subplotmetric][selection])[defaultlookback:].plot(legend=False,ax=subplotaxis, secondary_y=True)
    subplotaxis.set_ylabel(subplotmetric +  ' (Blue)')
    subplotaxis.right_ax.set_ylabel('YoY% (Green)')
    subplotaxis.set_xlabel('')
    return

问题在于,
plt.subplot(2,2)
调用向图形中添加四个AxesSubplot对象,
fig.add_subplot()
调用每个对象再添加一个*。您正在将数据绘制到第二组轴上,这就是为什么rogue记号标签的跨度为0到1。当您不将任何数据打印到Axis实例时,这是默认图形

解决此问题的最简单方法是替换

fig, axes = plt.subplots(2,2)

这将停止添加未使用的轴


*嗯,有点。在幕后,
fig.add_subplot()
调用检查该图是否已经有了带有此调用签名的子图,如果有,则返回该子图,而不是创建新的子图。这是第一个子批的情况,但接下来的三个子批的调用签名略有不同,可能与
sharex
/
sharey
参数有关,因此会为它们创建新的子批。

最好执行
fig,[[ax1,ax2],[ax3,ax4]=plt.子批(2,2)
。谢谢Deditos!很明显,我在复制和粘贴东西的时候并没有真正了解幕后的情况:)学习的痛苦。
fig = plt.figure()