4000字,25張精美交互圖表,開啟Plotly Express之旅
點(diǎn)擊下方“AI算法與圖像處理”,一起進(jìn)步!
重磅干貨,第一時間送達(dá)
Plotly Express 是一個新的高級 Python 可視化庫,它是 Plotly.py 的高級封裝,為復(fù)雜圖表提供簡單的語法。最主要的是 Plotly 可以與 Pandas 數(shù)據(jù)類型 DataFrame 完美的結(jié)合,對于數(shù)據(jù)分析、可視化來說實在是太便捷了,而且是完全免費(fèi)的,非常值得嘗試
下面我們使用 Ployly 的幾個內(nèi)置數(shù)據(jù)集來進(jìn)行相關(guān)圖表繪制的演示

數(shù)據(jù)集
Plotly 內(nèi)置的所有數(shù)據(jù)集都是 DataFrame 格式,也即是與 Pandas 深度契合的體現(xiàn)
不同國家歷年GDP收入與人均壽命
包含字段:國家、洲、年份、平均壽命、人口數(shù)量、GDP、國家簡稱、國家編號
gap?=?px.data.gapminder()
gap2007?=?gap.query("year==2007")
gap2007
Output

餐館的訂單流水
包含字段:總賬單、小費(fèi)、性別、是否抽煙、星期幾、就餐時間、人數(shù)
tips?=?px.data.tips()
tips
Output

鳶尾花
包含字段:萼片長、萼片寬、花瓣長、花瓣寬、種類、種類編號
iris?=?px.data.iris()??
iris
Output

風(fēng)力數(shù)據(jù)
包含字段:方向、強(qiáng)度、數(shù)值
wind?=?px.data.wind()??
wind
Output

2013年蒙特利爾市長選舉投票結(jié)果
包括字段:區(qū)域、Coderre票數(shù)、Bergeron票數(shù)、Joly票數(shù)、總票數(shù)、勝者、結(jié)果(占比分類)
election?=?px.data.election()?
election
Output

蒙特利爾一個區(qū)域中心附近的汽車共享服務(wù)的可用性
包括字段:緯度、經(jīng)度、汽車小時數(shù)、高峰小時
carshare?=?px.data.carshare()
carshare
Output


內(nèi)置調(diào)色板
Plotly 還擁有眾多色彩高級的調(diào)色板,使得我們在繪制圖表的時候不再為顏色搭配而煩惱
卡通片的色彩和序列
px.colors.carto.swatches()?
Output

CMOcean項目的色階
px.colors.cmocean.swatches()?
Output

還有其他很多調(diào)色板供選擇,就不一一展示了,下面只給出代碼,具體顏色樣式可以自行運(yùn)行代碼查看
ColorBrewer2項目的色階
px.colors.colorbrewer
周期性色標(biāo),適用于具有自然周期結(jié)構(gòu)的連續(xù)數(shù)據(jù)
px.colors.cyclical?
分散色標(biāo),適用于具有自然終點(diǎn)的連續(xù)數(shù)據(jù)
px.colors.diverging?????
定性色標(biāo),適用于沒有自然順序的數(shù)據(jù)
px.colors.qualitative
順序色標(biāo),適用于大多數(shù)連續(xù)數(shù)據(jù)
px.colors.sequential????????

Plotly Express 基本繪圖
散點(diǎn)圖
Plotly 繪制散點(diǎn)圖非常容易,一行代碼就可以完成
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp")
Output

還可以通過參數(shù) color 來區(qū)分不同的數(shù)據(jù)類別
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent")
Output

這里每個點(diǎn)都代表一個國家,不同顏色則代表不同的大洲
可以使用參數(shù) size 來體現(xiàn)數(shù)據(jù)的大小情況
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent",?size="pop",?size_max=60)
Output

還可以通過參數(shù) hover_name 來指定當(dāng)鼠標(biāo)懸浮的時候,展示的信息

還可以根據(jù)數(shù)據(jù)集中不同的數(shù)據(jù)類型進(jìn)行圖表的拆分
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent",?size="pop",?
???????????size_max=60,?hover_name="country",?facet_col="continent",?log_x=True)
Output

我們當(dāng)然還可以查看不同年份的數(shù)據(jù),生成自動切換的動態(tài)圖表
px.scatter(gap,?x="gdpPercap",?y="lifeExp",?color="continent",?size="pop",?
???????????size_max=60,?hover_name="country",?animation_frame="year",?animation_group="country",?log_x=True,
??????????range_x=[100,?100000],?range_y=[25,?90],?labels=dict(pop="Population",?gdpPercap="GDP?per?Capa",?lifeExp="Life?Expectancy"))
Output

地理信息圖
Plotly 繪制動態(tài)的地理信息圖表也是非常方便,通過這種地圖的形式,我們也可以清楚的看到數(shù)據(jù)集中缺少前蘇聯(lián)的相關(guān)數(shù)據(jù)
px.choropleth(gap,?locations="iso_alpha",?color="lifeExp",?hover_name="country",?animation_frame="year",?
??????????????color_continuous_scale=px.colors.sequential.Plasma,?projection="natural?earth")
Output

矩陣散點(diǎn)圖
px.scatter_matrix(iris,?dimensions=['sepal_width',?'sepal_length',?'petal_width',?'petal_length'],?color='species',?symbol='species')
Output

平行坐標(biāo)圖
px.parallel_coordinates(tips,?color='size',?color_continuous_scale=px.colors.sequential.Inferno)
Output

三元散點(diǎn)圖
px.scatter_ternary(election,?a="Joly",?b="Coderre",?c="Bergeron",?color="winner",?size="total",?hover_name="district",
???????????????????size_max=15,?color_discrete_map?=?{"Joly":?"blue",?
???????????????????"Bergeron":?"green",?"Coderre":"red"}?)
Output

極坐標(biāo)線條圖
px.line_polar(wind,?r="frequency",?theta="direction",?color="strength",?
????????????line_close=True,color_discrete_sequence=px.colors.sequential.Plotly3[-2::-1])
Output

小提琴圖
px.violin(tips,?y="tip",?x="sex",?color="smoker",?facet_col="day",?facet_row="time",box=True,?points="all",?
??????????category_orders={"day":?["Thur",?"Fri",?"Sat",?"Sun"],?"time":?["Lunch",?"Dinner"]},
??????????hover_data=tips.columns)
Output

極坐標(biāo)條形圖
px.bar_polar(wind,?r="frequency",?theta="direction",?color="strength",
????????????color_discrete_sequence=?px.colors.sequential.Plotly3[-2::-1])
Output

并行類別圖
px.parallel_categories(tips,?color="size",?color_continuous_scale=px.
????????????colors.sequential.Inferno)
Output

直方圖
px.histogram(tips,?x="total_bill",?color="smoker",facet_row="day",?facet_col="time")
Output

三維散點(diǎn)圖
px.scatter_3d(election,?x="Joly",?y="Coderre",?z="Bergeron",?color="winner",?
??????????????size="total",?hover_name="district",symbol="result",?
??????????????color_discrete_map?=?{"Joly":?"blue",?"Bergeron":?"green",?
??????????????"Coderre":"red"})
Output

密度等值線圖
px.density_contour(iris,?x="sepal_width",?y="sepal_length",?color="species")
Output

箱形圖
px.box(tips,?x="sex",?y="tip",?color="smoker",?notched=True)
Output

地理坐標(biāo)線條圖
px.line_geo(gap.query("year==2007"),?locations="iso_alpha",?
????????????color="continent",?projection="orthographic")
Output

條線圖
px.line(gap,?x="year",?y="lifeExp",?color="continent",?
????????line_group="country",?hover_name="country",
????????line_shape="spline",?render_mode="svg")
Output

面積圖
px.area(gap,?x="year",?y="pop",?color="continent",?
????????line_group="country")
Output

熱力圖
px.density_heatmap(iris,?x="sepal_width",?y="sepal_length",?
???????????????????marginal_x="rug",?marginal_y="histogram")
Output

條形圖
px.bar(tips,?x="sex",?y="total_bill",?color="smoker",?barmode="group")
Output

總體來說,Plotly/Plotly Express 還是非常強(qiáng)大繪圖工具,值得我們細(xì)細(xì)研究~
交流群
歡迎加入公眾號讀者群一起和同行交流,目前有美顏、三維視覺、計算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN、算法競賽等微信群
個人微信(如果沒有備注不拉群!) 請注明:地區(qū)+學(xué)校/企業(yè)+研究方向+昵稱
下載1:何愷明頂會分享
在「AI算法與圖像處理」公眾號后臺回復(fù):何愷明,即可下載。總共有6份PDF,涉及 ResNet、Mask RCNN等經(jīng)典工作的總結(jié)分析
下載2:終身受益的編程指南:Google編程風(fēng)格指南
在「AI算法與圖像處理」公眾號后臺回復(fù):c++,即可下載。歷經(jīng)十年考驗,最權(quán)威的編程規(guī)范!
下載3 CVPR2021 在「AI算法與圖像處理」公眾號后臺回復(fù):CVPR,即可下載1467篇CVPR?2020論文 和 CVPR 2021 最新論文

