I have a pandas dataframe with thee columns 'SystemState_InitDate'
, 'SystemState_FinalDate'
and 'State_Desc'
.
I want to plot a chronogram. To do that, I have generated the following function
for idx, row in df_cleaned.iterrows():
ax.barh(y=row['State_Desc'], left=row['SystemState_InitDate'], width=row['SystemState_FinalDate'] - row['SystemState_InitDate'], color=colors[row['State_Desc']], edgecolor='black')
ax.set_xlim([df_cleaned['SystemState_InitDate'].min(), df_cleaned['SystemState_FinalDate'].max()])
ax.xaxis.set_major_locator(mdates.HourLocator(interval=6))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
and later on I plot as
plt.xlabel('Fecha y Hora')
plt.ylabel('Estado del Sistema')
plt.title('Cronograma de Estados del Sistema')
The resulting plot, as you can see in the figure, has a wide blank space in the middle, since nothing was done during those times.
I was wondering if there was a way for me to cut all the times in which nothing is happening so that I can get a more dense figure where the details can be appreciated better without that useless white space[![enter image description here][1]][1].
I have tried chatgpt but it has suggested me to use the following:
prev_final_date = None
for idx, row in df_cleaned.iterrows():
if prev_final_date is not None and row['SystemState_InitDate'] > prev_final_date:
# Si hay un hueco, no dibujar nada en ese hueco
continue
ax.barh(y=row['State_Desc'], left=row['SystemState_InitDate'], width=row['SystemState_FinalDate'] - row['SystemState_InitDate'], color=colors.get(row['State_Desc'], 'grey'), edgecolor='black')
prev_final_date = row['SystemState_FinalDate']
ax.set_xlim([tiempoInicial, tiempoFinal])
ax.xaxis.set_major_locator(mdates.HourLocator(interval=6))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
which returns a blank image.
Can someone please tell me how can I solve my problem? Any help is appreciated.
[1]: https://i.sstatic.net/pBbPhysf.png