[英]plotly - fail to plot bar chart in offline mode with bar_mpl()
[英]How to draw bar chart using Plotly Offline mode in python?
我有一些功能和值,例如:
food 3.4
service 4.2
environment 4.3
我想使用Plotly離線模式繪制條形圖(而不是注冊和認證)。 到目前為止,我已經有了使用Plotly的離線模式繪制散線的代碼:
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"
)
})
這段代碼將打開一個HTML頁面並繪制一條零線。 如何修改它以繪制條形圖?
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])
]
})
要以離線模式繪制,請使用:
# 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)'))
})
要繪制堆疊圖或組圖,請參考教程: https : //github.com/SayaliSonawane/Plotly_Offline_Python/tree/master/Bar%20Chart
使用最新的可打印版本(我在4.1.0
),它比以往任何時候都容易。
import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
fig.show()
如果您想在Jupyterlab和網絡瀏覽器之間進行切換,則可以在開始時使用import plotly.io as pio
和pio.renderers.default = 'jupyterlab
或pio.renderers.default = 'browser'
。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.