我如何制作情节的图例,向我显示我正在处理的每个变量?

问题描述 投票:0回答:1

我正在制作覆盖图,但是当我添加图例并且图例显示在图例中时,它只显示一天,并重复了几次,就像这样

imagen = plt.figure(figsize=(25,10))

for day in [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30]:
    dia = datos[datos['Fecha'] == "2019-06-"+(f"{day:02d}")]
    tiempo= pd.to_datetime(dia['Hora'], format=' %H:%M:%S').dt.time
    temp= dia['TEMP']
    plt.plot(tiempo, temp) #, color = 'red' )# 

plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Temperatura Jun 2019")
plt.legend(datos['Fecha'])
plt.show()
imagen.savefig('TEMPJUN2019')

我从中得到的图像是下一个:

enter image description here

python python-3.x matplotlib overlay legend
1个回答
0
投票

尝试这样的事情:


imagen = plt.figure(figsize=(25,10))

dia_lst = [] # <======================================================= Here
for day in [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30]:
    dia = datos[datos['Fecha'] == "2019-06-"+(f"{day:02d}")]
    dia_lst.append(f"2019-06-{day:02d}") # <=========================== Here
    tiempo= pd.to_datetime(dia['Hora'], format=' %H:%M:%S').dt.time
    temp= dia['TEMP']
    plt.plot(tiempo, temp) #, color = 'red' )# 

plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Temperatura Jun 2019")
plt.legend(dia_lst) # <================================================ Here 
plt.show()
imagen.savefig('TEMPJUN2019')

所以您的datos ['Fecha']似乎只包含一个日期,应该根据需要对此日期进行更新。

© www.soinside.com 2019 - 2024. All rights reserved.