Good evening, I have a code that takes a dataframe and plots all the traces as well as calulated data values that are shown a subplot table. I am new to Plotly so know it is not an optimal solution, but would like to be able to change the initial loaded to dataframe with a user changable dropdown which updates the graphs and tables, all the dataframes are in a simular format with the same calculations, but may have a differing number of data columns dependanding on the dataframe. I know I could merge the dataframes into 1 and make individual traces visible/ invisible with dropdown, but as the loaded csv files will be chnaged, would like to keep them modular for ease of use.
Code:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
import math
import csv
import numpy as np
import numpy as np
import plotly.io as pio
from plotly.subplots import make_subplots
pd.options.mode.chained_assignment = None
df = pd.read_csv(r"C:\Users\tompl\Desktop\Characterisation\Files\Ti2-40x-09.22.csv", index_col=0)
fig = make_subplots(
rows=2, cols=1,
row_heights=[0.7,0.4],
subplot_titles=("", "Power across the 20nm Band (mW)"),
vertical_spacing = 0.15,
specs=[[{"type": "scatter"}],
[{"type": "table"}]])
cols = len(df.axes[1])
print(df)
fig.add_trace(go.Scatter(x=df[df.columns[0]], y=df[df.columns[1]], name= df.columns[1], mode='lines', line=dict(color="#000000", dash='dot')), row=1, col=1)
for i in range(2,cols):
fig.add_trace(go.Scatter(x=df[df.columns[0]], y=df[df.columns[i]], name= df.columns[i]), row=1, col=1)
fig.update_layout(xaxis_range=[350,840])
df365 = df.loc[355:375]
df400 = df.loc[390:410]
df435 = df.loc[425:445]
df470 = df.loc[460:480]
df500 = df.loc[490:510]
df550 = df.loc[540:560]
df580 = df.loc[570:580]
df635 = df.loc[625:645]
df1 = df.iloc[: , 1:]
col_list = list(df1)
Power = pd.DataFrame(index = ['365nm','400nm','435nm','470nm','500nm','550nm','580nm','635nm'], columns = [col_list])
for i in range(1,cols):
Power.iat[0,i-1]= round(np.trapz(y=df365.loc[:,df365.columns[i]], x=df365.loc[:,'Wavelength'])/1e3,3)
Power.iat[1,i-1]= round(np.trapz(y=df400.loc[:,df400.columns[i]], x=df400.loc[:,'Wavelength'])/1e3,3)
Power.iat[2,i-1]= round(np.trapz(y=df435.loc[:,df435.columns[i]], x=df435.loc[:,'Wavelength'])/1e3,3)
Power.iat[3,i-1]= round(np.trapz(y=df470.loc[:,df470.columns[i]], x=df470.loc[:,'Wavelength'])/1e3,3)
Power.iat[4,i-1]= round(np.trapz(y=df500.loc[:,df500.columns[i]], x=df500.loc[:,'Wavelength'])/1e3,3)
Power.iat[5,i-1]= round(np.trapz(y=df550.loc[:,df550.columns[i]], x=df550.loc[:,'Wavelength'])/1e3,3)
Power.iat[6,i-1]= round(np.trapz(y=df580.loc[:,df580.columns[i]], x=df580.loc[:,'Wavelength'])/1e3,3)
Power.iat[7,i-1]= round(np.trapz(y=df635.loc[:,df635.columns[i]], x=df635.loc[:,'Wavelength'])/1e3,3)
font_color = pd.DataFrame(index = ['365nm','400nm','435nm','470nm','500nm','550nm','580nm','635nm'], columns = [col_list])
for n in range(0,8):
for i in range(1,cols):
if Power.iat[n,i-1] <3:
font_color.iat[n,i-1]='black'
elif Power.iat[n,i-1]/Power.iat[n,0] <1:
font_color.iat[n,i-1]='red'
else:
font_color.iat[n,i-1]='green'
Power = Power.mask(Power < 3, 'N/A')
Wavelength = ['365nm','400nm','435nm','470nm','500nm','550nm','580nm','635nm']
Power.insert(0, 'Wavelength', Wavelength)
font_color.insert(0, 'Wavelength', Wavelength)
font_color['Wavelength'] = 'black'
font_color = font_color.T
text_size=275/cols
if text_size >= 12:
text_size = 12
else:
text_size = text_size
fig.add_trace(go.Table(
header=dict(values=list(Power.columns),
fill_color='paleturquoise',
align='center',
font_size=text_size),
cells=dict(values=Power.transpose().values.tolist(),
fill_color='lavender',
font_color=font_color,
align='center')), row=2, col=1)
maxPower=(df.max())
m=maxPower.max()
def round_up_to_nearest_1000(num):
return math.ceil(num / 1000) * 1000
m=round_up_to_nearest_1000(m)
fig.update_xaxes(showline=True, linewidth = 0.5, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth = 0.5, linecolor='black', mirror=True)
fig.update_xaxes(showgrid=True, linewidth = 0.25, gridcolor='lightgrey', mirror=True)
fig.update_yaxes(showgrid=True, linewidth = 0.25, gridcolor='lightgrey', mirror=True)
fig.update_layout(yaxis_range=[0, m])
fig.update_layout(title="Bi-Annual Characterisation on a Nikon Ti2 at 40x Magnification", title_x=0.5)
fig.update_layout(xaxis_title="Wavelength (nm)", yaxis_title="Spectral Flux (uW/nm)")
pio.write_html(fig, file='Test.html', auto_open=True)