pip install plotly
可以使用pandas方法导入csv文件数据 此处不再介绍 如有需要可查询上一篇 此篇介绍常用的图形绘制
# 绘制折线图
import plotly
import os
import plotly as py
import plotly.graph_objs as go
from plotly.graph_objs import Scatter
trace0 = Scatter(x=[1, 2, 3, 4], y=[12, 5, 8, 23])
trace1 = Scatter(x=[1, 2, 3, 4], y=[16, 8, 12, 6])
py.offline.plot([trace0, trace1])
代码很简单只不过数据是自己随便写的有些乱 各个参数已经在代码注释
import plotly as py
import plotly.graph_objs as go
from plotly.graph_objs import Scatter
trace1 = Scatter(x=[2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014],
y=[2, 4, 6, 8, 10, 8, 9, 5, 8, 12], mode='lines', name='历年生产总值',
line=dict(width=2, color='red')) # mode为绘制的图形样式 lines为折线图,markers为浮点图,line里面为宽度和颜色
trace2 = Scatter(x=[2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014],
y=[12, 14, 13, 18, 14, 25, 16, 20, 12, 29], mode='lines', name='历年投资',
line=dict(width=2, color='blue'))
trace3 = Scatter(x=[2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014],
y=[20, 24, 18, 28, 21, 34, 32, 16, 29, 38], mode='lines', name="历年收入",
line=dict(width=2, color='green'))
data = [trace1, trace2, trace3]
layout = go.Layout(title='财政收入变化', xaxis=dict(title='年份'), yaxis=dict(title='每年销售总额(千万)'),
legend=dict(x=1, y=1, font=dict(size=10, color='black')))# 设置图形标题,以及x轴y轴标题 legend为 三个name的样式 x=1y=1 是三个name的位置
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig)
import plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go
trace = [go.Bar(x=['河南', '河北', '北京', '湖南', '天津', '安徽'], y=[3456, 4568, 7865, 6444, 3900, 5960],
marker=dict(color=['red', 'yellow', 'blue', 'green', 'darkgray', 'orange']), opacity=0.99, width=0.5)]
layout = go.Layout(title='不同地区的订单数量', xaxis=dict(title='地区'))
fig = go.Figure(data=trace, layout=layout)
py.offline.plot(fig)
import plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go
trace1 = go.Bar(x=['河南', '河北', '北京', '湖南', '天津', '安徽'], y=[3456, 4568, 7865, 6444, 3900, 5960], name="历年教育投资")
trace2 = go.Bar(x=['河南', '河北', '北京', '湖南', '天津', '安徽'], y=[2378, 3568, 8865, 7444, 6900, 2960], name="历年农业投资")
trace3 = go.Bar(x=['河南', '河北', '北京', '湖南', '天津', '安徽'], y=[6456, 2568, 3865, 5444, 8900, 1960], name="历年财政收入")
data = [trace1, trace2, trace3]
layout = go.Layout(title='各地区经济情况', xaxis=dict(title='地区'),legend=dict(x=1, y=1, font=dict(size=10, color='black'))) # 加入barmode='stack' 绘制堆叠柱状图
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig)
import plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go
data = [go.Histogram(x=[23, 45, 65, 87, 23, 64, 8, 97, 34, 39, 80, 12, 28, 55, 58, 77, 79, 76], nbinsx=10,
histnorm='probability',
marker=dict(color='orange'))]
py.offline.plot(data)
import plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go
trace = [go.Pie(labels=['河南', '河北', '北京', '湖南', '天津', '安徽'], values=[3456, 4568, 7865, 6444, 3900, 5960], hole=0.3,
textfont=dict(size=10, color='red'))]
layout = go.Layout(title='不同地区的教育支出(万元)')
fig = go.Figure(data=trace, layout=layout)
py.offline.plot(fig)