利用Python進(jìn)行數(shù)據(jù)可視化的實(shí)例代碼
前言
前面寫過(guò)一篇用Python制作PPT的博客,感興趣的可以參考
這篇是關(guān)于用Python進(jìn)行數(shù)據(jù)可視化的,準(zhǔn)備作為一個(gè)長(zhǎng)貼,隨時(shí)更新有價(jià)值的Python可視化用例,都是網(wǎng)上搜集來(lái)的,與君共享,本文所有測(cè)試均基于Python3.
首先搭建環(huán)境
$pip install pyecharts -U $pip install echarts-themes-pypkg $pip install snapshot_selenium $pip install echarts-countries-pypkg $pip install echarts-cities-pypkg $pip install echarts-china-provinces-pypkg $pip install echarts-china-cities-pypkg $pip install echarts-china-counties-pypkg $pip install echarts-china-misc-pypkg $pip install echarts-united-kingdom-pypkg $pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts $git clone https://github.com/pyecharts/pyecharts.git $cd pyecharts/ $pip install -r requirements.txt $python setup.py install
一頓操作下來(lái),該裝的不該裝的都裝上了,多裝一些包沒(méi)壞處,說(shuō)不定哪天就用上了呢
實(shí)例代碼
例子1:
from pyecharts.charts import Bar from pyecharts import options as opts bar = ( Bar() .add_xaxis(["chenshan", "maoyi", "longdai", "kuzi", "fengyi", "gaogenxie", "wazi"]) .add_yaxis("storeA", [114, 55, 27, 101, 125, 27, 105]) .add_yaxis("storeB", [57, 134, 137, 129, 145, 60, 49]) .set_global_opts(title_opts=opts.TitleOpts(title="sales")) ) #bar.render_notebook() bar.render()
render():默認(rèn)將會(huì)在根目錄下生成一個(gè) render.html 的文件,支持 path 參數(shù),設(shè)置文件保存位置,如 render("./xx/xxx.html").
結(jié)果是以網(wǎng)頁(yè)的形式輸出的,執(zhí)行后,在當(dāng)前目錄下生成render.html,用瀏覽器打開,最好事先安裝chrome瀏覽器.
例子2:
from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker pie = ( Pie() .add("", [list(z) for z in zip(Faker.choose(), Faker.values())]) .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) .set_global_opts(title_opts=opts.TitleOpts(title="Pie-設(shè)置顏色")) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) ) pie.render()
例子3:
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker c = ( Line() .add_xaxis(Faker.choose()) .add_yaxis("storeA", Faker.values(), is_smooth=True) .add_yaxis("storeB", Faker.values(), is_smooth=True) .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth")) ) c.render()
例子4:
from pyecharts import options as opts from pyecharts.charts import Kline data = [ [2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22], ] k = ( Kline() .add_xaxis(["2017/7/{}".format(i + 1) for i in range(31)]) .add_yaxis("k-line", data) .set_global_opts( yaxis_opts=opts.AxisOpts(is_scale=True), xaxis_opts=opts.AxisOpts(is_scale=True), title_opts=opts.TitleOpts(title="Kline-examples"), ) ) k.render()
例子5:
from pyecharts import options as opts from pyecharts.charts import Gauge g = ( Gauge() .add("", [("complete", 66.6)]) .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-basic examples")) ) g.render()
例子6:
from pyecharts import options as opts from pyecharts.charts import Bar ( Bar() .add_xaxis( [ "名字很長(zhǎng)的X軸標(biāo)簽1", "名字很長(zhǎng)的X軸標(biāo)簽2", "名字很長(zhǎng)的X軸標(biāo)簽3", "名字很長(zhǎng)的X軸標(biāo)簽4", "名字很長(zhǎng)的X軸標(biāo)簽5", "名字很長(zhǎng)的X軸標(biāo)簽6", ] ) .add_yaxis("商家A", [10, 20, 30, 40, 50, 40]) .add_yaxis("商家B", [20, 10, 40, 30, 40, 50]) .set_global_opts( xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), title_opts=opts.TitleOpts(title="Bar-旋轉(zhuǎn)X軸標(biāo)簽", subtitle="解決標(biāo)簽名字過(guò)長(zhǎng)的問(wèn)題"), ) .render() )
from pyecharts import options as opts from pyecharts.faker import Faker from pyecharts.charts import Page, Pie l1 = ['aa','bb','cc','dd','ee'] num =[10,20,15,25,30] c = ( Pie() .add( "", [list(z) for z in zip(l1, num)], radius=["40%", "75%"], # 圓環(huán)的粗細(xì)和大小 ) .set_global_opts( title_opts=opts.TitleOpts(title="Pie-Radius"), legend_opts=opts.LegendOpts( orient="vertical", pos_top="5%", pos_left="2%" # 左面比例尺 ), ) .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}")) ) c.render()
from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.charts import Page, Pie l1 = ['aa','bb','cc','dd','ee'] num =[10,20,15,25,30] c = ( Pie() .add( "", [list(z) for z in zip(l1, num)], radius=["40%", "55%"], label_opts=opts.LabelOpts( position="outside", formatter="{a|{a}}{abg|} {hr|} {b|: }{c} {per|vvxyksv9kd%} ", background_color="#eee", border_color="#aaa", border_width=1, border_radius=4, rich={ "a": {"color": "#999", "lineHeight": 22, "align": "center"}, "abg": { "backgroundColor": "#e3e3e3", "width": "100%", "align": "right", "height": 22, "borderRadius": [4, 4, 0, 0], }, "hr": { "borderColor": "#aaa", "width": "100%", "borderWidth": 0.5, "height": 0, }, "b": {"fontSize": 16, "lineHeight": 33}, "per": { "color": "#eee", "backgroundColor": "#334455", "padding": [2, 4], "borderRadius": 2, }, }, ), ) .set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例")) ) c.render()
from pyecharts import options as opts from pyecharts.charts import Line, Bar, Grid bar = ( Bar() .add_xaxis(["襯衫", "毛衣", "領(lǐng)帶", "褲子", "風(fēng)衣", "高跟鞋", "襪子"]) .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105]) .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49]) .set_global_opts(title_opts=opts.TitleOpts(title="運(yùn)維之路"),) ) week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] high_temperature = [11, 11, 15, 13, 12, 13, 10] low_temperature = [1, -2, 2, 5, 3, 2, 0] line2 = ( Line(init_opts=opts.InitOpts(width="1600px", height="800px")) .add_xaxis(xaxis_data=week_name_list) .add_yaxis( series_name="最高氣溫", y_axis=high_temperature, markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ] ), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="平均值")] ), ) .add_yaxis( series_name="最低氣溫", y_axis=low_temperature, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)] ), markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(type_="average", name="平均值"), opts.MarkLineItem(symbol="none", x="90%", y="max"), opts.MarkLineItem(symbol="circle", type_="max", name="最高點(diǎn)"), ] ), ) .set_global_opts( #title_opts=opts.TitleOpts(title="氣溫變化", subtitle="純屬虛構(gòu)"), tooltip_opts=opts.TooltipOpts(trigger="axis"), toolbox_opts=opts.ToolboxOpts(is_show=True), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), #legend_opts=opts.LegendOpts(pos_left="right"), ) #.render("temperature_change_line_chart.html") ) # 最后的 Grid #grid_chart = Grid(init_opts=opts.InitOpts(width="1400px", height="800px")) grid_chart = Grid() grid_chart.add( bar, grid_opts=opts.GridOpts( pos_left="3%", pos_right="1%", height="20%" ), ) # wr grid_chart.add( line2, grid_opts=opts.GridOpts( pos_left="3%", pos_right="1%", pos_top="40%", height="35%" ), ) #grid_chart.render("professional_kline_chart.html") grid_chart.render()
from pyecharts import options as opts from pyecharts.charts import Radar v1=[[83, 92, 87, 49, 89, 86]] # 數(shù)據(jù)必須為二維數(shù)組,否則會(huì)集中一個(gè)指示器顯示 v2=[[88, 95, 66, 43, 86, 96]] v3=[[80, 92, 87, 58, 78, 81]] radar1=( Radar() .add_schema(# 添加schema架構(gòu) schema=[ opts.RadarIndicatorItem(name='傳球',max_=100),# 設(shè)置指示器名稱和最大值 opts.RadarIndicatorItem(name='射門',max_=100), opts.RadarIndicatorItem(name='身體',max_=100), opts.RadarIndicatorItem(name='防守',max_=100), opts.RadarIndicatorItem(name='速度',max_=100), opts.RadarIndicatorItem(name='盤帶',max_=100), ] ) .add('羅納爾多',v1,color="#f9713c") # 添加一條數(shù)據(jù),參數(shù)1為數(shù)據(jù)名,參數(shù)2為數(shù)據(jù),參數(shù)3為顏色 .add('梅西',v2,color="#4169E1") .add('蘇亞雷斯',v3,color="#00BFFF") .set_global_opts(title_opts=opts.TitleOpts(title='雷達(dá)圖'),) ) radar1.render()
import math import random from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.charts import Page, Polar c = ( Polar() .add_schema( angleaxis_opts=opts.AngleAxisOpts(data=Faker.week, type_="category") ) .add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar", stack="stack0") .add("B", [2, 4, 6, 1, 2, 3, 1], type_="bar", stack="stack0") .add("C", [1, 2, 3, 4, 1, 2, 5], type_="bar", stack="stack0") .set_global_opts(title_opts=opts.TitleOpts(title="Polar-AngleAxis")) ) c.render()
import math import random from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.charts import Page, Polar data = [(i, random.randint(1, 100)) for i in range(10)] c = ( Polar() .add("", data, type_="effectScatter", effect_opts=opts.EffectOpts(scale=10, period=5), label_opts=opts.LabelOpts(is_show=False)) # type默認(rèn)為"line", # "effectScatter",scatter,bar .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Scatter0")) ) c.render()
import math import random from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.charts import Page, Polar c = ( Polar() .add_schema( radiusaxis_opts=opts.RadiusAxisOpts(data=Faker.week, type_="category") ) .add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar", stack="stack0") .add("B", [2, 4, 6, 1, 2, 3, 1], type_="bar", stack="stack0") .add("C", [1, 2, 3, 4, 1, 2, 5], type_="bar", stack="stack0") .set_global_opts(title_opts=opts.TitleOpts(title="Polar-RadiusAxis")) ) c.render()
from pyecharts import options as opts from pyecharts.charts import Liquid, Page from pyecharts.globals import SymbolType c = ( Liquid() .add("lq", [0.61, 0.7],shape='rect',is_outline_show=False) # 水球外形,有' circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow' 可選。 # 默認(rèn) 'circle'。也可以為自定義的 SVG 路徑。 #is_outline_show設(shè)置邊框 .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-基本示例")) ) c.render()
散點(diǎn)圖:
from pyecharts.charts import Scatter import pyecharts.options as opts female_height = [161.2,167.5,159.5,157,155.8,170,159.1,166,176.2,160.2,172.5,170.9,172.9,153.4,160,147.2,168.2,175,157,167.6,159.5,175,166.8,176.5,170.2,] female_weight = [51.6,59,49.2,63,53.6,59,47.6,69.8,66.8,75.2,55.2,54.2,62.5,42,50,49.8,49.2,73.2,47.8,68.8,50.6,82.5,57.2,87.8,72.8,54.5,] male_height = [174 ,175.3 ,193.5 ,186.5 ,187.2 ,181.5 ,184 ,184.5 ,175 ,184 ,180 ,177.8 ,192 ,176 ,174 ,184 ,192.7 ,171.5 ,173 ,176 ,176 ,180.5 ,172.7 ,176 ,173.5 ,178 ,] male_weight = [65.6 ,71.8 ,80.7 ,72.6 ,78.8 ,74.8 ,86.4 ,78.4 ,62 ,81.6 ,76.6 ,83.6 ,90 ,74.6 ,71 ,79.6 ,93.8 ,70 ,72.4 ,85.9 ,78.8 ,77.8 ,66.2 ,86.4 ,81.8 ,89.6 ,] scatter = Scatter() scatter.add_xaxis(female_height) scatter.add_xaxis(male_height) scatter.add_yaxis("female", female_weight, symbol_size=15) #散點(diǎn)大小 scatter.add_yaxis("male", male_weight, symbol_size=15) #散點(diǎn)大小 scatter.set_global_opts(title_opts=opts.TitleOpts(title="身高體重分布"), xaxis_opts=opts.AxisOpts( type_ = "value", # 設(shè)置x軸為數(shù)值軸 splitline_opts=opts.SplitLineOpts(is_show = True)), # x軸分割線 yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))# y軸分割線 ) scatter.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) scatter.render("./html/scatter_base.html")
總結(jié)
到此這篇關(guān)于利用Python進(jìn)行數(shù)據(jù)可視化的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖
- 淺談哪個(gè)Python庫(kù)才最適合做數(shù)據(jù)可視化
- python數(shù)據(jù)可視化plt庫(kù)實(shí)例詳解
- 學(xué)會(huì)Python數(shù)據(jù)可視化必須嘗試這7個(gè)庫(kù)
- Python中seaborn庫(kù)之countplot的數(shù)據(jù)可視化使用
- python實(shí)現(xiàn)股票歷史數(shù)據(jù)可視化分析案例
- Python數(shù)據(jù)可視化之基于pyecharts實(shí)現(xiàn)的地理圖表的繪制
- Python爬蟲實(shí)戰(zhàn)之爬取京東商品數(shù)據(jù)并實(shí)實(shí)現(xiàn)數(shù)據(jù)可視化
- Python數(shù)據(jù)可視化之用Matplotlib繪制常用圖形
- Python數(shù)據(jù)可視化之繪制柱狀圖和條形圖
- python用pyecharts實(shí)現(xiàn)地圖數(shù)據(jù)可視化
- python數(shù)據(jù)可視化 – 利用Bokeh和Bottle.py在網(wǎng)頁(yè)上展示你的數(shù)據(jù)
相關(guān)文章
python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法
這篇文章主要介紹了python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Transformer導(dǎo)論之Bert預(yù)訓(xùn)練語(yǔ)言解析
這篇文章主要為大家介紹了Transformer導(dǎo)論之Bert預(yù)訓(xùn)練語(yǔ)言解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python的Django框架中自定義模版標(biāo)簽的示例
這篇文章主要介紹了Python的Django框架中自定義模版標(biāo)簽的示例,標(biāo)簽的用處比過(guò)濾器更多,需要的朋友可以參考下2015-07-07python自動(dòng)化測(cè)試之setUp與tearDown實(shí)例
這篇文章主要介紹了python自動(dòng)化測(cè)試之setUp與tearDown實(shí)例,其中setUp()方法中進(jìn)行測(cè)試前的初始化工作,并在tearDown()方法中執(zhí)行測(cè)試后的清除工作,setUp()和tearDown()都是TestCase類中定義的方法,需要的朋友可以參考下2014-09-09Python數(shù)據(jù)結(jié)構(gòu)之遞歸可視化詳解
遞歸函數(shù)是直接調(diào)用自己或通過(guò)一系列語(yǔ)句間接調(diào)用自己的函數(shù)。遞歸在程序設(shè)計(jì)有著舉足輕重的作用,在很多情況下,借助遞歸可以優(yōu)雅的解決問(wèn)題。本文主要介紹了如何利用可視化方式來(lái)了解遞歸函數(shù)的執(zhí)行步驟,需要的可以參考一下2022-04-04Python&Matlab實(shí)現(xiàn)灰狼優(yōu)化算法的示例代碼
灰狼優(yōu)化算法是一種群智能優(yōu)化算法,它的獨(dú)特之處在于一小部分擁有絕對(duì)話語(yǔ)權(quán)的灰狼帶領(lǐng)一群灰狼向獵物前進(jìn)。本文具體介紹了灰狼優(yōu)化算法的兩種實(shí)現(xiàn)示例代碼,需要的可以參考一下2022-03-03python中將一個(gè)全部為int的list 轉(zhuǎn)化為str的list方法
下面小編就為大家分享一篇python中將一個(gè)全部為int的list 轉(zhuǎn)化為str的list方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04python實(shí)現(xiàn)MD5進(jìn)行文件去重的示例代碼
工作中偶爾會(huì)收到一大堆文件,名稱各不相同,分析文件的時(shí)候發(fā)現(xiàn)有不少重復(fù)的文件,導(dǎo)致工作效率低下,那么,這里就寫了一個(gè)python腳本實(shí)現(xiàn)文件去重功能,感興趣的就一起來(lái)了解一下2021-07-07