我在使用Panel和Altair时遵循此tutorial [github]。
返回我的情节的函数如下:
@pn.depends(ticker.param.value, date_range_slider.param.value)
def get_plot(ticker, date_range):
df = data.stocks()
df['date'] = pd.to_datetime(df['date'])
start_date, end_date = date_range
mask = (df['date'] > start_date) & (df['date'] <= end_date)
dfm = df.loc[mask]
chart = (alt.Chart(dfm)
.mark_line()
.encode(
x='date', y='price',
tooltip=alt.Tooltip(['date', 'price']))
.transform_filter((datum.symbol == ticker))
)
return chart
我遇到一个问题,如果直接执行该图表,该图表将正常显示:
但在使用Panel运行时不会:
似乎当我将工作的chart
放入panel
时,它失去了到数据的链接:
为仪表板提供服务存在相同的问题,因此在我看来,Panel和Altair之间存在兼容性问题。
任何人都知道哪里出了问题以及如何解决?
版本:
答案 0 :(得分:0)
这现在可以工作了,运行教程中的代码并在这个问题中进行了修改,显示了绘图并响应了小部件输入:
完整代码:
import panel as pn
import altair as alt
import pandas as pd
from vega_datasets import data
import datetime as dt
from altair import datum
alt.renderers.enable('default')
pn.extension('vega')
source = data.stocks()
source = pd.DataFrame(source)
source.head()
# set a title for your dashboard
title = '## Stock Price Dashboard'
subtitle = 'This dashboard allows you to select a company and date range to see stock prices.'
# create list of company names (tickers)
tickers = ['AAPL', 'GOOG', 'IBM', 'MSFT']
# this creates the dropdown widget
ticker = pn.widgets.Select(name='Company', options=tickers)
# this creates the date range slider
date_range_slider = pn.widgets.DateRangeSlider(
name='Date Range Slider',
start=dt.datetime(2001, 1, 1), end=dt.datetime(2010, 1, 1),
value=(dt.datetime(2001, 1, 1), dt.datetime(2010, 1, 1))
)
@pn.depends(ticker.param.value, date_range_slider.param.value)
def get_plot(ticker, date_range):
df = data.stocks()
df['date'] = pd.to_datetime(df['date'])
start_date, end_date = date_range
mask = (df['date'] > start_date) & (df['date'] <= end_date)
dfm = df.loc[mask]
chart = (alt.Chart(dfm)
.mark_line()
.encode(
x='date', y='price',
tooltip=alt.Tooltip(['date', 'price']))
.transform_filter((datum.symbol == ticker))
)
return chart
dashboard = pn.Row(
pn.Column(title, subtitle, ticker, date_range_slider),
get_plot # draw chart function!
)
dashboard
版本:
from sinfo import sinfo
sinfo()
-----
altair 4.1.0
pandas 1.2.0
panel 0.10.3
vega_datasets 0.9.0
-----
IPython 7.19.0
jupyter_client 6.1.11
jupyter_core 4.7.0