【问题标题】:How to fix plotly graph that shows data when in plotly but appears empty when run with dash?如何修复在绘图时显示数据但在使用破折号运行时显示为空的绘图图?
【发布时间】:2019-07-04 06:46:07
【问题描述】:

我在破折号中设置了一个dcc.Graph() 对象,并将该图设置为我在plotly 中创建的散点图。

该图完全按照我在plotly 中的方式显示其所有数据,但是当我运行本地服务器并查看破折号中的图时,除了轴和刻度标签外,所有内容都是空白的。

我尝试了以下方法 - 编辑布局中的宽度和高度 - 创建了一个只有图形对象的仪表板服务器 - 单独运行图表 - 查看 plotly 主页上的图形表示

其他图表的数据有效并显示在同一个破折号中,但不是这个。

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
from sklearn import linear_model

def create_tile_scatter_plot_figure():
    def data_to_plotly(x):
        k = []
        for i in range(0, len(x)):
            k.append(x[i][0])
        return k

    tiles = ['ALAÏA Vienne Laser-Cut Leather Tote',
             'AZZEDINE ALAÏA LASER-CUT KNITTED DRESS',
             'NEOPRENE SNEAKERS',
             'GRIP JACKET BLACK',
             'ORBIT PANT BLACK',
             'BLACK SHELL, MULTI FOX FUR',
             'LEATHER JACKET', 'LAUREN RALPH LAUREN Turtleneck Sweater',
             'Edwart Paris',
             'SG VARSITY POM BEANIE DARK BLUE',
             'Pro Longwear Foundation',
             'CHOCOLATE EYE PALETTE',
             'ALAÏA Laser-Cut Ankle Boots',
             'Mac 12 Lash',
             'Bobby Brown BUFF A Beige Pink Lipgloss',
             'ALAÏA Bracelet Leather Bucket Bag',
             'Bar 8 Mandarin Oriental Hotel',
             'Mandarin Hotel Paris', 'Grand Palais Paris']
    x_data = np.array([i + 1 for i in range(len(tiles))]).reshape(-1, 1)
    y_data = np.array([383, 367, 320, 318, 327, 420, 377, 303, 283, 302, 264, 257, 296,
                       317, 335, 302, 292, 297, 264]).reshape(-1, 1)
    tickvals = [i + 1 for i in range(len(tiles))]

    regr = linear_model.LinearRegression().fit(x_data, y_data)

    p1 = go.Scatter(x=data_to_plotly(x_data),
                    y=y_data,
                    mode='markers',
                    marker=dict(color='black')
                    )

    p2 = go.Scatter(x=data_to_plotly(x_data),
                    y=regr.predict(x_data),
                    mode='lines',
                    line=dict(color='blue', width=3)
                    )

    layout = go.Layout(title='Engagement by Tile',
                       paper_bgcolor='rgba(0,0,0,0)',
                       plot_bgcolor='rgba(0,0,0,0)',
                       width=1300,
                       height=800,
                       xaxis=dict(ticks='',
                                  ticktext=tiles,
                                  tickvals=tickvals,
                                  showgrid=False,
                                  showline=True,
                                  showticklabels=True,
                                  automargin=False,
                                  range=[0, len(tiles)+1]),
                       yaxis=dict(ticks='',
                                  showgrid=False,
                                  showline=True,
                                  showticklabels=False,
                                  automargin=True),
                       showlegend=False,
                       hovermode='closest')

    return go.Figure(data=[p1, p2], layout=layout)
app = dash.Dash(__name__)
app.layout = html.Div(id='main-container', children=[
    dcc.Graph(id='scatter-regression', figure=create_tile_scatter_plot_figure())]),
    ])
if __name__ == '__main__':
    app.run_server(debug=True)

我希望数据以破折号出现在图表上,但我看不到它。

【问题讨论】:

    标签: python plotly-dash plotly-python


    【解决方案1】:

    您的 y 坐标格式错误。我建议你在你的分散创建中将它们展平,所以你改变:

    y=y_data
    

    y=y_data.flatten()
    

    y=regr.predict(x_data)
    

    y=regr.predict(x_data).flatten()
    

    【讨论】:

    • 非常感谢,解决了!非常感谢您的帮助
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2014-02-28
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2022-07-22
    相关资源
    最近更新 更多