第一个箱线图丢失或不可见
First boxplot missing or not visible
为什么第一个箱线图不见了? 本来应该有 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' )
你的箱线图在那里,但它被隐藏了。这重现了您的问题:
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()
为什么第一个箱线图不见了? 本来应该有 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' )
你的箱线图在那里,但它被隐藏了。这重现了您的问题:
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()