4000字,25张精美交互图表,开启Plotly Express之旅!
Plotly Express 是一個新的高級 Python 可視化庫,它是 Plotly.py 的高級封裝,為復雜圖表提供簡單的語法。最主要的是 Plotly 可以與 Pandas 數(shù)據(jù)類型 DataFrame 完美的結(jié)合,對于數(shù)據(jù)分析、可視化來說實在是太便捷了,而且是完全免費的,非常值得嘗試
下面我們使用 Ployly 的幾個內(nèi)置數(shù)據(jù)集來進行相關(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") gap2007Output
餐館的訂單流水
包含字段:總賬單、小費、性別、是否抽煙、星期幾、就餐時間、人數(shù)
tips?=?px.data.tips() tipsOutput
鳶尾花
包含字段:萼片長、萼片寬、花瓣長、花瓣寬、種類、種類編號
iris?=?px.data.iris()?? irisOutput
風力數(shù)據(jù)
包含字段:方向、強度、數(shù)值
wind?=?px.data.wind()?? windOutput
2013年蒙特利爾市長選舉投票結(jié)果
包括字段:區(qū)域、Coderre票數(shù)、Bergeron票數(shù)、Joly票數(shù)、總票數(shù)、勝者、結(jié)果(占比分類)
election?=?px.data.election()? electionOutput
蒙特利爾一個區(qū)域中心附近的汽車共享服務(wù)的可用性
包括字段:緯度、經(jīng)度、汽車小時數(shù)、高峰小時
carshare?=?px.data.carshare() carshareOutput
內(nèi)置調(diào)色板
Plotly 還擁有眾多色彩高級的調(diào)色板,使得我們在繪制圖表的時候不再為顏色搭配而煩惱
卡通片的色彩和序列
px.colors.carto.swatches()Output
CMOcean項目的色階
px.colors.cmocean.swatches()Output
還有其他很多調(diào)色板供選擇,就不一一展示了,下面只給出代碼,具體顏色樣式可以自行運行代碼查看
ColorBrewer2項目的色階
px.colors.colorbrewer周期性色標,適用于具有自然周期結(jié)構(gòu)的連續(xù)數(shù)據(jù)
px.colors.cyclical分散色標,適用于具有自然終點的連續(xù)數(shù)據(jù)
px.colors.diverging定性色標,適用于沒有自然順序的數(shù)據(jù)
px.colors.qualitative順序色標,適用于大多數(shù)連續(xù)數(shù)據(jù)
px.colors.sequentialPlotly Express 基本繪圖
散點圖
Plotly 繪制散點圖非常容易,一行代碼就可以完成
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp")Output
還可以通過參數(shù) color 來區(qū)分不同的數(shù)據(jù)類別
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent")Output
這里每個點都代表一個國家,不同顏色則代表不同的大洲
可以使用參數(shù) size 來體現(xiàn)數(shù)據(jù)的大小情況
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent",?size="pop",?size_max=60)Output
還可以通過參數(shù) hover_name 來指定當鼠標懸浮的時候,展示的信息
還可以根據(jù)數(shù)據(jù)集中不同的數(shù)據(jù)類型進行圖表的拆分
px.scatter(gap2007,?x="gdpPercap",?y="lifeExp",?color="continent",?size="pop",?size_max=60,?hover_name="country",?facet_col="continent",?log_x=True)Output
我們當然還可以查看不同年份的數(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
矩陣散點圖
px.scatter_matrix(iris,?dimensions=['sepal_width',?'sepal_length',?'petal_width',?'petal_length'],?color='species',?symbol='species')Output
平行坐標圖
px.parallel_coordinates(tips,?color='size',?color_continuous_scale=px.colors.sequential.Inferno)Output
三元散點圖
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
極坐標線條圖
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
極坐標條形圖
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
三維散點圖
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
地理坐標線條圖
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 還是非常強大繪圖工具,值得我們細細研究~
好了今天的分享就到這里,后續(xù)還會分享更多 Plotly 相關(guān)的知識。
-?END -
對比Excel系列圖書累積銷量達15w冊,讓你輕松掌握數(shù)據(jù)分析技能,可以在全網(wǎng)搜索書名進行了解:總結(jié)
以上是生活随笔為你收集整理的4000字,25张精美交互图表,开启Plotly Express之旅!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pandas在数据分析中的应用
- 下一篇: 做了个专为“数据分析师”打造的刷题平台!