✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Multiple Heatmap from CSV file

Hi everyone,

I am trying to plot multiple heatmaps using columns from a CSV file.

The CSV file has columns
X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3…X28,Y28,Z28

I am using a button, to be able to plot each heatmap
So

  • Heatmap1 pertaining to columns X1, Y1, Z1
  • Heatmap2 pertaining to columns X2, Y2, Z2
  • Heatmap28 pertaining to columns X28, Y28, Z28

I have the below code

######AUTO VERSION
#Read CSV File
data=pd.read_csv("maindata.csv")
data_labels=pd.read_csv("labels.csv")

labels= list(data_labels['Main Labels'])

#fig = go.Figure()
fig = make_subplots(
    rows=1, cols=1, 
)

counter=0
traces={}
for datacolumns in range(1,29):
    for k, col in enumerate(data.columns):
        XString='X'+str(datacolumns)
        YString='Y'+str(datacolumns)
        ZString=Z'+str(datacolumns)
        if k > 0:
            traces['trace_' + col]=go.Heatmap(
                                    x=XString,
                                    y=YString,
                                    z=ZString,
                                    hovertemplate='Z: %{z}<br>'+'Y: %{y}<br>'+'X: %{x}<br>',
                                    colorscale="greens", #Have to make this random
                                    #colorbar = dict(x=0.45),
                                    visible=False,
                                    ),1, 1)

# convert data to form required by plotly
df_plot=list(traces.values())

# build figure
fig=go.Figure(df_plot)

### Create buttons for drop down menu
buttons = []
for i, label in enumerate(labels):
    visibility = [i==j for j in range(len(labels))]
    button = dict(
                 label =  label,
                 method = 'update',
                 args = [{'visible': visibility},
                     #{'title': label}
                     ])
    buttons.append(button)

updatemenus = list([
    dict(active=-1,
         x=-0.15,
         buttons=buttons
    )
])


fig.update_xaxes(
                title_text="X",
                row=1, col=1
)

fig.update_yaxes(
                title_text="Y",
                row=1, col=1
)

fig['layout']['title'] = 'Heatmap Plot'
fig['layout']['showlegend'] = False
fig['layout']['updatemenus'] = updatemenus

fig.show()

with open('plot_heatmap_auto.html', 'w') as f:
    f.write(fig.to_html(include_plotlyjs='cdn'))

I think I have made an error in how I have structured the loop.
Any suggestions?

Thanks in advance.