首页 > 解决方案 > 如何在同一个交互式图形上绘制两条线?

问题描述

是否可以在同一张图上绘制两条线?我在 Jupyter 上使用面板。但是,我只能在两个不同的图表上绘制两条线。我希望在更改输入值时将所有内容都放在同一个图表上以比较结果。

我不在乎是否必须使用 Panel、Bokeh、Holoview 等。

最好的问候,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   

在此处输入图像描述

标签: pythonmatplotlibbokehpanelholoviews

解决方案


您可以使用几乎任何绘图库在同一图表上绘制两条线。为此,请按照您使用的任何绘图库的说明进行操作;面板将能够绘制您想出的任何内容(但不参与此类叠加)。在这里,您的代码使用 Pandas .p​​lot(),据我所知,它只为同一数据框中的数据制作叠加图,但您可以使用本机 Matplotlib API 直接创建图。参见例如https://python-graph-gallery.com/122-multiple-lines-chart/或 Matplotlib 文档。


推荐阅读