首页 > 解决方案 > go.Histogram2dContour 从 plotly 显示不同的图表类型

问题描述

我正在尝试使用带有下拉菜单的 plotly 的 Histogram2dContour 绘图函数在烧瓶应用程序中显示 2d 密度图。我使用此处的基本示例制作的等高线图。相反,我得到一个连接的散点图。图上的点是正确的。

我从下面的按钮创建函数中的 args 中删除了可见参数。我还尝试更改 plots 函数中的参数,但没有运气,

    def kde_chart(self, chart_type, data_columns_list, df):
        '''
        Creates 2d kde denisty plot with pair combinations of columns from dataframes that can be
        selected from a dropdown within the plotly visualization.
        :param chart_type: string KDE
        :param data_columns_list: data columns from dataframe in database
        :param df: dataframe used in database
        :return:
        '''

        if chart_type == 'KDE':

            # create pairs of columns with combinations
            kde_columns = [column for column in data_columns_list if df[column].dtype == 'float']
            kde_columns_combinations = combinations(kde_columns, 2)
            kde_columns = [pair for pair in kde_columns_combinations]

            # create traces
            traces = [self._kde_chart_maker(pair, df) for pair in kde_columns]

            # create updatemenus
            updatemenus = list([dict(
                        active=-1,
                        buttons=[self._kde_updatemenu_button_maker(pair, traces[i])
                                for i, pair in enumerate(kde_columns)])])

            # create plot
            kde_plot = dict(data=[traces],
                            layout=dict(
                                title='KDE plot',
                                showlegend=False,
                                autosize=False,
                                width=800,
                                height=550,
                                updatemenus=updatemenus,
                                hovermode='closest'
                                )
                            )

            graphJSON = json.dumps(kde_plot, cls=plotly.utils.PlotlyJSONEncoder)

        return graphJSON

    def _kde_chart_maker(self, data_column_pair, df):
        # create contours
        trace = go.Histogram2dContour(
            x=df[data_column_pair[0]],
            y=df[data_column_pair[1]],
            #name='density',
            ncontours=20,
            colorscale='Hot',
            reversescale=True,
            showscale=True
        )

        return trace

    def _kde_updatemenu_button_maker(self, kde_pair, trace_selected):
        button = dict(label=kde_pair[0] + '/' + kde_pair[1],
                      method='update',
                      args=[dict(
                          x=[trace_selected['x']],
                          y=[trace_selected['y']],
                          title=kde_pair[0] + '/' + kde_pair[1])])
        return button

未显示任何错误消息。

标签: pythonflaskplotly

解决方案


没关系,我找到了解决方案。我看到了错误,然后重构了代码以适应输入数据的修改形式。


推荐阅读