【问题标题】:First boxplot missing or not visible第一个箱线图丢失或不可见
【发布时间】:2015-09-29 01:33:45
【问题描述】:

为什么缺少第一个箱线图? 应该有 24 个箱线图,但如图所示,只显示了 23 个箱线图。 我必须更改谁的尺寸才能使其可见?我已尝试更改图形的尺寸,但结果相同。

不确定是否有帮助,但这是代码:

def obtenerBoxplotsAnualesIntercalados(self, directorioEntrada, directorioSalida):
    meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio", "Julio", "Agosto","Septie.","Octubre","Noviem.","Diciem."]
    ciudades = ["CO","CR"]      
    anios = ["2011", "2012", "2013"]
    for anio in anios:
        fig = plt.figure()
        fig.set_size_inches(14.3, 9)
        ax = plt.axes()
        plt.hold(True)
        bpArray = []
        i=0
        ticks = []
        for mes in range(len(meses)):
            archivoCO = open(directorioEntrada+"/"+"CO"+"-"+self.mesStr(mes+1)+"-"+anio, encoding = "ISO-8859-1")
            archivoCR = open(directorioEntrada+"/"+"CR"+"-"+self.mesStr(mes+1)+"-"+anio, encoding = "ISO-8859-1")
            datosCOmes = self.obtenerDatosmensuales(archivoCO)
            datosCRmes = self.obtenerDatosmensuales(archivoCR)
            data = [    [int(float(datosCOmes[2])), int(float(datosCOmes[0])), int(float(datosCOmes[1]))],
                        [int(float(datosCRmes[2])), int(float(datosCRmes[0])), int(float(datosCRmes[1]))] ]
            bpArray.append(plt.boxplot(data, positions=[i,i+1], widths=0.5))
            ticks.append(i+0.5)
            i=i+2
        hB, = plt.plot([1,1],'b-')
        hR, = plt.plot([1,1],'r-')
        plt.legend((hB, hR),('Caleta', 'Comodoro'))
        hB.set_visible(False)
        hR.set_visible(False)
        ax.set_xticklabels(meses)
        ax.set_xticks(ticks)
        self.setBoxColors(bpArray)
        plt.title('Variación de temperatura mensual Caleta Olivia-Comodoro Rivadavia. Año '+anio)
        plt.savefig(directorioSalida+"/asdasd"+str(anio)+".ps", orientation='landscape', papertype='A4' )

【问题讨论】:

    标签: python python-3.x matplotlib boxplot


    【解决方案1】:

    你的箱线图在那里,但它被隐藏了。这重现了您的问题:

    import matplotlib
    import numpy as np
    
    data = np.random.normal(10,2,100*24).reshape(24,-1) # let's get 12 pairs of arrays to plot
    meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio", "Julio", "Agosto","Septie.","Octubre","Noviem.","Diciem."]
    
    ax = plt.axes()
    plt.hold(True)
    
    i=0
    
    ticks = []
    
    for mes in range(0,len(meses)):
        plt.boxplot(data, positions=[i,i+1], widths=0.5)
        ticks.append(i+0.5)
        i+=2
    
    ax.set_xticklabels(meses)
    ax.set_xticks(ticks)    
    
    plt.show()
    

    请注意,您将位置定义为从 0 到 12,但您将 ticks 附加为 range(0,12) + 0.5。因此,当您稍后执行set_xticks(ticks) 时,您的 x 轴将从 0.5 开始,但您的第一个箱线图绘制在位置 0。

    我已经稍微修改了您的代码以产生您想要的结果:

    ax = plt.axes()
    plt.hold(True)
    
    i=1 # we start plotting from position 1 now
    
    ticks = []
    
    for mes in range(0,len(meses)):
        plt.boxplot(data, positions=[i,i+1], widths=0.5)
        ticks.append(i+0.5)
        i+=2
    
    ax.set_xticklabels(meses)
    ax.set_xlim(0,ticks[-1]+1) # need to shift the right end of the x limit by 1
    ax.set_xticks(ticks)    
    
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多