尝试使用Jupyter Notebook中的下拉菜单中的值绘制绘图

时间:2019-04-17 16:17:13

标签: python ipywidgets

所以我有5家公司的股价。我想要做的是使用下拉菜单选择公司,并使用从下拉菜单中选择的公司数据创建线图。到目前为止,我的代码如下:

SELECT 1
FROM FM.SAAdjustment SAA
    LEFT JOIN 
    (select RecordIDKey, 
            ApprovalLevel, 
            NumberOfApprovals = count(ApprovedID)
    from dbo.Approved A 
        JOIN dbo.OrgApproval OA ON A.OrgApprovalID = OA.OrgApprovalID
    where A.ProcessID = 21
        and A.Status = 1
        and convert(varchar(32),GetDate(),101) between OA.EffectiveStartDate AND OA.EffectiveEndDate
    group by RecordIDKey, ApprovalLevel
    ) A ON SAA.SAAdjustmentID = A.RecordIDKey and SAA.CurrentLevel = A.ApprovalLevel
    LEFT JOIN 
    (select OrgStructureID, 
            ApprovalLevel, 
            NumberofApprovalRequired
    from dbo.OrgApproval
    where ProcessID = 21
        and convert(varchar(32),GetDate(),101) between EffectiveStartDate AND EffectiveEndDate
    ) OA on SAA.OrgStructureID = OA.OrgStructureID and SAA.CurrentLevel = OA.ApprovalLevel
    LEFT JOIN 
    (select OrgStructureID, 
            HighestApprovalLevel = max(ApprovalLevel)
    from dbo.OrgApproval 
    where ProcessID = 21
        and convert(varchar(32),GetDate(),101) between EffectiveStartDate AND EffectiveEndDate
    group by OrgStructureID
    ) MA ON SAA.OrgStructureID = MA.OrgStructureID
WHERE SAA.BankAccountID = @BankAccountID
    and SAA.ProcessStatus = 1
    and SAA.AdjustmentMethod = 5
    and isnull(SAA.ValidatedFlag,0) = 1
    and (SAA.CurrentLevel < MA.HighestApprovalLevel
            or SAA.CurrentLevel = HighestApprovalLevel and isnull(A.NumberOfApprovals,0) < OA.NumberofApprovalRequired
)

但是,当我从下拉菜单中选择一个值时,图不显示。我究竟做错了什么?我以前使用过Plotly来绘制图表,但是我是ipywidgets的新手,因此,我们将不胜感激。

预先感谢

2 个答案:

答案 0 :(得分:1)

在代码结尾,您需要使下拉窗口小部件的值更改时调用response函数。

stocks.observe(response, type='change', names=['value'])

答案 1 :(得分:0)

Plotly具有一个小部件模式,该模式很好用,我有时将其与Voila一起用于仪表板(https://github.com/QuantStack/voila)以及与其他ipywidgets一起使用,包括BQplot。 (我更喜欢一些Plotly绘图类型,我更喜欢BQplot的交互方式)。

此代码尚未经过测试,但可能是这样的。

import ipywidgets as widgets
import plotly.graph_objs as go


data = [go.Scatter(
    x = Intel['date'],
    y = Intel['close'],
    mode = 'lines',
    name = 'Data',
    line = dict(
        color = ('rgb(205, 12, 24)'),
        width = 4)
)]

plotlyFig = go.Figure(data=data)
plotlyFig['layout'].update(height=800,
                           width=800,
                           title='',
                           xaxis=dict(
                               title='x Axis',
                               titlefont=dict(
                                   family='Arial',
                                   size=18,
                                   color='#7f7f7f'
                               )
                           ),
                           yaxis=dict(
                               title='y Axis',
                               titlefont=dict(
                                   family='Arial',
                                   size=18,
                                   color='#7f7f7f'
                               )
                           )
                           )

PlotlyWidget = go.FigureWidget(plotlyFig)   # This creates a Plotly Ipywidget.
PlotlyWidgetData = PlotlyWidget.data[0]        

def response(change):
    name = stocks.value

    if name=='Intel' :
        PlotlyWidgetData.x = Intel['date']
        PlotlyWidgetData.y = Intel['close']
    elif name=='Nvidia' :
        PlotlyWidgetData.x = Nvidia['date']
        PlotlyWidgetData.y = Nvidia['close']
    elif name=='AMD' :
        PlotlyWidgetData.x = AMD['date']
        PlotlyWidgetData.y = AMD['close']
    elif name=='Gigabyte' :
        PlotlyWidgetData.x = Gigabyte['date']
        PlotlyWidgetData.y = Gigabyte['close']
    elif name=='Asus' :
        PlotlyWidgetData.x = Asus['date']
        PlotlyWidgetData.y = Asus['close']
    elif name=='MSI' :
        PlotlyWidgetData.x = MSI['date']
        PlotlyWidgetData.y = MSI['close']
    elif name=='EVGA' :
        PlotlyWidgetData.x = EVGA['date']
        PlotlyWidgetData.y = EVGA['close']  

stocks = widgets.Dropdown(
    description='Stocks:   ',
    options=['','Intel','Nvidia','AMD','Gigabyte','Asus','MSI','EVGA'])

stocks.observe(response, type='change', names=['value'])

stocks