How to plot two lines on the same interactive graph?

Viewed 472

is it possible to plot two lines on the same graph? I am using Panel on Jupyter. However, I only can plot two lines on two different graphs. I would like to have everything on the same graph to compare results while I am changing input values.

I do not care if I have to use Panel, Bokeh, Holoview, etc.

Best regards, VQ



import panel as pn
pn.extension()
import panel.widgets as pnw
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def mplplot(df, **kwargs):
    fig = df.plot(title='Title - Convert temperature from degree Celsius to Kelvin') 


    fig.legend(["Temperature"]);
    fig.set_xlabel("Celcius")
    fig.set_ylabel("Kelvin")
    fig= fig.get_figure()
    plt.close(fig)
    return fig


def Celsius_to_Kelvin(C=100, view_fn=mplplot):
    C = np.arange(0,C, 1)    
    K = (C + 273.15)
    df = pd.DataFrame(dict(y=K), index=C)

    return view_fn(df, K=K, C=C)

def Celsius_to_Kelvin1(C=100, view_fn=mplplot):
    C = np.arange(0,C, 1)    
    K = (C + 273.15)
    df = pd.DataFrame(dict(y=K), index=C)

    return view_fn(df, K=K, C=C)


C  = pnw.FloatSlider(name='C', value=50, start=0, end=100) 
C1  = pnw.FloatSlider(name='C1', value=40, start=0, end=100)

@pn.depends(C.param.value)
def Celsius_to_Kelvin_variables(C):
    return Celsius_to_Kelvin(C)

@pn.depends(C1.param.value)
def Celsius_to_Kelvin_variables1(C1):
    return Celsius_to_Kelvin1(C1)

widgets    = pn.Column("<br>\n#### Variable configuration", C, C1)
Celsius_to_Kelvin_panel = pn.Row(Celsius_to_Kelvin_variables, Celsius_to_Kelvin_variables1, widgets)

Celsius_to_Kelvin_panel   

enter image description here

1 Answers

You can plot two lines on the same graph using just about any plotting library. To do it, you follow the instructions for whatever plotting library you are using; Panel will be able to plot whatever you come up with (but is otherwise not involved in such overlays). Here your code is using Pandas .plot(), which as far as I know only makes overlaid plots for data in the same dataframe, but you can use the native Matplotlib API to create the plot directly. See e.g. https://python-graph-gallery.com/122-multiple-lines-chart/ or the Matplotlib documentation.

Related