简体   繁体   中英

How to draw bar chart using Plotly Offline mode in python?

I have some features and values like:

food        3.4   
service     4.2  
environment 4.3 

and I want to draw a bar chart using Plotly offline mode (not registering and authenticating). So far I have the code for drawing a scattered line using Plotly's offline mode:

import plotly
print (plotly.__version__) 
from plotly.graph_objs import Scatter, Layout
plotly.offline.plot({
"data": [
    Scatter(x=[1, 2, 3, 4], y=[4, 1, 3, 7])
],
"layout": Layout(
    title="hello world"
)
})

This code opens an HTML page and draws a scattered line. How to modify it so it draws a bar chart?

import plotly
import plotly.graph_objs

plotly.offline.plot({
"data": [
    plotly.graph_objs.Bar(x=['food','service','environment'],y=[3.4,4.2,4.3])
]
})

To plot it in offline mode use:

# Import package 
import plotly 
# Use init_notebook_mode() to view the plots in jupyter notebook
plotly.offline.init_notebook_mode()
from plotly.graph_objs import Scatter,Layout,Bar

trace1 = Bar(x=['food','service','environment'],y=[3.4,4.2,4.3])

# Create chart 
plotly.offline.iplot({
                         "data": [
                                  trace1
                                  ], 
                        "layout": Layout(title="<b>Sample_Title</b>",xaxis= dict(
                                                        title= '<b>X axis label</b>',
                                                        zeroline= False,
                                                        gridcolor='rgb(183,183,183)',
                                                        showline=True
                                                    ),
                                                    yaxis=dict(
                                                        title= '<b>Y axis Label</b>',
                                                        gridcolor='rgb(183,183,183)',
                                                        zeroline=False,
                                                        showline=True
                                                    ),font=dict(family='Courier New, monospace', size=12, color='rgb(0,0,0)'))
                                  })

To plot stacked or group chart refer tutorial: https://github.com/SayaliSonawane/Plotly_Offline_Python/tree/master/Bar%20Chart

With recent plotly versions (I'm on 4.1.0 ) it's easier than ever.

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
fig.show()

在此处输入图片说明

I'f you'd like to switch between let's say Jupyterlab and a web browser, you can set this in the beginning using import plotly.io as pio and pio.renderers.default = 'jupyterlab or pio.renderers.default = 'browser' , respectively.

import plotly.graph_objects as go
import plotly.io as pio

pio.renderers.default = 'browser'

animals=['giraffes', 'orangutans', 'monkeys']
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM