Create a GUI to show a plot iterating over rows in Python

For each “CODE” i need to display a plot that has the four “vPoint” on the vertical axis and the four “step” on the horizontal one. So i need to create a viewer/GUI that allows the user to switch between the plots of each row. enter image description here

Is there a way to implement it in python? Should I use tkinter? Any further advice is welcomed.

Trivial with Plotly. Should be even easier in PowerBI.

See also Bokeh. (We have an active community Discourse if you try and have questions)

3 Likes

I would recommend using HoloViz Panel.

You can use matplotlib, bokeh, plot, altair or your favorite plotting library.

Here I’m using hvplot because it is easy to use and you get interactive plots.

app

app.py

import hvplot.pandas # Could be any plotting library
import pandas as pd

wide_data = pd.DataFrame({
    "CODE": ["A", "B", "C"],
    "vPoint1_R4": [357, 247, 232],
    "vPoint2_R4": [430, 365, 285],
    "vPoint3_R4": [502, 448, 360],
    "vPoint4_R4": [539, 477, 398],
    "step1": [15, 15, 15],
    "step2": [30, 30, 30],
    "step3": [45, 45, 45],
    "step4": [60, 60, 60],
})

def to_tidy_data(wide_data):
    ydata = pd.melt(wide_data, id_vars=["CODE"], value_vars=["vPoint1_R4", "vPoint2_R4", "vPoint3_R4", "vPoint4_R4"], value_name="y", var_name="item")
    ydata["item"]=ydata["item"].str[-4]
    xdata = pd.melt(wide_data, id_vars=["CODE"], value_vars=["step1", "step2", "step3", "step4"], value_name="x", var_name="item")
    xdata["item"]=xdata["item"].str[-1]
    data = pd.merge(xdata, ydata, on=["CODE", "item"])
    return data

tidy_data = to_tidy_data(wide_data)

def plot(index):
    if not index:
        return "No row selected"
    
    code = wide_data.iloc[index].CODE.iloc[0]
    filtered_data = tidy_data[tidy_data["CODE"]==code]
    return filtered_data.hvplot.scatter(x="x", y="y")

import panel as pn

pn.extension(sizing_mode="stretch_width")

table = pn.widgets.Tabulator(wide_data, height=125, theme="fast", selectable=1, )
interactive_plot = pn.bind(plot, index=table.param.selection)

pn.template.FastListTemplate(
    site="Awesome Panel", title="Table selections",
    main=[table, pn.panel(interactive_plot, height=350)]
).servable()
pip install pandas hvplot panel
panel serve app.py --autoreload --show

For inspiration check out my site awesome-panel.org.

2 Likes