簡體   English   中英

如何修復在繪圖中顯示數據但在使用破折號運行時顯示為空的繪圖圖形?

[英]How to fix plotly graph that shows data when in plotly but appears empty when run with dash?

我在破折號中設置dcc.Graph()對象,並將圖形設置為在plotly創建的plotly

該圖完全按照我在plotly顯示的方式顯示其所有數據,但是當我運行本地服務器並以破折號查看該圖時,除坐標軸和刻度標簽外,其他所有內容均為空白。

我嘗試了以下方法-編輯布局中的寬度和高度-僅使用圖形對象創建了破折號服務器-僅運行圖形-在pallyly的主頁上查看了圖形表示

其他圖形的數據在相同的破折號中工作並顯示,但不是該破折號。

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)

我希望數據以短划線顯示在圖表上,但無法看到它。

您的y坐標格式錯誤。 我建議您在散點圖創建中將它們展平,因此您可以進行以下更改:

y=y_data

y=y_data.flatten()

y=regr.predict(x_data)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM