Python使用Plotly绘图工具,绘制散点图、线形图
今天在研究Plotly绘制散点图的方法
使用Python3.6 + Plotly
Plotly版本2.0.0
在开始之前先说说,还需要安装库Numpy,安装方法在我的另一篇博客中有写到:https://www.cnblogs.com/ws17345067708/p/10531531.html
因为Plotly没有自己独立的线性图形函数,所以把线性图形与散点图形全部用一个函数实现
这个函数是Scatter函数
下面举几个简单的例子
先画一个纯散点图,代码如下:
import plotly
import plotly.graph_objs as go
import numpy pyplt = plotly.offline.plot #使用离线模式
N = 100
random_x = numpy.linspace(0, 1, N)
random_y0 = numpy.random.randn(N)+5
random_y1 = numpy.random.randn(N)
random_y2 = numpy.random.randn(N)-5
#上面是一些随机数据
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers', # 绘制纯散点图
name = 'markers' # 图例名称
)
data = [trace0]
pyplt(data, filename='tmp/scatter_diagram.html')#html放置的位置
运行程序会得到如下图所示图形

接下来我们画一个线性图,数据还是之前的数据。看看是什么样子,代码如下
import plotly
import plotly.graph_objs as go
import numpy pyplt = plotly.offline.plot #使用离线模式
N = 100
random_x = numpy.linspace(0, 1, N)
random_y0 = numpy.random.randn(N)+5
random_y1 = numpy.random.randn(N)
random_y2 = numpy.random.randn(N)-5
trace1 = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines', # 线性图
name = 'lines'
)
data = [trace1]
pyplt(data, filename='tmp/line.html')
我们会得到如下图所示的线形图

下面我们把线性图,和散点图合到一起
import plotly
import plotly.graph_objs as go
import numpy
pyplt = plotly.offline.plot #使用离线模式
N = 100
random_x = numpy.linspace(0, 1, N)
random_y0 = numpy.random.randn(N)+5
random_y1 = numpy.random.randn(N)
random_y2 = numpy.random.randn(N)-5
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode = 'lines+markers', # 散点+线的绘图
name = 'lines+markers'
)
data = [trace1]
pyplt(data, filename='tmp/add.html')
得到如下图所示图例

三个图在一张图中表示的例子
import plotly
import plotly.graph_objs as go
import numpy
pyplt = plotly.offline.plot #使用离线模式
N = 100
random_x = numpy.linspace(0, 1, N)
random_y0 = numpy.random.randn(N)+5
random_y1 = numpy.random.randn(N)
random_y2 = numpy.random.randn(N)-5
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers', # 纯散点的绘图
name = 'markers' # 曲线名称
)
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode = 'lines+markers', # 散点+线的绘图
name = 'lines+markers'
)
trace2 = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines', # 线的绘图
name = 'lines'
)
data = [trace0,trace1,tarace2]
pyplt(data, filename='tmp/all.html')
得到如下图

可以看到,三个图,绘制在一张图上了!
也可以对样式进行设置下面看个例子,改变一下颜色,代码如下:
import plotly
import plotly.graph_objs as go
import numpy
pyplt = plotly.offline.plot #使用离线模式
N = 100
random_x = numpy.linspace(0, 1, N)
random_y0 = numpy.random.randn(N)+5
random_y1 = numpy.random.randn(N)
random_y2 = numpy.random.randn(N)-5
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers', # 纯散点图
name = 'markers', # 曲线名称
marker = dict(
size = 10, # 设置点的宽度
color = 'rgba(255, 182, 193, .9)', #设置曲线的颜色
line = dict(
width = 2, # 设置线条的宽度
color = 'rgb(0, 255, 0)' #设置线条的颜色
)
)
)
data = [trace0]
pyplt(data, filename='tmp/style.html')

marker的参数设置很重要,设置颜色color,大小size
line设置线条宽度width,color 设置线条颜色等
Python使用Plotly绘图工具,绘制散点图、线形图的更多相关文章
- Python使用Plotly绘图工具,绘制面积图
今天我们来讲一下如何使用Python使用Plotly绘图工具,绘制面积图 绘制面积图与绘制散点图和折线图的画法类似,使用plotly graph_objs 中的Scatter函数,不同之处在于面积图对 ...
- Python使用Plotly绘图工具,绘制直方图
今天我们再来讲解一下Python使用Plotly绘图工具如何绘制直方图 使用plotly绘制直方图需要用到graph_objs包中的Histogram函数 我们将数据赋值给函数中的x变量,x = da ...
- Python使用Plotly绘图工具,绘制饼图
今天我们来学习一下如何使用Python的Plotly绘图工具,绘制饼图 使用Plotly绘制饼图的方法,我们需要使用graph_objs中的Pie函数 函数中最常用的两个属性values,用于赋值给需 ...
- Python使用Plotly绘图工具,绘制气泡图
今天来讲讲如何使用Python 绘图工具,Plotly来绘制气泡图. 气泡图的实现方法类似散点图的实现.修改散点图中点的大小,就变成气泡图. 实现代码如下: import plotly as py i ...
- Python使用Plotly绘图工具,绘制甘特图
今天来讲一下如何使用Python 的绘图工具Plotly来绘制甘特图的方法 甘特图大家应该了解熟悉,就是通过条形来显示项目的进度.时间安排等相关情况的. 我们今天来学习一下,如何使用ployly来绘制 ...
- Python使用Plotly绘图工具,绘制水平条形图
水平条形图与绘制柱状图类似,大家可以先看看我之前写的博客,如何绘制柱状图 水平条形图需要在Bar函数中设置orientation= 'h' 其他的参数与柱状图相同.也可以通过设置barmode = ' ...
- Python使用Plotly绘图工具,绘制柱状图
使用Plotly绘制基本的柱状图,需要用到的函数是graph_objs 中 Bar函数 通过参数,可以设置柱状图的样式. 通过barmod进行设置可以绘制出不同类型的柱状图出来. 我们先来实现一个简单 ...
- 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
2.使用工厂方法模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
- 1、使用简单工厂模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程
1.使用简单工厂模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
随机推荐
- MyEclipse设置Console输出到文件
Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置: 在JAVA程序上右键--> Run As --> Run Configuration ...
- [Swift]LeetCode817. 链表组件 | Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- [Swift]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- HBase之Table.put客户端流程(续)
上篇博文中已经谈到,有两个流程没有讲到.一个是MetaTableAccessor.getRegionLocations,另外一个是ConnectionImplementation.cacheLocat ...
- nodejs接收get参数和post参数
get请求用query //http://localhost:3000?a=3&b=4&c=5 router.get('/', function (req, res, next) { ...
- Linux驱动模块编译模板
hello.c文件: #include <linux/module.h> #include <linux/kernel.h> static int hello_init(voi ...
- linux磁盘管理系列一:磁盘配额管理
磁盘管理系列 linux磁盘管理系列一:磁盘配额管理 http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_040_quota.html l ...
- scrapy爬虫学习系列三:scrapy部署到scrapyhub上
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
- React Fiber 数据结构揭秘
此章节会通过两个 demo 来展示 Stack Reconciler 以及 Fiber Reconciler 的数据结构. 个人博客 首先用代码表示上图节点间的关系.比如 a1 节点下有 b1.b2. ...