99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀如何用python做出超炫酷圖表?
如何用python做出超炫酷圖表?
2019-02-14
收藏

CDA數(shù)據(jù)分析研究院原創(chuàng)作品,轉(zhuǎn)載需授權(quán)


小編總是被那些玩轉(zhuǎn)數(shù)據(jù)、利用數(shù)據(jù)做出超炫酷圖表的大佬深深折服,膝蓋都不夠給他們。進行數(shù)據(jù)可視化做出超炫圖表的軟件有很多,今天小編也用數(shù)據(jù)分析常用的python來演示一下如何做出精彩的數(shù)據(jù)可視化呈現(xiàn)。


導(dǎo)入相關(guān)的庫和加載數(shù)據(jù)

import numpy as np 

import pandas as pd 

import seaborn as sns

import matplotlib.pyplot as plt

from datetime import date, timedelta, datetime


設(shè)置路徑和加載數(shù)據(jù)

小編使用的是一個記錄美國1908年到2009年飛機出事和死亡乘客記錄的數(shù)據(jù)。

import os

os.chdir(r'D:\data\air_data')

Data=pd.read_csv('airplane.csv')

查看各列有沒有缺失值:

Data.isnull().sum()


對缺失數(shù)據(jù)進行清洗:

Data['Time'] = Data['Time'].replace(np.nan, '00:00') 

Data['Time'] = Data['Time'].str.replace('c: ', '')

Data['Time'] = Data['Time'].str.replace('c:', '')

Data['Time'] = Data['Time'].str.replace('c', '')

Data['Time'] = Data['Time'].str.replace('12\'20', '12:20')

Data['Time'] = Data['Time'].str.replace('18.40', '18:40')

Data['Time'] = Data['Time'].str.replace('0943', '09:43')

Data['Time'] = Data['Time'].str.replace('22\'08', '22:08')

Data['Time'] = Data['Time'].str.replace('114:20', '00:00')

Data['Time'] = Data['Date'] + ' ' + Data['Time'] 

    return datetime.strptime(x, '%m/%d/%Y %H:%M')

Data['Time'] = Data['Time'].apply(todate) 

print('Date ranges from ' + str(Data.Time.min()) + ' to ' + str(Data.Time.max()))


Data.Operator = Data.Operator.str.upper() 

數(shù)據(jù)可視化

繪制1908年到2009年飛機出事頻數(shù)的折線圖,大概得出一個趨勢變化。

Temp = Data.groupby(Data.Time.dt.year)[['Date']].count()

Temp = Temp.rename(columns={"Date": "Count"})


plt.figure(figsize=(12,6))

plt.style.use('bmh')

plt.plot(Temp.index, 'Count', data=Temp, color='blue', marker = ".", linewidth=1)

plt.xlabel('Year', fontsize=10)

plt.ylabel('Count', fontsize=10)

plt.title('Count of accidents by Year', loc='Center', fontsize=14)

plt.show()

我們把時間再精細化點,觀察每月,每個星期,甚至每小時的事故,這次我們不看趨勢,看量,繪制條形圖

import matplotlib.pylab as pl

import matplotlib.gridspec as gridspec


gs = gridspec.GridSpec(2, 2)

pl.figure(figsize=(15,10))

plt.style.use('seaborn-muted')

ax = pl.subplot(gs[0, :]) # row 0, col 0

sns.barplot(Data.groupby(Data.Time.dt.month)[['Date']].count().index, 'Date', data=Data.groupby(Data.Time.dt.month)[['Date']].count(), color='lightskyblue', linewidth=2)

plt.xticks(Data.groupby(Data.Time.dt.month)[['Date']].count().index, ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])

plt.xlabel('Month', fontsize=10)

plt.ylabel('Count', fontsize=10)

plt.title('Count of accidents by Month', loc='Center', fontsize=14)


ax = pl.subplot(gs[1, 0])

sns.barplot(Data.groupby(Data.Time.dt.weekday)[['Date']].count().index, 'Date', data=Data.groupby(Data.Time.dt.weekday)[['Date']].count(), color='lightskyblue', linewidth=2)

plt.xticks(Data.groupby(Data.Time.dt.weekday)[['Date']].count().index, ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])

plt.xlabel('Day of Week', fontsize=10)

plt.ylabel('Count', fontsize=10)

plt.title('Count of accidents by Day of Week', loc='Center', fontsize=14)

ax = pl.subplot(gs[1, 1])

sns.barplot(Data[Data.Time.dt.hour != 0].groupby(Data.Time.dt.hour)[['Date']].count().index, 'Date', data=Data[Data.Time.dt.hour != 0].groupby(Data.Time.dt.hour)[['Date']].count(),color ='lightskyblue', linewidth=2)

plt.xlabel('Hour', fontsize=10)

plt.ylabel('Count', fontsize=10)

plt.title('Count of accidents by Hour', loc='Center', fontsize=14)

plt.tight_layout()

plt.show()

出事時,每年登機人數(shù)與死亡人數(shù)的對比圖

Fatalities = Data.groupby(Data.Time.dt.year).sum()

Fatalities['Proportion'] = Fatalities['Fatalities'] / Fatalities['Aboard']


plt.figure(figsize=(15,6))

plt.subplot(1, 2, 1)

plt.fill_between(Fatalities.index, 'Aboard', data=Fatalities, color="skyblue", alpha=0.2)

plt.plot(Fatalities.index, 'Aboard', data=Fatalities, marker = ".", color="Slateblue", alpha=0.6, linewidth=1)

plt.fill_between(Fatalities.index, 'Fatalities', data=Fatalities, color="olive", alpha=0.2)

plt.plot(Fatalities.index, 'Fatalities', data=Fatalities, color="olive", marker = ".", alpha=0.6, linewidth=1)

plt.legend(fontsize=10)

plt.xlabel('Year', fontsize=10)

plt.ylabel('Amount of people', fontsize=10)

plt.title('Total number of people involved by Year', loc='Center', fontsize=14)

plt.subplot(1, 2, 2)

plt.plot(Fatalities.index, 'Proportion', data=Fatalities, marker = ".", color = 'red', linewidth=1)

plt.xlabel('Year', fontsize=10)

plt.ylabel('Ratio', fontsize=10)

plt.title('Fatalities / Total Ratio by Year', loc='Center', fontsize=14)

plt.tight_layout()

plt.show()


通過對比圖我們可以看到死亡人數(shù)變得如此之高(即使在90年代后似乎有下降的趨勢)。一些人提出了一個很好的觀點,那就是圖表并沒有顯示每年所有航班發(fā)生事故的比例。因此,1970-1990年在空中交通信號燈的歷史上看起來是可怕的一年,死亡人數(shù)上升,但也有可能是乘飛機的總?cè)藬?shù)上升,而實際上比例下降了。


親愛的筒子們,想了解更多用python玩轉(zhuǎn)數(shù)據(jù)、掌握炫酷可視化技能那就趕緊關(guān)注CDA數(shù)據(jù)分析師微信公眾號(cdacdacda)吧,點贊、轉(zhuǎn)發(fā)、收藏,更多干貨內(nèi)容呈現(xiàn)給你噢。


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }