python - 如何在同一个交互式图形上绘制两条线?
问题描述
是否可以在同一张图上绘制两条线?我在 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
解决方案
您可以使用几乎任何绘图库在同一图表上绘制两条线。为此,请按照您使用的任何绘图库的说明进行操作;面板将能够绘制您想出的任何内容(但不参与此类叠加)。在这里,您的代码使用 Pandas .plot(),据我所知,它只为同一数据框中的数据制作叠加图,但您可以使用本机 Matplotlib API 直接创建图。参见例如https://python-graph-gallery.com/122-multiple-lines-chart/或 Matplotlib 文档。
推荐阅读
- .net - Microsoft.Diagnostics.Tracing.EventSource 与 RabbitMQ.Client.dll 异常
- makefile - 如何在 Makefile.am 中调整 PATH 变量?
- java - 将所有请求参数映射到 Spring Controller 中的对象中
- c - 为 UART 模块设计状态机
- botframework - 将带有 POST 的参数发送到 Botframework(和不同的通道)
- c# - 如何将热键绑定到通用按钮?
- android-studio - 如何在 Flutter 中实现 Google fitkit?
- ruby-on-rails - Rspec GET 到不同的端点一秒钟 - ActionController::UrlGenerationError
- javascript - 如何设计类 Bill 和类 SimplifiedBill 之间的关系?
- powershell - 使 arraylist 上的 select -unique 返回 arraylist 而不是字符串