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

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代使用python來繪制漂亮的圖表:pandas篇!
使用python來繪制漂亮的圖表:pandas篇!
2020-05-27
收藏
使用Python繪制數(shù)據(jù),可以使用三種不同方式,它們分別是pandas,Seaborn和Plotly。 我們將通過利用《 2019年世界幸福報告》中的數(shù)據(jù)來做到這一點。我用Gapminder和Wikipedia的信息豐富了《世界幸福報告》數(shù)據(jù),以便探索新的關(guān)系和可視化。在整篇文章中,我將使用Life Ladder作為目標(biāo)變量。

我使用Python繪圖的歷史
大約兩年前,我開始更認(rèn)真地學(xué)習(xí)Python。 從那以后,幾乎沒有一個星期不讓我驚嘆于Python本身,或生態(tài)系統(tǒng)中眾多令人驚嘆的開源庫的簡單性和易用性。 我熟悉的命令,模式和概念越多,那么所有事情就越有意義。
Matplotlib
使用Python進(jìn)行繪圖的情況恰恰相反。 最初,我用Matplotlib創(chuàng)建的幾乎每個圖表都看起來像是八十年代逃脫的罪犯。 更糟糕的是,要創(chuàng)建這些可憎的東西,我通常不得不在Stackoverflow上花費數(shù)小時。 例如,研究nitty-gritty命令以更改x-ticks的傾斜度或類似的愚蠢行為。 甚至沒有讓我開始使用多張圖表。 結(jié)果看起來令人印象深刻,并且以編程方式創(chuàng)建這些圖表是一種奇妙的感覺。 例如,一次生成50個針對不同變量的圖表。 但是,這只是很多工作,需要您記住很多其他本來沒用的命令。
Seaborn
了解Seaborn是一種解脫。 Seaborn提取了許多微調(diào)。 毫無疑問,最終圖表的美學(xué)意義是一個巨大的飛躍。 但是,它也是基于Matplotlib構(gòu)建的。 通常,對于非標(biāo)準(zhǔn)調(diào)整,仍然有必要深入了解機(jī)器級的matplotlib代碼。
Bokeh
簡要了解一下Bokeh,我以為Bokeh將成為我的首選解決方案。 當(dāng)我從事地理空間可視化工作時遇到了Bokeh。 但是,我很快意識到,雖然Bokeh與眾不同,但它與matplotlib一樣復(fù)雜。
Plotly
我確實在一段時間前嘗試了plot.ly(從現(xiàn)在開始被稱為plotly)。 再一次,致力于地理空間數(shù)據(jù)的可視化。 那時,它似乎比前面提到的庫還要不靠譜。需要一個帳戶,必須通過筆記本電腦登錄,然后才能使所有內(nèi)容在線呈現(xiàn)。 然后,還需要下載結(jié)果圖表。 我迅速放棄了。 但是,最近,我看了一個有關(guān)YouTube的視頻,該視頻關(guān)于plotly express和plotly 4.0,最重要的是,他們擺脫了所有這些在線限制。 我玩了一下,這篇文章就是它的結(jié)果。 我想遲到總比沒有好。
Kepler.gl(地理空間數(shù)據(jù)榮譽獎)
Kepler.gl雖然絕對不是Python庫,但它是一種用于地理空間數(shù)據(jù)的基于Web的強大可視化工具。 您只需要CSV文件,即可使用Python輕松創(chuàng)建。 試試看!
我目前的工作流程
最終,我決定使用Pandas原始繪圖進(jìn)行快速檢查,并使用Seaborn生成要在報告和演示文稿中使用的圖表(視覺效果很重要)。

分布的重要性在圣地亞哥學(xué)習(xí)期間,我教過統(tǒng)計學(xué)(Stats 119)。 統(tǒng)計119是統(tǒng)計的入門課程。 該課程包括統(tǒng)計基礎(chǔ),例如數(shù)據(jù)匯總(可視和定量),幾率和概率的概念,回歸,抽樣以及最重要的分布。 這次是我對數(shù)量和現(xiàn)象的理解幾乎全部轉(zhuǎn)變?yōu)榛诜植急硎荆ù蟛糠謺r間是高斯)。
時至今日,我發(fā)現(xiàn)這兩個數(shù)量的平均含義令人吃驚,而標(biāo)準(zhǔn)偏差可以幫助您把握現(xiàn)象。 僅通過了解這兩個數(shù)字,就可以輕松得出特定結(jié)果的可能性。 人們立即知道大部分結(jié)果將在哪里。 它為您提供了一個參考框架,可以快速將軼事與有統(tǒng)計意義的事件區(qū)分開來,而無需進(jìn)行過于復(fù)雜的計算。
一般而言,面對新數(shù)據(jù)時,我的第一步是嘗試可視化其分布,以更好地理解數(shù)據(jù)。

加載數(shù)據(jù)和導(dǎo)入庫讓我們加載本文中將要使用的數(shù)據(jù)。 我確實對數(shù)據(jù)進(jìn)行了一些預(yù)處理。 我在有意義的地方進(jìn)行數(shù)據(jù)填充處理。
# Load the data data = pd.read_csv('https://raw.githubusercontent.com/FBosler/AdvancedPlotting/master/combined_set.csv') # this assigns labels per year data['Mean Log GDP per capita'] = data.groupby('Year')['Log GDP per capita'].transform( pd.qcut, q=5, labels=(['Lowest','Low','Medium','High','Highest']) )數(shù)據(jù)集包含以下各列的值:
· Year 年:計量年(從2007年到2018年)
· Life Ladder 人生階梯:受訪者根據(jù)Cantril階梯,以0到10的等級(10最佳)衡量他們今天的生活價值
· Log GDP per capita 對數(shù)人均GDP:根據(jù)購買力平價(PPP)調(diào)整的人均GDP,根據(jù)世界銀行于2018年11月14日發(fā)布的世界發(fā)展指標(biāo)(WDI)得出
· Social support 社會支持:對問題的回答:“如果遇到麻煩,您是否有親戚或朋友可以在需要時幫助您?”
· Healthy life expectancy at birth 出生時的健康預(yù)期壽命:出生時的預(yù)期壽命是根據(jù)世界衛(wèi)生組織(WHO)全球衛(wèi)生觀察站數(shù)據(jù)存儲庫中的數(shù)據(jù)構(gòu)建的,其中提供了2005、2010、2015和2016年的數(shù)據(jù)。
· Freedom to make life choices 選擇生活的自由度:對問題的回答:“您對選擇自己的生活的自由感到滿意還是不滿意?”
· Generosity 慷慨度:與"人均GDP"相比,對"您在過去一個月向慈善機(jī)構(gòu)捐款了嗎?"的回應(yīng)
· Perceptions of corruption 對腐敗的看法:回答"腐敗是否在整個政府范圍內(nèi)廣泛存在?“和"腐敗是否在企業(yè)內(nèi)部廣泛分布?”
· Positive affect 積極影響:包括前一天的幸福,笑聲和享受的平均頻率。
· Negative affect 負(fù)面影響:包括前一天擔(dān)憂,悲傷和憤怒的平均頻率。
· Confidence in national government 對國家政府的信心:不言自明
· Democratic Quality 民主素質(zhì):一個國家的民主程度
· Delivery Quality 交付質(zhì)量:一個國家兌現(xiàn)其政策的效果如何
· Gapminder Life Expectancy Gapminder預(yù)期壽命:Gapminder的預(yù)期壽命
· Gapminder Population Gapminder人口:一個國家的人口

快速:使用Pandas進(jìn)行基本繪圖
Pandas具有內(nèi)置的繪圖功能,可以在SeriesDataFrame上調(diào)用它。 我喜歡這些繪圖功能,因為它們簡潔,使用合理的默認(rèn)值,并且可以快速了解發(fā)生的情況。
要創(chuàng)建繪圖,請對數(shù)據(jù)調(diào)用.plot(kind = ),如下所示:
np.exp(data[data['Year']==2018]['Log GDP per capita']).plot( kind='hist' )運行上面的命令將產(chǎn)生以下圖表。

2018: Histogram of the number of countries per GDP per Capita bucket. Not surprisingly, most countri
使用Pandas進(jìn)行繪圖時,有五個主要參數(shù):
· kind:Pandas必須知道您要創(chuàng)建哪種圖,以下選項可用hist,bar,barh,scatter,area,kde,line,box,hexbin,餅圖。
· figsize:允許覆蓋6英寸寬和4英寸高的默認(rèn)輸出大小。 figsize需要一個元組(例如,我經(jīng)常使用的figsize =(12,8))
· title:向圖表添加標(biāo)題。 在大多數(shù)情況下,我用它來澄清圖表中顯示的內(nèi)容,以便當(dāng)我回到圖表上時,可以快速確定發(fā)生了什么。 title需要一個字符串。
· bins:允許覆蓋直方圖的bin寬度。 bins需要一個列表或類似列表的值序列(例如bins = np.arange(2,8,0.25))
· xlim / ylim:允許覆蓋軸的最大值和最小值的默認(rèn)值。 xlim和ylim都期望有一個元組(例如xlim =(0,5))
讓我們快速瀏覽各種可用的plots類型。
垂直條形圖data[ data['Year'] == 2018 ].set_index('Country name')['Life Ladder'].nlargest(15).plot( kind='bar', figsize=(12,8) )
2018: List of 15 happiest countries is led by Finnland
水平條形圖np.exp(data[ data['Year'] == 2018 ].groupby('Continent')['Log GDP per capita'].mean()).sort_values().plot( kind='barh', figsize=(12,8) )!

Average GDP per capita by continent in 2011 USD Dollars clearly led by Australia and New Zealand
箱形圖data['Life Ladder'].plot( kind='box', figsize=(12,8) )
Box plot of the distribution of Life Ladder shows that the median is somewhere around 5.5 ranging fr
散點圖data[['Healthy life expectancy at birth','Gapminder Life Expectancy']].plot( kind='scatter', x='Healthy life expectancy at birth', y='Gapminder Life Expectancy', figsize=(12,8) )
Scatter plot of the World Happiness Report life expectation against the Gapminder life expectation s
六邊形圖data[data['Year'] == 2018].plot( kind='hexbin',   x='Healthy life expectancy at birth',   y='Generosity',   C='Life Ladder',   gridsize=20,   figsize=(12,8),    cmap="Blues", # defaults to greenish   sharex=False # required to get rid of a bug )
2018: Hexbin plot, plotting life expectancy against generosity. The color of bins indicates the aver
餅圖data[data['Year'] == 2018]  .groupby( ['Continent'])['Gapminder Population']   .sum()   .plot(     kind='pie',     figsize=(12,8),     cmap="Blues_r", # defaults to orangish   )
2018: Pie chart showing the total population by continent
堆積面積圖data.groupby( ['Year','Continent'])['Gapminder Population'] .sum() .unstack() .plot(     kind='area',     figsize=(12,8),     cmap="Blues", # defaults to orangish )
Population numbers accross the globe are on the rise.
折線圖data[ data['Country name'] == 'Germany'] .set_index('Year')['Life Ladder'] .plot( kind='line', figsize=(12,8) )
Line chart depicting the development of happiness in Germany.
關(guān)于Pandas繪圖的結(jié)論用Pandas繪圖很方便。 它易于訪問,而且速度很快。 Plot很難看。 偏離默認(rèn)值是不可能的,這是可以的,因為我們還有其他工具可以使圖表更具美學(xué)吸引力。 去看看seaborn吧。

本文翻譯自Fabian Bosler的文章《Learn how to create beautiful and insightful charts with Python — the Quick, the Pretty, and the Awesome》 參考https://towardsdatascience.com/plotting-with-python-c2561b8c0f1f)

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(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ù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) 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); }