Iām having a problem trying to figure out how to rotate the text on my heatmap. That dark-gray.white line running horic=zontally in the center of my heatmap is text that is overlaid on top of each other. I would like to rotate it 90 degrees so that it displays vertical, rather than horizontal. I have tried using textangle, add_annotation, and update_annotations with no luck. Does someone here know how this could be accomplished? Seems like it should be an easy thing to do but I am having a bit of trouble.
truck_df output:
Truck Date VIPR Data Viriciti Data
3082 231_28_KAL_T2E_KIT_353604 2022-06-10 True False
3083 231_28_KAL_T2E_KIT_353604 2022-06-11 True False
3084 231_28_KAL_T2E_KIT_353604 2022-06-12 True False
3085 231_28_KAL_T2E_KIT_353604 2022-06-13 True False
3086 231_28_KAL_T2E_KIT_353604 2022-06-14 True False
3087 231_28_KAL_T2E_KIT_353604 2022-06-15 True False
3088 231_28_KAL_T2E_KIT_353604 2022-06-16 True False
3089 231_28_KAL_T2E_KIT_353604 2022-06-17 True False
3090 231_28_KAL_T2E_KIT_353604 2022-06-18 True False
3091 231_28_KAL_T2E_KIT_353604 2022-06-19 True False
3092 231_28_KAL_T2E_KIT_353604 2022-06-20 True False
3093 231_28_KAL_T2E_KIT_353604 2022-06-21 True False
3094 231_28_KAL_T2E_KIT_353604 2022-06-22 False False
3095 231_28_KAL_T2E_KIT_353604 2022-06-23 False False
3096 231_28_KAL_T2E_KIT_353604 2022-06-24 False False
3097 231_28_KAL_T2E_KIT_353604 2022-06-25 False False
3098 231_28_KAL_T2E_KIT_353604 2022-06-26 False False
3099 231_28_KAL_T2E_KIT_353604 2022-06-27 False False
3100 231_28_KAL_T2E_KIT_353604 2022-06-28 False False
3101 231_28_KAL_T2E_KIT_353604 2022-06-29 False False
3102 231_28_KAL_T2E_KIT_353604 2022-06-30 False False
3103 231_28_KAL_T2E_KIT_353604 2022-07-01 False False
3104 231_28_KAL_T2E_KIT_353604 2022-07-02 False False
3105 231_28_KAL_T2E_KIT_353604 2022-07-03 False False
3106 231_28_KAL_T2E_KIT_353604 2022-07-04 False False
3107 231_28_KAL_T2E_KIT_353604 2022-07-05 True False
3108 231_28_KAL_T2E_KIT_353604 2022-07-06 True False
3109 231_28_KAL_T2E_KIT_353604 2022-07-07 True False
3110 231_28_KAL_T2E_KIT_353604 2022-07-08 True False
3111 231_28_KAL_T2E_KIT_353604 2022-07-09 True False
3112 231_28_KAL_T2E_KIT_353604 2022-07-10 True False
3113 231_28_KAL_T2E_KIT_353604 2022-07-11 True False
3114 231_28_KAL_T2E_KIT_353604 2022-07-12 True False
3115 231_28_KAL_T2E_KIT_353604 2022-07-13 True False
3116 231_28_KAL_T2E_KIT_353604 2022-07-14 True False
3117 231_28_KAL_T2E_KIT_353604 2022-07-15 True False
3118 231_28_KAL_T2E_KIT_353604 2022-07-16 True False
3119 231_28_KAL_T2E_KIT_353604 2022-07-17 True False
3120 231_28_KAL_T2E_KIT_353604 2022-07-18 True False
3121 231_28_KAL_T2E_KIT_353604 2022-07-19 True False
3122 231_28_KAL_T2E_KIT_353604 2022-07-20 True False
3123 231_28_KAL_T2E_KIT_353604 2022-07-21 True False
3124 231_28_KAL_T2E_KIT_353604 2022-07-22 False False
3125 231_28_KAL_T2E_KIT_353604 2022-07-23 False False
3126 231_28_KAL_T2E_KIT_353604 2022-07-24 True False
3127 231_28_KAL_T2E_KIT_353604 2022-07-25 True False
3128 231_28_KAL_T2E_KIT_353604 2022-07-26 True False
3129 231_28_KAL_T2E_KIT_353604 2022-07-27 True False
3130 231_28_KAL_T2E_KIT_353604 2022-07-28 True False
3131 231_28_KAL_T2E_KIT_353604 2022-07-29 True False
3132 231_28_KAL_T2E_KIT_353604 2022-07-30 True False
3133 231_28_KAL_T2E_KIT_353604 2022-07-31 True False
3134 231_28_KAL_T2E_KIT_353604 2022-08-01 True False
3135 231_28_KAL_T2E_KIT_353604 2022-08-02 True False
3136 231_28_KAL_T2E_KIT_353604 2022-08-03 True False
3137 231_28_KAL_T2E_KIT_353604 2022-08-04 True False
3138 231_28_KAL_T2E_KIT_353604 2022-08-05 True False
3139 231_28_KAL_T2E_KIT_353604 2022-08-06 True False
3140 231_28_KAL_T2E_KIT_353604 2022-08-07 True False
3141 231_28_KAL_T2E_KIT_353604 2022-08-08 True False
3142 231_28_KAL_T2E_KIT_353604 2022-08-09 True False
3143 231_28_KAL_T2E_KIT_353604 2022-08-10 True False
3144 231_28_KAL_T2E_KIT_353604 2022-08-11 True False
3145 231_28_KAL_T2E_KIT_353604 2022-08-12 True False
3146 231_28_KAL_T2E_KIT_353604 2022-08-13 True False
3147 231_28_KAL_T2E_KIT_353604 2022-08-14 True False
3148 231_28_KAL_T2E_KIT_353604 2022-08-15 True False
3149 231_28_KAL_T2E_KIT_353604 2022-08-16 True False
3150 231_28_KAL_T2E_KIT_353604 2022-08-17 True False
3151 231_28_KAL_T2E_KIT_353604 2022-08-18 True False
3152 231_28_KAL_T2E_KIT_353604 2022-08-19 True False
3153 231_28_KAL_T2E_KIT_353604 2022-08-20 True False
3154 231_28_KAL_T2E_KIT_353604 2022-08-21 True False
3155 231_28_KAL_T2E_KIT_353604 2022-08-22 True False
3156 231_28_KAL_T2E_KIT_353604 2022-08-23 True False
3157 231_28_KAL_T2E_KIT_353604 2022-08-24 True False
3158 231_28_KAL_T2E_KIT_353604 2022-08-25 True False
3159 231_28_KAL_T2E_KIT_353604 2022-08-26 True False
3160 231_28_KAL_T2E_KIT_353604 2022-08-27 True False
3161 231_28_KAL_T2E_KIT_353604 2022-08-28 True False
3162 231_28_KAL_T2E_KIT_353604 2022-08-29 False False
3163 231_28_KAL_T2E_KIT_353604 2022-08-30 True False
3164 231_28_KAL_T2E_KIT_353604 2022-08-31 False False
3165 231_28_KAL_T2E_KIT_353604 2022-09-01 False False
3166 231_28_KAL_T2E_KIT_353604 2022-09-02 False False
3167 231_28_KAL_T2E_KIT_353604 2022-09-03 False False
3168 231_28_KAL_T2E_KIT_353604 2022-09-04 False False
3169 231_28_KAL_T2E_KIT_353604 2022-09-05 True False
3170 231_28_KAL_T2E_KIT_353604 2022-09-06 False False
3171 231_28_KAL_T2E_KIT_353604 2022-09-07 False False
3172 231_28_KAL_T2E_KIT_353604 2022-09-08 False False
3173 231_28_KAL_T2E_KIT_353604 2022-09-09 False False
3174 231_28_KAL_T2E_KIT_353604 2022-09-10 False False
3175 231_28_KAL_T2E_KIT_353604 2022-09-11 False False
3176 231_28_KAL_T2E_KIT_353604 2022-09-12 False False
3177 231_28_KAL_T2E_KIT_353604 2022-09-13 False False
3178 231_28_KAL_T2E_KIT_353604 2022-09-14 False False
3179 231_28_KAL_T2E_KIT_353604 2022-09-15 False False
3180 231_28_KAL_T2E_KIT_353604 2022-09-16 True False
3181 231_28_KAL_T2E_KIT_353604 2022-09-17 True False
3182 231_28_KAL_T2E_KIT_353604 2022-09-18 True False
3183 231_28_KAL_T2E_KIT_353604 2022-09-19 True False
3184 231_28_KAL_T2E_KIT_353604 2022-09-20 True False
3185 231_28_KAL_T2E_KIT_353604 2022-09-21 True False
3186 231_28_KAL_T2E_KIT_353604 2022-09-22 False False
3187 231_28_KAL_T2E_KIT_353604 2022-09-23 False False
3188 231_28_KAL_T2E_KIT_353604 2022-09-24 False False
3189 231_28_KAL_T2E_KIT_353604 2022-09-25 False False
3190 231_28_KAL_T2E_KIT_353604 2022-09-26 True False
3191 231_28_KAL_T2E_KIT_353604 2022-09-27 True False
3192 231_28_KAL_T2E_KIT_353604 2022-09-28 True False
3193 231_28_KAL_T2E_KIT_353604 2022-09-29 True False
3194 231_28_KAL_T2E_KIT_353604 2022-09-30 True False
3195 231_28_KAL_T2E_KIT_353604 2022-10-01 True False
3196 231_28_KAL_T2E_KIT_353604 2022-10-02 True False
3197 231_28_KAL_T2E_KIT_353604 2022-10-03 True False
3198 231_28_KAL_T2E_KIT_353604 2022-10-04 True False
3199 231_28_KAL_T2E_KIT_353604 2022-10-05 True False
3200 231_28_KAL_T2E_KIT_353604 2022-10-06 False False
Code:
import plotly.graph_objects as go
import numpy as np
import pandas as pd
from datetime import date, datetime, timedelta
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
try:
# Subset the DataFrame to isolate the date column
truck_date_df = truck_df['Date']
# Convert the 'Date' column of the DataFrame into a list for the heatmap
dates = list(truck_df['Date'])
# Convert the 'vipr_bool' column of the DataFrame into a list
vipr_bool = list(truck_df['VIPR Data'])
# Convert 'False' to '0'
vipr_bool = [0 if item == False else item for item in vipr_bool]
# Convert 'True' to '1'
vipr_bool = [1 if item == True else item for item in vipr_bool]
except Exception as e:
print('-' * 40)
print('update_plot ERROR')
print(e)
print('-' * 40)
try:
# Filter the list to find the first 'True' value
vipr_first_true = vipr_bool.index(1)
# Reverse the list
vipr_first_true_reversed = vipr_bool[::-1]
# Find the index position of the last 'True'
vipr_first_index_reversed = vipr_first_true_reversed.index(1)
# Subtract 1 & the index position of the last 'True' from the length of 'vipr_bool' to obtain the index of the last 'True'
vipr_last_true = len(vipr_bool)-1-vipr_first_index_reversed
# Finds the row # and date of the first 'True' value
vipr_low = truck_date_df[vipr_first_true:vipr_first_true+1]
# Removes the index row # and isolates only the date
vipr_low = vipr_low.iloc[0]
# Finds the row # and date of the last 'True' value
vipr_high = truck_date_df[vipr_last_true:vipr_last_true+1]
# Removes the index row # and isolates only the date
vipr_high = vipr_high.iloc[0]
# Convert all '0' values to 'None', leaving the '1' values
vipr_ones = [None if v!=1 else v for v in vipr_bool]
# Convert all '1' values to 'None' leaving the '0' values
vipr_zeros = [None if v!=0 else v for v in vipr_bool]
except Exception as e:
print('VIPR')
print(e)
vipr_low = str(date(2014, 1, 1))
vipr_high = str(date.today())
vipr_ones = [0] * len(vipr_bool)
vipr_zeros = [0] * len(vipr_bool)
try:
# Assign vipr_low & vipr_high to low_range & high_range
low_range = vipr_low
high_range = vipr_high
except Exception as e:
print(e)
try:
# Initialize figure for plotly graph object
fig = go.Figure()
except Exception as e:
print('-' * 40)
print('fig initialization ERROR')
print(e)
print('-' * 40)
try:
# Add a trace to the heatmap containing all of the '1' values along with a legend marker
fig.add_trace(go.Heatmap(
x=truck_date_df,
y=[''],
z=[vipr_ones],
text=[dates],
texttemplate='%{text}',
textfont={'size':10},
name='= Data Found',
colorscale=[(0.00, "#fde725"), (1.0, "#fde725")],
showscale=False,
showlegend=True,
hovertemplate='<i><b>Date</i>: %{x}<extra></extra>'
)
)
except Exception as e:
print(e)
try:
# Add a trace to the heatmap containing all of the '0' values along with a legend marker
fig.add_trace(go.Heatmap(
x=truck_date_df,
y=[''],
z=[vipr_zeros],
name='= Data Not Found',
colorscale=[(0.00, "#440154"), (1.0, "#440154")],
showscale=False,
showlegend=True,
hovertemplate='<i><b>Date</i>: %{x}<extra></extra>'
)
)
except Exception as e:
print(e)
# Add a trace to the heatmap containing all of the '0' values along with a legend marker
fig.add_trace(go.Heatmap(
x=truck_date_df,
y=[''],
z=[vipr_zeros],
name='= Data Not Found',
colorscale=[(0.00, "#440154"), (1.0, "#440154")],
showscale=False,
showlegend=True
)
)
try:
# Update the figure's x-axis/y-axis titles and the x-axis range
fig.update_layout(title=go.layout.Title(text='<b>Data for Truck: </b>' + str(truck)),
xaxis_title='<b>Date</b>',
yaxis_title='',
xaxis_range=[low_range, high_range]
)
except Exception as e:
print(e)
# Update the figure's x-axis/y-axis titles and the x-axis range
fig.update_layout(title=go.layout.Title(text='<b>Data for Truck: </b>' + str(truck)),
xaxis_title='<b>Date</b>',
yaxis_title='',
xaxis_range=[date(2014, 1, 1), date(date.today())]
)
fig.update_layout(legend=dict(
orientation='h',
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
fig.show()