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

熱線電話:13121318867

登錄
首頁精彩閱讀不到100行Python代碼教你做出精美炫酷的可視化大屏
不到100行Python代碼教你做出精美炫酷的可視化大屏
2021-12-16
收藏


作者:俊欣

來源:關(guān)于數(shù)據(jù)分析與可視化

“碳達(dá)峰、碳中和”是2021年政府在不斷強(qiáng)調(diào)與非常重視的事兒,那什么是“碳達(dá)峰”、什么又是“碳中和”呢?這里小編來為大家科普一下,所謂的“碳達(dá)峰”指的是在某一時間點,二氧化碳的排放不再達(dá)到峰值,之后逐步回落。

而“碳中和”也就意味著企業(yè)、個體與團(tuán)體在一定時間內(nèi)直接或間接產(chǎn)生的溫室氣體排放總量,通過植樹造林、節(jié)能減排等形式,抵消自身產(chǎn)生的二氧化碳排放,實現(xiàn)二氧化碳的“零排放”。

今天小編就用Python來制作一張可視化大屏,讓大家來感受一下近百年來二氧化碳排放的趨勢以及給我們所居住的環(huán)境造成了什么樣的影響。

介紹數(shù)據(jù)來源

本地可視化大屏中引用的數(shù)據(jù)來自于由英國牛津大學(xué)知名教授創(chuàng)辦的網(wǎng)站“用數(shù)據(jù)看世界(Our World in Data”,里面收入了各個學(xué)科的數(shù)據(jù),包括衛(wèi)生、食品、收入增長和分配、能源、教育、環(huán)境等行業(yè)進(jìn)行了分析與可視化展示,十分地全面,并且當(dāng)中的元數(shù)據(jù)開放在Github當(dāng)中

導(dǎo)入模塊并且讀取數(shù)據(jù)

我們導(dǎo)入需要用到的模塊

import streamlit as st
import plotly.express as px
import pandas as pd

我們這次是用到streamlit模塊來制作可視化大屏,該模塊是基于Python的可視化工具,最初開發(fā)出來的目的是給機(jī)器學(xué)習(xí)和數(shù)據(jù)科學(xué)團(tuán)隊使用的。同時我們用plotly.express模塊來繪制各種圖表,因此圖表是具備交互性的,pandas模塊來讀取數(shù)據(jù)

@st.cache
def get_data():
    url_1 = 'https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Climate%20change%20impacts/Climate%20change%20impacts.csv'
    url_2 = "https://github.com/owid/co2-data/raw/master/owid-co2-data.csv"

    df_1 = pd.read_csv(url_1)
    df_1_1 = df_1.query("Entity == 'World' and Year <=2021")

    df_2 = pd.read_csv(url_2)
    return df_1_1, df_2

可視化大屏的制作

然后我們來制作整個可視化大屏,首先我們先確認(rèn)好可視化大屏的布局,如下圖所示

然后我們針對每一篇布局來編寫代碼,首先看到的是標(biāo)題部分,我們通過streamlit模塊當(dāng)中的markdown方法來實現(xiàn)即可

st.markdown()

然后根據(jù)上面的布局設(shè)計,我們這么來編寫代碼

col2, space2, col3 = st.columns((10,1,10))

with col2:
    year = st.slider('選擇年份',1750,2020)
    ...

with col3: 
    ...
    selected_countries = st.multiselect('選擇國家',countries,default_countries)
    ...

col4, space3, col5, space4, col6 = st.columns((10,1,10,1,10))
with col4:
    st.markdown("""## 二氧化碳和全球變暖之間的關(guān)系""")

with col5:
    st.subheader(" 副標(biāo)題一 ")
    ...

with col6:
    st.subheader(" 副標(biāo)題二 ")
    ...

我們這里使用columns方法來將頁面均勻的分成若干列,并且給定特定的寬度,當(dāng)然每列之間還需要留一點空隙,從美觀程度上來考慮,因此才有了變量space對應(yīng)的是寬度1的空隙

col2, space2, col3 = st.columns((10,1,10))

然后我們針對分割開來的每個區(qū)域進(jìn)行圖表的繪制,例如左上方的世界地圖,我們用plotly.express當(dāng)中的choropleth方法來繪制,另外我們添加了時間軸,通過調(diào)用streamlit模塊當(dāng)中的slider方法來實現(xiàn)

with col2:
    year = st.slider('選擇時間', 1750, 2020)
    fig = px.choropleth(df_co2[df_co2['year'] == year], locations="iso_code",
                        color="co2_per_capita",
                        hover_name="country",
                        range_color=(0, 25),
                        color_continuous_scale=px.colors.sequential.Reds)
    st.plotly_chart(fig, use_container_width=True)

而例如右上方的折線圖,同樣也是調(diào)用plotly.express模塊來實現(xiàn)的,其中多選框則是調(diào)用了streamlit模塊當(dāng)中的multiselect方法,代碼如下

with col3:
    default_countries = ['World', 'United States', 'United Kingdom', 'EU-27', 'China', 'Canada']
    countries = df_co2['country'].unique()
    selected_countries = st.multiselect('選擇國家或者區(qū)域性組織', countries, default_countries)
    df3 = df_co2.query('country in @selected_countries')
    fig2 = px.line(df3, "year", "co2_per_capita", color="country")
    st.plotly_chart(fig2, use_container_width=True)

最后的成品如下圖所示:

從上面繪制的圖表中我們能夠看到的是,美國以及加拿大這兩國家二氧化碳的排放量一直都很高,超過了包括歐盟、英國以及中國在內(nèi)的主要經(jīng)濟(jì)體。當(dāng)然近些年各個國家的政府也對該問題相當(dāng)?shù)闹匾暎朴喠讼鄬?yīng)的節(jié)能減排的應(yīng)對措施。


數(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); }