import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head()
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
gapminder_2007 = gapminder.loc[gapminder["year"] == 2007]
px.scatter(
gapminder_2007,
x="gdpPercap",
y="lifeExp",
size="pop", # 要素の大きさ
color="continent", # 要素の色
hover_name="country", # ホバーツールのタイトル
log_x=True, # X軸を対数にとる
size_max=60, # 要素の大きさの最大値
).show()
tips = px.data.tips()
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
scatter_matrix_fig = px.scatter_matrix(
tips,
# 描画対象の列名
dimensions=["total_bill", "tip", "size"],
color="time", # ❶ time列で色分け
symbol="smoker", # ❷ smoker列ごとにマーカの形状で分類
).show()
gapminder_Oceania = gapminder.loc[gapminder["continent"] == "Oceania"]
px.line(
gapminder_Oceania,
x="year",
y="lifeExp",
color="country", # ❶ country列でデータを分割
).show()
gapminder_Canada = gapminder.loc[gapminder["country"] == "Canada"]
px.bar(
gapminder_Canada,
x="year",
y="pop",
color="lifeExp", # ❶ 値をカラースケールで表現する列
hover_data=["lifeExp", "gdpPercap"], # ❷ ホバーツールに表示する列
).show()
px.bar(
tips,
x="sex",
y="total_bill", # ❶ time列で分割して積み上げ
color="time",
).show()
px.bar(
tips,
x="sex",
y="total_bill", # ❶ smoker列で分割してグループ化
color="smoker",
barmode="group",
).show()
px.area(
gapminder,
x="year",
y="pop", # ❶ continent列で分割
color="continent", # ❷ country列のデータごとに境界線を描画
line_group="country",
).show()
import numpy as np
import pandas as pd
np.random.seed(1)
df = pd.DataFrame(np.random.randn(100, 2), columns=["x", "y"])
px.scatter(
df,
x="x",
y="y",
error_x=np.random.rand(100) * 0.1, # X値のエラー値
error_y=np.random.rand(100) * 0.1, # Y値のエラー値
).show()
px.box(tips, x="time", y="total_bill").show()
px.box(
tips,
x="time",
y="total_bill",
color="smoker", # smoker列で色分け
notched=True, # ❶ ノッチを入れる
points="all", # ❷ すべての要素を点で描画
title="Box plot of total bill",
hover_data=["day"], # ❸ ホバーツールにday列の値を表示
).show()
px.violin(
tips,
y="tip",
x="smoker",
color="sex", # ❶ sex列で色分け
box=True, # ❷ 箱ひげ図を重ねて描画
points="all", # ❸ すべての要素を点で描画
hover_data=tips.columns, # ホバーツールにすべて列の値を表示
).show()
px.histogram(tips, x="total_bill").show()
px.histogram(
tips,
x="total_bill",
color="sex", # ❶ sex列で分割して積み上げ
marginal="rug", # ❷ ラグプロットをサブプロットで表示
hover_data=tips.columns, # ❸ ホバーツールにすべて列の値を表示
).show()
px.pie(tips, names="day", values="tip")
px.sunburst(
gapminder_2007, path=["continent", "country"], values="pop"
).show()
px.treemap(
gapminder_2007, path=["continent", "country"], values="pop"
).show()
iris = px.data.iris()
iris.head()
sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
---|---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
px.parallel_coordinates(
iris,
dimensions=["sepal_length", "sepal_width", "petal_length", "petal_width"],
color="species_id", # ❶ species_id列をカラースケールで表現
).show()
px.parallel_categories(
tips,
dimensions=["sex", "smoker", "time", "day"], # 対象列を指定
color="size", # ❶ size列で色分け
color_continuous_scale=px.colors.sequential.Inferno, # カラースケール
).show()
election = px.data.election()
election.head()
district | Coderre | Bergeron | Joly | total | winner | result | district_id | |
---|---|---|---|---|---|---|---|---|
0 | 101-Bois-de-Liesse | 2481 | 1829 | 3024 | 7334 | Joly | plurality | 101 |
1 | 102-Cap-Saint-Jacques | 2525 | 1163 | 2675 | 6363 | Joly | plurality | 102 |
2 | 11-Sault-au-Récollet | 3348 | 2770 | 2532 | 8650 | Coderre | plurality | 11 |
3 | 111-Mile-End | 1734 | 4782 | 2514 | 9030 | Bergeron | majority | 111 |
4 | 112-DeLorimier | 1770 | 5933 | 3044 | 10747 | Bergeron | majority | 112 |
px.scatter_ternary(
election,
a="Joly",
b="Coderre",
c="Bergeron",
color="winner", # ❶ winner列で色分け
size="total", # ❷ 要素の大きさをtotal列の値
size_max=15, # ❷ 要素の大きさの最大値
hover_name="district", # ❸ ホバーツールのタイトル
).show()
wind = px.data.wind()
wind.head()
direction | strength | frequency | |
---|---|---|---|
0 | N | 0-1 | 0.5 |
1 | NNE | 0-1 | 0.6 |
2 | NE | 0-1 | 0.5 |
3 | ENE | 0-1 | 0.4 |
4 | E | 0-1 | 0.4 |
px.bar_polar(
wind,
r="frequency", # 値
theta="direction", # 角度
color="strength", # ❶ strengthで分割して積み上げ
).show()
gapmider_2007 = gapminder[gapminder["year"] == 2007]
px.choropleth(
gapmider_2007,
locations="iso_alpha", # 位置をISO 3166-1 alpha-3形式で指定
color="lifeExp",
hover_name="country", # ❶ ホバーツールのタイトル
).show()
px.scatter_geo(
gapmider_2007,
locations="iso_alpha",
size="gdpPercap", # ❶ 要素のサイズ
color="lifeExp", # ❷ 要素の色
hover_name="country",
animation_frame="year", # ❸ アニメーション
).show()
scatter_3d_fig = px.scatter_3d(
gapminder,
x="year",
y="continent",
z="pop",
size="gdpPercap", # ❶ 要素の大きさ
color="lifeExp", # ❷ lifeExp列で色分け
hover_data=["country"], # ❸ ホバーツールに表示
)
# Z軸を対数にとる
scatter_3d_fig.layout.update(scene={"zaxis": {"type": "log"}})
scatter_3d_fig.show()