1. 程式人生 > 程式設計 >Python 視覺化神器Plotly詳解

Python 視覺化神器Plotly詳解

Python 視覺化神器Plotly詳解

文 |潮汐

來源:Python 技術「ID: pythonall」

學習Python是做數分析的最基礎的一步,資料分析離不開資料視覺化。Python第三方庫中我們最常用的視覺化庫是 pandas,matplotlib,pyecharts, 當然還有 Tableau,另外最近在學習過程中發現另一款視覺化神器-Plotly,它是一款用來做資料分析和視覺化的線上平臺,功能非常強大,可以線上繪製很多圖形比如條形圖、散點圖、餅圖、直方圖等等。除此之外,它還支援線上編輯,以及多種語言 python、javascript、matlab、R等許多API。它在python中使用也非常簡單,直接用pip install plotly

安裝好即可使用。本文將結合 plotly 庫在 jupyter notebook 中來進行圖形繪製。

使用 Plotly 可以畫出很多媲美Tableau的高質量圖,如下圖所示:

Python 視覺化神器Plotly詳解

Python 視覺化神器Plotly詳解

折線點圖

折現點圖畫圖步驟如下:首先在 Pycharm 介面輸入 jupyter notebook後進入網頁編輯介面,新建一個檔案,匯入相應的包即可進行圖形繪製:

# import pkg
from plotly.graph_objs import Scatter,Layout
import plotly
import plotly.offline as py
import numpy as np
import plotly.graph_objs as go
#設定編輯模式
plotly.offline.init_notebook_mode(connected=True)
#製作折線圖
N = 150
random_x = np.linspace(0,1,N)
random_y0 = np.random.randn(N)+7
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-7
 
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,trace2]
py.iplot(data)

顯示結果如下:

Python 視覺化神器Plotly詳解

直方圖

# 直方圖
trace0 = go.Bar(
  x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],y = [20,15,25,16,18,28,19,67,12,56,14,27],name = 'Primary Product',marker=dict(
    color = 'rgb(49,130,189)'
  )
)
trace1 = go.Bar(
  x = ['Jan',y = [29,32,10,82,16],name = 'Secondary Product',marker=dict(
    color = 'rgb(204,204,204)'
  )
)
data = [trace0,trace1]
py.iplot(data)

顯示結果如下:

Python 視覺化神器Plotly詳解

散點圖

# 散點圖
trace1 = go.Scatter(
   y = np.random.randn(700),marker = dict(
    size = 16,color = np.random.randn(800),colorscale = 'Viridis',showscale = True
  )
)
data = [trace1]
py.iplot(data)

顯示結果如下:

Python 視覺化神器Plotly詳解

總結

今天的文章主要學習視覺化神器-plotpy 的相關操作,希望在平時的工作中有所應用。更多的內容詳見 https://plotly.com/python/

到此這篇關於Python 視覺化神器Plotly詳解的文章就介紹到這了,更多相關Python 視覺化神器Plotly內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!