您的位置:首页 > 脚本大全 > > 正文

python绘折线图数据(python2.7使用plotly绘制本地散点图和折线图)

更多 时间:2021-10-23 10:04:09 类别:脚本大全 浏览量:2138

python绘折线图数据

python2.7使用plotly绘制本地散点图和折线图

本人在学习使用python和plotly处理数据时,经过两个小时艰难试错,终于完成了散点图和折线图的实例。在使用过程中遇到一个大坑,因为官方给出的案例是用在线存储的,所以需要安装jupyter(也就是ipython)才能使用notebook来处理生成的文件,一开始我没太懂iplot和plot之间的差异,导致浪费了很多时间。

重要提示:最新的jupyter不支持python3.2及以下版本

python绘折线图数据(python2.7使用plotly绘制本地散点图和折线图)

最后我只能继续采用本地文件的形式来解决这个问题了。下面放出我的测试代码,被注释掉的是官方给出的代码以及离线存储的代码。应该是最新版的python的方案。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • #!/usr/bin/python
  • # coding=utf-8
  •  
  • import plotly.plotly
  • import random
  • from plotly.graph_objs import *
  • import plotly.graph_objs as abc # 必须
  • import numpy as np
  •  
  •  
  • def sayhello():
  •  n=100
  •  xx = [];
  •  for i in range(20):
  •   xx.append(i)
  •  y0 = [];
  •  for i in range(20):
  •   y0.append(random.randint(0, 10))
  •  y1 = [];
  •  for i in range(20):
  •   y1.append(random.randint(10, 20))
  •  y2 = [];
  •  for i in range(20):
  •   y2.append(random.randint(20, 30))
  •  #xx = np.linspace(0, 1, n)
  •  #y0 = np.random.randn(n) + 5
  •  #y1 = np.random.randn(n)
  •  #y2 = np.random.randn(n) - 5
  •  data_1 = abc.scatter(
  •   x=xx,
  •   y=y0,
  •   name='test1',
  •   mode='markers'
  •  )
  •  date_2 = abc.scatter(
  •   x=xx,
  •   y=y1,
  •   name='test2',
  •   mode="lines"
  •  )
  •  date_3 = abc.scatter(
  •   x=xx,
  •   y=y2,
  •   name='test3',
  •   mode="lines+markers"
  •  )
  •  '''
  •  n = 1000
  •  random_x = np.random.randn(n)
  •  random_y = np.random.randn(n)
  •  # create a trace
  •  trace = abc.scatter(
  •   x=random_x,
  •   y=random_y,
  •   mode='markers'
  •  )
  •  data1 = [trace]
  •  '''
  •  data1 = data([data_1, date_2,date_3])
  •  plotly.offline.plot(data1)
  •  #plotly.offline.iplot(data1,filename='test01')
  •  
  •  
  • if __name__ == "__main__":
  •  sayhello()
  • 下面是我最终结果的截图:

    python绘折线图数据(python2.7使用plotly绘制本地散点图和折线图)

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。

    原文链接:https://blog.csdn.net/Fhaohaizi/article/details/78754423

    您可能感兴趣