python使用Plotly繪圖工具繪制散點(diǎn)圖、線形圖
今天在研究Plotly繪制散點(diǎn)圖的方法,供大家參考,具體內(nèi)容如下
使用Python3.6 + Plotly
Plotly版本2.0.0
在開始之前先說(shuō)說(shuō),還需要安裝庫(kù)Numpy,安裝方法在我的另一篇博客中有寫到:python3.6下Numpy庫(kù)下載與安裝圖文教程
因?yàn)镻lotly沒(méi)有自己獨(dú)立的線性圖形函數(shù),所以把線性圖形與散點(diǎn)圖形全部用一個(gè)函數(shù)實(shí)現(xiàn)
這個(gè)函數(shù)是Scatter函數(shù)
下面舉幾個(gè)簡(jiǎn)單的例子
先畫一個(gè)純散點(diǎn)圖,代碼如下:
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 #上面是一些隨機(jī)數(shù)據(jù) trace0 = go.Scatter( x = random_x, y = random_y0, mode = 'markers', # 繪制純散點(diǎn)圖 name = 'markers' # 圖例名稱 ) data = [trace0] pyplt(data, filename='tmp/scatter_diagram.html')#html放置的位置
運(yùn)行程序會(huì)得到如下圖所示圖形

接下來(lái)我們畫一個(gè)線性圖,數(shù)據(jù)還是之前的數(shù)據(jù)??纯词鞘裁礃幼樱a如下
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')
我們會(huì)得到如下圖所示的線形圖

下面我們把線性圖,和散點(diǎn)圖合到一起
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', # 散點(diǎn)+線的繪圖 name = 'lines+markers' ) data = [trace1] pyplt(data, filename='tmp/add.html')
得到如下圖所示圖例

三個(gè)圖在一張圖中表示的例子
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', # 純散點(diǎn)的繪圖 name = 'markers' # 曲線名稱 ) trace1 = go.Scatter( x = random_x, y = random_y1, mode = 'lines+markers', # 散點(diǎn)+線的繪圖 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')
得到如下圖

可以看到,三個(gè)圖,繪制在一張圖上了!
也可以對(duì)樣式進(jìn)行設(shè)置下面看個(gè)例子,改變一下顏色,代碼如下:
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', # 純散點(diǎn)圖 name = 'markers', # 曲線名稱 marker = dict( size = 10, # 設(shè)置點(diǎn)的寬度 color = 'rgba(255, 182, 193, .9)', #設(shè)置曲線的顏色 line = dict( width = 2, # 設(shè)置線條的寬度 color = 'rgb(0, 255, 0)' #設(shè)置線條的顏色 ) ) ) data = [trace0] pyplt(data, filename='tmp/style.html')

marker的參數(shù)設(shè)置很重要,設(shè)置顏色color,大小size
line設(shè)置線條寬度width,color 設(shè)置線條顏色等
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 用 xlwings 庫(kù) 生成圖表的操作方法
這篇文章主要介紹了python 用 xlwings 庫(kù) 生成圖表的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
windows11環(huán)境安裝django項(xiàng)目GNU gettext工具的步驟
Django 框架具有很好的 I18N 和 L10N 的支持,其實(shí)現(xiàn)是基于 GNU 的 gettext,本文主要介紹了windows11環(huán)境安裝django項(xiàng)目GNU gettext工具的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Python利用Pandas進(jìn)行數(shù)據(jù)分析的方法詳解
Pandas是最流行的用于數(shù)據(jù)分析的?Python?庫(kù)。它提供高度優(yōu)化的性能。本文將利用Python進(jìn)行數(shù)據(jù)分析,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
簡(jiǎn)單學(xué)習(xí)Python多進(jìn)程Multiprocessing
這篇文章主要和大家一起簡(jiǎn)單的學(xué)習(xí)Python多進(jìn)程Multiprocessing ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解
這篇文章主要介紹了Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過(guò)python對(duì)數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09
Python+QTimer計(jì)時(shí)器實(shí)現(xiàn)攝像頭視頻的播放和暫停
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)QTimer計(jì)時(shí)器實(shí)現(xiàn)攝像頭視頻的播放和暫停功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法
下面小編就為大家?guī)?lái)一篇python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

