Plotly: How to display charts in Spyder?

后端 未结 2 1264
误落风尘
误落风尘 2020-12-03 21:55

Since November 2015, plotly is Open-Source and available for python. https://plot.ly/javascript/open-source-announcement/

When trying to do some plots offline, these

相关标签:
2条回答
  • 2020-12-03 22:13

    importing plot instead iplot (and changing the last line from iplot(fig) to plot(fig) resolved the problem, at least in python 3:

    from plotly.offline import download_plotlyjs, init_notebook_mode,  plot
    from plotly.graph_objs import *
    init_notebook_mode()
    
    trace0 = Scatter(
        x=[1, 2, 3, 4],
        y=[10, 11, 12, 13],
        mode='markers',
        marker=dict(
            size=[40, 60, 80, 100],
        )
    )
    data = [trace0]
    layout = Layout(
        showlegend=False,
        height=600,
        width=600,
    )
    
    fig = dict( data=data, layout=layout )
    
    plot(fig)  
    

    But instead you could do the following, which is slightly easier:

    import plotly
    import plotly.graph_objs
    plotly.offline.plot({
    "data": [
        plotly.graph_objs.Scatter(    x=[1, 2, 3, 4],
        y=[10, 11, 12, 13], mode='markers',
        marker=dict(
            size=[40, 60, 80, 100]))],
    "layout": plotly.graph_objs.Layout(showlegend=False,
        height=600,
        width=600,
    )
    })
    
    0 讨论(0)
  • 2020-12-03 22:32

    If you'd like to develop your plotly figures in Spyder, perhaps because of Spyders superb variable explorer, you can easily display a non-interactive image by just running fig.show(). Note that this is for newer versions of plotly where you don't have to worry about iplot and plotly.offline. If you'd like to send your figure to the browser for an interactive version, just run:

    import plotly.io as pio
    
    pio.renderers.default='browser'
    

    Now your figure will be displayed in your default browser

    To switch back to Spyder, just run:

    import plotly.io as pio
    
    pio.renderers.default='svg'
    

    You can check other options too usingpio.renderers?:

    Renderers configuration
    -----------------------
    Default renderer: 'svg'
    Available rendere <...> wser', 'firefox', 'chrome', 'chromium', 'iframe',
    'iframe_connected', 'sphinx_gallery']
    

    You'll find even more details here under Setting the default renderer

    Here's a detailed example

    Code:

    import plotly.graph_objects as go
    
    import plotly.io as pio
    #pio.renderers.default = 'svg'
    pio.renderers.default = 'browser'
    
    x = ['Product A', 'Product B', 'Product C']
    y = [20, 14, 23]
    
    fig = go.Figure(data=[go.Bar(
                x=x, y=y,
                text=y,
                textposition='auto',
            )])
    fig.show()
    

    Plot:

    System info:

    Python 3.7.6
    Spyder 3.3.1
    Plotly 3.2.0
    
    0 讨论(0)
提交回复
热议问题