python 數據可視化利器

2021-03-02 zone7

閱讀本文大約需要 12 分鐘

概述前言

前段時間有讀者向我反映,想看看數據可視化方面的文章,這不?現在就開始寫了,如果你想看哪些方面的文章,可以通過留言或者後臺告訴我。數據可視化的第三方庫挺多的,這裡我主要推薦兩個,分別是 bokeh、pyecharts。如果我的文章對你有幫助,歡迎關注、點讚、轉發,這樣我會更有動力做原創分享。

bokeh

這裡展示一下常用的圖表,詳細的文檔可查看(https://bokeh.pydata.org/en/latest/docs/user_guide/categorical.html)

條形圖條形圖

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure
output_file("colormapped_bars.html")
fruits = ['Apples', '魅族', 'OPPO', 'VIVO', '小米', '華為'] 
counts = [5, 3, 4, 2, 4, 6] 
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))
p = figure(x_range=fruits, y_range=(0,9), plot_height=250, title="Fruit Counts",
           toolbar_location=None, tools="")
p.vbar(x='fruits', top='counts', width=0.9, color='color', legend="fruits", source=source)
p.xgrid.grid_line_color = None 
p.legend.orientation = "horizontal" 
p.legend.location = "top_center"
show(p) 

年度條形圖年度條形圖

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
output_file("bars.html")
fruits = ['Apples', '魅族', 'OPPO', 'VIVO', '小米', '華為']
years = ['2015', '2016', '2017']
data = {'fruits': fruits,
        '2015': [2, 1, 4, 3, 2, 4],
        '2016': [5, 3, 3, 2, 4, 6],
        '2017': [3, 2, 4, 4, 5, 3]}
x = [(fruit, year) for fruit in fruits for year in years]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ())  
source = ColumnDataSource(data=dict(x=x, counts=counts))
p = figure(x_range=FactorRange(*x), plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")
p.vbar(x='x', top='counts', width=0.9, source=source)
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)

餅圖餅圖

from collections import Counter
from math import pi
import pandas as pd
from bokeh.io import output_file, show
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.transform import cumsum
output_file("pie.html")
x = Counter({ 
    '中國': 157,
    '美國': 93,
    '日本': 89,
    '巴西': 63,
    '德國': 44,
    '印度': 42,
    '義大利': 40,
    '澳大利亞': 35,
    '法國': 31,
    '西班牙': 29
})
data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(index=str, columns={0:'value', 'index':'country'})
data['angle'] = data['value']/sum(x.values()) * 2*pi
data['color'] = Category20c[len(x)]
p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips="@country: @value")
p.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend='country', source=data)
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None 
show(p)                                                                                                          

條形圖年度水果進出口

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import GnBu3, OrRd3
from bokeh.plotting import figure
output_file("stacked_split.html")
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
exports = {'fruits': fruits,
           '2015': [2, 1, 4, 3, 2, 4],
           '2016': [5, 3, 4, 2, 4, 6],
           '2017': [3, 2, 4, 4, 5, 3]}
imports = {'fruits': fruits,
           '2015': [-1, 0, -1, -3, -2, -1],
           '2016': [-2, -1, -3, -1, -2, -2],
           '2017': [-1, -2, -1, 0, -2, -2]}
p = figure(y_range=fruits, plot_height=250, x_range=(-16, 16), title="Fruit import/export, by year",
           toolbar_location=None)
p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
             legend=["%s exports" % x for x in years])
p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),
             legend=["%s imports" % x for x in years])
p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None
show(p)

散點圖散點圖

from bokeh.plotting import figure, output_file, show
output_file("line.html")
p = figure(plot_width=400, plot_height=400)
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
show(p)

六邊形圖六邊形圖

import numpy as np
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.util.hex import axial_to_cartesian
output_file("hex_coords.html")
q = np.array([0, 0, 0, -1, -1, 1, 1])
r = np.array([0, -1, 1, 0, 1, -1, 0])
p = figure(plot_width=400, plot_height=400, toolbar_location=None) 
p.grid.visible = False 
p.hex_tile(q, r, size=1, fill_color=["firebrick"] * 3 + ["navy"] * 4,
           line_color="white", alpha=0.5)
x, y = axial_to_cartesian(q, r, 1, "pointytop")
p.text(x, y, text=["(%d, %d)" % (q, r) for (q, r) in zip(q, r)],
       text_baseline="middle", text_align="center")
show(p)

元素周期表元素周期表pyecharts

pyecharts 也是一個比較常用的數據可視化庫,用得也是比較多的了,是百度 echarts 庫的 python 支持。這裡也展示一下常用的圖表。文檔地址為(http://pyecharts.org/#/zh-cn/prepare?id=%E5%AE%89%E8%A3%85-pyecharts)

條形圖條形圖

from pyecharts import Bar
bar = Bar("我的第一個圖表", "這裡是副標題")
bar.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])

bar.render()    

散點圖散點圖

from pyecharts import Polar
import random
data_1 = [(10, random.randint(1, 100)) for i in range(300)]
data_2 = [(11, random.randint(1, 100)) for i in range(300)]
polar = Polar("極坐標系-散點圖示例", width=1200, height=600)
polar.add("", data_1, type='scatter')
polar.add("", data_2, type='scatter')
polar.render()

餅圖餅圖

import random
from pyecharts import Pie
attr = ['A', 'B', 'C', 'D', 'E', 'F']
pie = Pie("餅圖示例", width=1000, height=600)
pie.add(
    "",
    attr,
    [random.randint(0, 100) for _ in range(6)],
    radius=[50, 55],
    center=[25, 50],
    is_random=True,
)
pie.add(
    "",
    attr,
    [random.randint(20, 100) for _ in range(6)],
    radius=[0, 45],
    center=[25, 50],
    rosetype="area",
)
pie.add(
    "",
    attr,
    [random.randint(0, 100) for _ in range(6)],
    radius=[50, 55],
    center=[65, 50],
    is_random=True,
)
pie.add(
    "",
    attr,
    [random.randint(20, 100) for _ in range(6)],
    radius=[0, 45],
    center=[65, 50],
    rosetype="radius",
)
pie.render()

詞雲詞雲

from pyecharts import WordCloud
name = ['Sam S Club'] 
value = [10000] 
wordcloud = WordCloud(width=1300, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
wordcloud.render()

樹圖樹圖後記

大概介紹就是這樣了,總體也不是很難,按照文檔來修改數據都能夠直接上手使用。主要是多練習。

公眾號「zone7」

相關焦點

  • python 數據可視化利器 plus
    python 數據可視化利器 plus概述前言推薦plotlybokehpyecharts後記前言更新:上一篇文章《python 數據可視化利器》中,我寫了 bokeh、pyecharts前段時間有讀者向我反映,想看看數據可視化方面的文章,這不?現在就開始寫了,如果你想看哪些方面的文章,可以通過留言或者後臺告訴我。數據可視化的第三方庫挺多的,這裡我主要推薦兩個,分別是 bokeh、pyecharts。如果我的文章對你有幫助,歡迎關注、點讚、轉發,這樣我會更有動力做原創分享。
  • Python 數據可視化利器
    (給Python開發者加星標,提升Python技能)作者:zone7(本文來自作者投稿,簡介見末尾)概述前言推薦plotlybokehpyecharts後記前言前段時間有讀者向我反映,想看看數據可視化方面的文章
  • 【Python教程】用Python進行數據可視化
    在本文中,我將介紹如何開始使用Python和matplotlib、seaborn兩個庫對數據進行可視化。通過上面的例子,我們應該可以感受到利用可視化能多麼美麗的展示數據。而且和其它語言相比,使用 Python 進行可視化更容易簡便一些。
  • 13分鐘,教你python可視化分析20W數據,找到妹子最愛的內衣
    一提到爬取,我們肯定先想到python,那可真的是利器,不過我提醒一句,不允許爬的千萬不要碰。python雖然爬取簡單,但是做可視化分析需要費不少力氣,雖然有 Matplotlib 和 Seaborn 兩個包就足夠了。
  • 推薦: 一本「高顏值」的Python語言數據可視化圖書
    另外,《R語言數據可視化之美》基於R中的ggplot2包及其拓展包等,系統性地介紹了幾乎所有常見的二維和三維圖表的繪製方法。所以很有必要系統性地介紹python的繪圖語法系統,包括最基礎也最常用的matplotlib包、常用於統計分析的seaborn、最新出現類似R ggplot2語法的plotnine以及用於地理空間數據可視化的basemap包。
  • ​數據可視化 | 6個基本可視化Python庫
    數據可視化是使用可視元素(例如圖表,圖形等)表示數據的過程,有助於從數據中獲取有意義的見解。它旨在揭示數據背後的信息,並進一步幫助查看者查看數據中的結構。數據可視化將使任何人只要對數據科學的了解最少,即可獲得科學發現,並幫助人們輕鬆地交流信息。畢竟,一張圖片勝過千言萬語。在本文中我們將介紹一些Python支持的最出色的可視化庫。
  • Python數據可視化—Seaborn
    今天為大家分享的小技巧是python的可視化畫圖庫Seaborn。相信很多小俠客用過matplotlib、pyecharts等可視化庫,可是為什麼還要介紹Seaborn呢?因為它修復了上述兩個庫的一些缺點,比如Seaborn提供了大量的高級接口和自定義主題,而matplotlib沒有這些接口使得很難確定哪些設置來自定義圖表。
  • Python數據可視化教程之基礎篇
    經過學習之後,我總結了利用python實現可視化的三個步驟:確定問題,選擇圖形轉換數據,應用函數參數設置,一目了然python中最基本的作圖庫就是matplotlib,是一個最基礎的Python可視化庫,一般都是從matplotlib上手Python數據可視化,
  • Python數據可視化-seaborn Iris鳶尾花數據
    StripplotStripplot的本質就是把數據集中具有quantitative屬性的變量按照類別去做散點圖(Scatterplot)。我們將紙鳶花數據集中不同種類花的sepal length做stripplot可視化上邊左側的圖片便是在默認風格下用stripplot繪製的散點圖。
  • 數據統計可視化——python生成詞雲
    有的人會認為它是文本挖掘的可視化、有的人 會給一些用戶打上標籤、有的人則只是想玩玩這酷炫的詞彙……但詞雲產生的視覺效果才是讓人更著迷的地方。詞雲主要是對文本中出現頻率較高的「關鍵詞」進行視覺上的突出,讓人更直觀地看出文本的重點。本文針對上一篇文章
  • Python的數據可視化:對比7種工具包
    Python 的scientific stack(一個介紹Python科學計算包的網站)已經完全成熟,並且有各種各樣用例的庫,包括機器學習(連結:machine learning),數據分析(連結:data analysis)。數據可視化是探索數據和清晰的解釋結果很重要的一部分,但是Python在過去卻相對於其他工具比如R落後一點。
  • Python數據可視化,無法繞過的matplotlib基礎知識!!!
    來源:逐夢erhttps://zhumenger.blog.csdn.net/article/details/106530281【導語】:出色的數據可視化,會讓你的數據分析等工作錦上添花,讓人印(升)象(職)深(加)刻(薪)。
  • 想用Python做數據可視化?先邁過這個「坎」
    用過python的人都會面臨一個問題,尤其是初學者:我應該選哪個來實現數據可視化? 以下就是將要用來創建繪製數據的示例: ·Pandas ·Seaborn ·Ggplot 在示例中,我將使用pandas進行數據處理並使用它來完成可視化的效果。
  • 利用Python畫中國地圖,實現各省數據可視化
    第一步:安裝pyecharts pyecharts是一款將python
  • Python的可視化工具概述
    >ggplotBokehpygalPlotly在例子用,我將使用pandas操作數據,並啟動其可視化.在大多數情況下使用這些工具不需要pandas,但是我覺得pandas+可視化工具如此普遍,這是最好的起點。
  • Python數據分析利器:matplotlib超詳細基礎介紹!!!
    ,會讓你的數據分析等工作錦上添花,讓人印(升)象(職)深(加)刻(薪)。matplotlib是python優秀的數據可視化庫,python數據分析必備利器,本文專門為你整理了matplotlib詳細使用方法,來學習吧!--- 以下是正文 ---數據可視化非常重要,因為錯誤或不充分的數據表示方法可能會毀掉原本很出色的數據分析工作。
  • 15種獨一無二的創新數據可視化方式
    全文共5489字,預計學習時長11分鐘可視化能解答那些尚未發現的問題。本·施耐德曼(Ben Shneiderman)掌握數據可視化技術會打開新世界的大門,帶來更多機會。精心設計的可視化能幫助程式設計師找到原始數據集的核心。 這是成功的數據科學項目和普通的數據科學項目之間的區別。 因此,本文旨在展示數據可視化的強大功能。
  • 大數據時代,如何培養數據分析思維?|建模|excel|數據分析|python|...
    大數據時代來臨,「數據」熱度飆升,衍生出的行業也受到追捧。據悉,中國大數據行業人才需求2020年將達210萬,未來5年需求量在2000萬人左右。可見,國內數據分析崗錢途與前景並存,想入門該朝陽產業,需要先明白數據分析究竟是什麼?
  • Python 可視化神器--Plotly
    學習Python是做數分析的最基礎的一步,數據分析離不開數據可視化。Python第三方庫中我們最常用的可視化庫是 pandas,matplotlib,pyecharts,當然還有 Tableau,另外最近在學習過程中發現另一款可視化神器-Plotly,它是一款用來做數據分析和可視化的在線平臺,功能非常強大,可以在線繪製很多圖形比如條形圖、散點圖、餅圖、直方圖等等。
  • 使用biopython可視化染色體和基因元件
    在biopython中,通過BiolGraphics子模塊可以對基因組結構進行可視化,支持線性和圈圖兩種可視化方式。其中,基因組結構信息存儲在genebank格式的文件中,首先通過Bio.SeqIO讀取結構信息,然後通過Bio.Graphics模塊進行可視化。