1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Python可視化,十分鐘入門簡單而強(qiáng)大的可視化庫 Plotly

        共 5971字,需瀏覽 12分鐘

         ·

        2022-02-14 08:49

        在python的世界里,用于可視化分析的庫有很多,有最原始的matplotlib,還有更加簡單好用的serborn(對matplotlib進(jìn)行封裝),而pyecharts則是以動(dòng)態(tài)、可交互的特點(diǎn)吸引著眾人。
        今天為大家介紹一個(gè)兼具pyecharts的動(dòng)態(tài)性和serborn的簡便性與一體的可視化庫——plotly express
        讓我們對比一下pyecharts與plotly express完成?;鶊D的步驟
        首先看pyecharts,由于其需要苛刻的數(shù)據(jù)結(jié)構(gòu),所以需要花費(fèi)大量的精力用于數(shù)據(jù)處理。
        img
        然后我們看plotly express,簡簡單單幾行代碼就實(shí)現(xiàn)了類似的效果,可以節(jié)省大量的時(shí)間精力,提升工作效率!

        接下來我們就來學(xué)習(xí)plotly express的使用方法吧!
        首先導(dǎo)入我們的可視化庫,加載數(shù)據(jù)集,并且查看數(shù)據(jù)集的結(jié)構(gòu)
        import?pandas?as?pd
        import?numpy?as?np
        import?plotly.express?as?px
        #?數(shù)據(jù)集
        gapminder?=?px.data.gapminder()
        gapminder.head(10)
        img
        線圖:用year列作為x軸,lifeExp為y軸,以continent列的不同值作顏色區(qū)分,線條的數(shù)量就是country數(shù)量。
        #?line?圖
        fig?=?px.line(
        ??gapminder,??#?數(shù)據(jù)集
        ??x="year",??#?橫坐標(biāo)
        ??y="lifeExp",??#?縱坐標(biāo)
        ??color="continent",??#?顏色的數(shù)據(jù)
        ??line_group="continent",??#?線性分組
        ??hover_name="country",???#?懸停hover的數(shù)據(jù)
        ??line_shape="spline",??#?線的形狀
        ??render_mode="svg"??#?生成的圖片模式
        )
        fig.show()
        img
        因?yàn)閲业臄?shù)量太多了,所以線條很多,有點(diǎn)亂,可以點(diǎn)擊圖例隱藏一些數(shù)據(jù)以更好展現(xiàn)圖表
        面積圖
        #?area?圖
        fig?=?px.area(
        ??gapminder,??#?數(shù)據(jù)集
        ??x="year",??#?橫坐標(biāo)
        ??y="pop",??#?縱坐標(biāo)
        ??color="continent",???#?顏色
        ??line_group="country"??#?線性組別
        )
        fig.show()
        img
        散點(diǎn)圖
        #?散點(diǎn)圖
        px.scatter(
        ??gapminder???#?繪圖DataFrame數(shù)據(jù)集
        ??,x="gdpPercap"??#?橫坐標(biāo)
        ??,y="lifeExp"??#?縱坐標(biāo)
        ??,color="continent"??#?區(qū)分顏色
        ??,size="pop"???#?區(qū)分圓的大小
        ??,size_max=60??#?散點(diǎn)大小
        )
        img
        px.scatter(
        ??gapminder???#?繪圖使用的數(shù)據(jù)
        ??,x="gdpPercap"?#?橫縱坐標(biāo)使用的數(shù)據(jù)
        ??,y="lifeExp"??#?縱坐標(biāo)數(shù)據(jù)
        ??,color="continent"??#?區(qū)分顏色的屬性
        ??,size="pop"???#?區(qū)分圓的大小
        ??,size_max=60??#?圓的最大值
        ??,hover_name="country"??#?圖中可視化最上面的名字
        ??,animation_frame="year"??#?橫軸滾動(dòng)欄的屬性year
        ??,animation_group="country"??#?標(biāo)注的分組
        ??,facet_col="continent"???#?按照國家country屬性進(jìn)行分格顯示
        ??,log_x=True??#?橫坐標(biāo)表取對數(shù)
        ??,range_x=[100,100000]??#?橫軸取值范圍
        ??,range_y=[25,90]??#?縱軸范圍
        ??,labels=dict(pop="Populations",??#?屬性名字的變化,更直觀
        ???????????????gdpPercap="GDP?per?Capital",
        ???????????????lifeExp="Life?Expectancy")
        )
        img
        地理圖
        #?地理圖
        px.choropleth(
        ??gapminder,??#?數(shù)據(jù)集
        ??locations="iso_alpha",??#?配合顏色color顯示
        ??color="lifeExp",?#?顏色的字段選擇
        ??hover_name="country",??#?懸停字段名字
        ??animation_frame="year",??#?注釋
        ??color_continuous_scale=px.colors.sequential.Plasma,??#?顏色變化
        ??projection="natural?earth"??#?全球地圖
        ?????????????)
        img
        fig?=?px.scatter_geo(
        ??gapminder,???#?數(shù)據(jù)
        ??locations="iso_alpha",??#?配合顏色color顯示
        ??color="continent",?#?顏色
        ??hover_name="country",?#?懸停數(shù)據(jù)
        ??size="pop",??#?大小
        ??animation_frame="year",??#?數(shù)據(jù)幀的選擇
        ??projection="natural?earth"??#?全球地圖
        ????????????????????)

        fig.show()
        img
        fig?=?px.line_geo(
        ??gapminder,??#?數(shù)據(jù)集
        ??locations="iso_alpha",??#?配合和color顯示數(shù)據(jù)
        ??color="continent",??#?顏色
        ??projection="orthographic")???#?球形的地圖
        fig.show()
        img
        接下來換上大名鼎鼎內(nèi)置的鳶尾花數(shù)據(jù)集
        #?iris數(shù)據(jù)集
        iris?=?px.data.iris()
        iris.head(10)
        img
        繼續(xù)探索散點(diǎn)圖
        #?選擇兩個(gè)屬性作為橫縱坐標(biāo)來繪制散點(diǎn)圖
        fig?=?px.scatter(
        ??iris,??#?數(shù)據(jù)集
        ??x="sepal_width",??#?橫坐標(biāo)
        ??y="sepal_length",#?縱坐標(biāo)
        ??color="species"????
        ????????????????)
        fig.show()
        img
        px.scatter(
        ??iris,??#?數(shù)據(jù)集
        ??x="sepal_width",?#?橫坐標(biāo)
        ??y="sepal_length",??#?縱坐標(biāo)
        ??color="species",??#?顏色
        ??marginal_x="histogram",??#?橫坐標(biāo)直方圖
        ??marginal_y="rug"???#?細(xì)條圖
        )
        img
        px.scatter(
        ??iris,??#?數(shù)據(jù)集
        ??x="sepal_width",??#?橫坐標(biāo)
        ??y="sepal_length",??#?縱坐標(biāo)
        ??color="species",??#?顏色
        ??marginal_y="violin",??#?縱坐標(biāo)小提琴圖
        ??marginal_x="box",??#?橫坐標(biāo)箱型圖
        ??trendline="ols"??#?趨勢線
        )
        img
        px.scatter_matrix(
        ??iris,??#?數(shù)據(jù)
        ??dimensions=["sepal_width","sepal_length","petal_width","petal_length"],??#?維度選擇
        ??color="species")??#?顏色
        img
        平行對比圖 :通過這張圖,可以直觀發(fā)現(xiàn)數(shù)據(jù)之間的相關(guān)性
        px.parallel_coordinates(
        ??iris,???#?數(shù)據(jù)集
        ??color="species_id",??#?顏色
        ??labels={"species_id":"Species",??#?各種標(biāo)簽值
        ??????????"sepal_width":"Sepal?Width",
        ??????????"sepal_length":"Sepal?Length",
        ??????????"petal_length":"Petal?Length",
        ??????????"petal_width":"Petal?Width"},
        ??color_continuous_scale=px.colors.diverging.Tealrose,
        ??color_continuous_midpoint=2)
        img
        等高密度圖
        px.density_contour(
        ??iris,??#?繪圖數(shù)據(jù)集
        ??x="sepal_width",??#?橫坐標(biāo)
        ??y="sepal_length",??#?縱坐標(biāo)值
        ??color="species"?,?#?顏色
        ??marginal_x="histogram",?
        ??marginal_y="histogram"
        )
        img
        px.density_contour(
        ??iris,?#?數(shù)據(jù)集
        ??x="sepal_width",??#?橫坐標(biāo)值
        ??y="sepal_length",??#?縱坐標(biāo)值
        ??color="species",??#?顏色
        ??marginal_x="rug",??#?橫軸為線條圖
        ??marginal_y="histogram"???#?縱軸為直方圖
        ??????????????????)
        ??????????
        img
        熱圖
        px.density_heatmap(
        ??iris,??#?數(shù)據(jù)集
        ??x="sepal_width",???#?橫坐標(biāo)值
        ??y="sepal_length",??#?縱坐標(biāo)值
        ??marginal_y="rug",??#?縱坐標(biāo)值為線型圖
        ??marginal_x="histogram"??#?直方圖
        ??????????????????)
        img
        再換個(gè)數(shù)據(jù)集看看
        #?小票數(shù)據(jù)集
        tips?=?px.data.tips()
        tips.head()
        img
        平行種類圖 ? 前面一開始提到的?;鶊D就是基于此制作的
        fig?=?px.parallel_categories(
        ??tips,??#?數(shù)據(jù)集?
        ??color="size",??#?顏色
        ??color_continuous_scale=px.colors.sequential.Inferno)??#?顏色變化取值
        fig.show()
        img
        柱狀圖
        bar?=?px.bar(
        ????tips,?
        ???????x="sex",?
        ???????y="total_bill",?
        ???????color="smoker",?
        ???????barmode="group")
        bar.show()
        img
        fig?=?px.bar(
        ??tips,??#?數(shù)據(jù)集
        ??x="sex",??#?橫軸
        ??y="total_bill",??#?縱軸
        ??color="smoker",??#?顏色參數(shù)取值
        ??barmode="group",??#?柱狀圖模式取值
        ??facet_row="time",??#?行取值
        ??facet_col="day",??#?列元素取值
        ??category_orders={
        ????"day":?["Thur","Fri","Sat","Sun"],??#?分類順序
        ????"time":["Lunch",?"Dinner"]})
        fig.show()
        img
        盒須圖
        px.box(tips,??#?數(shù)據(jù)集
        ???????x="day",??#?橫軸數(shù)據(jù)
        ???????y="total_bill",??#?縱軸數(shù)據(jù)
        ???????color="smoker",??#?顏色
        ???????notched=True)??#?連接處的錐形部分顯示出來
        img
        小提琴圖
        px.violin(
        ????tips,???#?數(shù)據(jù)集
        ????x="smoker",??#?橫軸坐標(biāo)
        ????y="tip",??#?縱軸坐標(biāo)??
        ????color="sex",???#?顏色參數(shù)取值
        ????box=True,???#?box是顯示內(nèi)部的箱體
        ????points="all",??#?同時(shí)顯示數(shù)值點(diǎn)
        ????hover_data=tips.columns)??#?結(jié)果中顯示全部數(shù)據(jù)
        img
        最后再來看一個(gè)內(nèi)置數(shù)據(jù)集吧
        #?風(fēng)數(shù)據(jù)集
        wind?=?px.data.wind()
        wind.head(10)
        img
        極坐標(biāo)圖
        fig?=?px.scatter_polar(
        ????wind,?
        ????r?=?"frequency"?,?#?半徑
        ????theta?=?"direction",?#?角度
        ????color?=?"strength",?#?顏色
        ????symbol?=?"strength",?#?圖標(biāo)
        ????color_continuous_scale=px.colors.sequential.Inferno??#?顏色變化取值
        ????
        )
        fig.show()
        與散點(diǎn)圖結(jié)合
        img
        fig?=?px.line_polar(
        ????wind,??#?數(shù)據(jù)集
        ????r="frequency",??#?半徑
        ????theta="direction",??#?角度
        ????color="strength",??#?顏色
        ????line_close=True,??#?線性閉合
        ????color_discrete_sequence=px.colors.sequential.Plasma_r)??#?顏色變化
        fig.show()
        與線圖結(jié)合
        img
        與柱狀圖結(jié)合
        fig?=?px.bar_polar(
        ????wind,???#?數(shù)據(jù)集
        ????r="frequency",???#?半徑
        ????theta="direction",??#?角度
        ????color="strength",??#?顏色
        ????template="plotly_dark",??#?主題
        ????color_discrete_sequence=px.colors.sequential.Plasma_r)??#?顏色變化
        fig.show()
        img
        看完了這些是不是很想學(xué)習(xí)這個(gè)庫呢?那就快去找官方文檔看吧!

        最后,推薦螞蟻老師的《Python Pandas 編程100題》,限時(shí)69元!



        瀏覽 348
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            一级A片久久久免费直播间 | 免费国产黄色电影 | 一级片黄色免费 | 欧美高清无码成人网 | 免费操人网站 | 毛片二区| 国产精品免费人成网站酒店 | 影音先锋一区二区三区 | 91性视频| 韩日一级|