如何在 python 中使用 Plotly Offline 模式绘制条形图?
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()
我有一些特点和价值观,例如:
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()