时间:2022-08-11 11:27:24 | 栏目:Python代码 | 点击:次
首先创建一个新的python3记录,之后在开始位置输入以下语句并执行:
import plotly import plotly.offline as py py.init_notebook_mode(connected=False) import plotly.graph_objs as go
注意此时实际上是将plotly的库文件写在了ipynb文件内部,因此保存后的ipynb文件会比较大,一般在5M以上.
补充知识:plotly 绘制离线图例(折线)
我就废话不多说了,还是直接看代码吧!
#log.txt 1 9 15 2 9 16 1 10 17 2 10 18 1 9 19
#!/usr/bin/env python import plotly.offline as pltoff import plotly.graph_objs as go def line_plots(name="line_plots.html"): dataset = { 'x': [], 'y1': [], 'y2': [], 'y3': [] } with open("./log.txt") as f: i = 0 for line in f: items = line.split() dataset['x'].append(i) dataset['y1'].append(items[0]) dataset['y2'].append(items[1]) dataset['y3'].append(items[2]) i += 1 data_g = [] # 构建 数据关系,折线图 x_y1 = go.Scatter( x=dataset['x'], y=dataset['y1'], mode='lines', name='lines') data_g.append(x_y1) x_y2 = go.Scatter( x=dataset['x'], y=dataset['y2'], mode='markers', name='markers') data_g.append(x_y2) x_y3 = go.Scatter( x=dataset['x'], y=dataset['y3'], mode='lines+markers', name='lines+markers') data_g.append(x_y3) # 设置图表布局 layout = go.Layout(title="Line plots", xaxis={'title': 'X'}, yaxis={'title': 'Y'}) fig = go.Figure(data=data_g, layout=layout) # 生成离线html pltoff.plot(fig, filename=name) if __name__ == '__main__': line_plots()