python使用Plotly繪圖工具繪制散點(diǎn)圖、線形圖
今天在研究Plotly繪制散點(diǎn)圖的方法,供大家參考,具體內(nèi)容如下
使用Python3.6 + Plotly
Plotly版本2.0.0
在開始之前先說說,還需要安裝庫Numpy,安裝方法在我的另一篇博客中有寫到:python3.6下Numpy庫下載與安裝圖文教程
因為Plotly沒有自己獨(dú)立的線性圖形函數(shù),所以把線性圖形與散點(diǎn)圖形全部用一個函數(shù)實現(xiàn)
這個函數(shù)是Scatter函數(shù)
下面舉幾個簡單的例子
先畫一個純散點(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)行程序會得到如下圖所示圖形
接下來我們畫一個線性圖,數(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')
我們會得到如下圖所示的線形圖
下面我們把線性圖,和散點(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')
得到如下圖所示圖例
三個圖在一張圖中表示的例子
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')
得到如下圖
可以看到,三個圖,繪制在一張圖上了!
也可以對樣式進(jìn)行設(shè)置下面看個例子,改變一下顏色,代碼如下:
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è)置線條顏色等
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
windows11環(huán)境安裝django項目GNU gettext工具的步驟
Django 框架具有很好的 I18N 和 L10N 的支持,其實現(xiàn)是基于 GNU 的 gettext,本文主要介紹了windows11環(huán)境安裝django項目GNU gettext工具的步驟,具有一定的參考價值,感興趣的可以了解一下2024-04-04Python利用Pandas進(jìn)行數(shù)據(jù)分析的方法詳解
Pandas是最流行的用于數(shù)據(jù)分析的?Python?庫。它提供高度優(yōu)化的性能。本文將利用Python進(jìn)行數(shù)據(jù)分析,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09簡單學(xué)習(xí)Python多進(jìn)程Multiprocessing
這篇文章主要和大家一起簡單的學(xué)習(xí)Python多進(jìn)程Multiprocessing ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解
這篇文章主要介紹了Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05利用python對月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過python對數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09Python+QTimer計時器實現(xiàn)攝像頭視頻的播放和暫停
這篇文章主要為大家詳細(xì)介紹了Python如何通過QTimer計時器實現(xiàn)攝像頭視頻的播放和暫停功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法
下面小編就為大家?guī)硪黄猵ython 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06