こんな人にオススメ
px
にscatter_matrix
っていう関数があるんだけど、これは一体何?
公式の例だとsubplotみたいに描画されているんだけど。
ということで、今回はplotly
のpx
のscatter_matrix
について解説する。scatter_matrix
は簡単にいうと複数種類の軸をsubplot(サブプロット)するグラフ。
以下の記事でpx
のsubplot
について解説したが、このsubplot
はある一定の軸を持つグラフをある基準でsunplot
するってものだった。以下の記事だと横軸がGDPで縦軸が平均寿命で固定。
-
-
【px&facet】plotly.expressでsubplotsを描く
続きを見る
しかし、scatter_matrix
の場合だと複数種の軸でグラフを作成できるので、データA x データBのグラフ、データA x データCのグラフといった風に様々な種類のグラフが描ける。
なお、scatter_matrix
だとアニメーションもできないのでここは使い勝手が悪め。
-
-
【px&animation】plotly.expressでアニメーションのグラフを作成
続きを見る
python環境は以下。
- Python 3.10.0
- numpy 1.21.4
- matplotlib 3.5.0
- plotly 5.4.0
- plotly-orca 3.4.2
作成したコード全文
+ クリックでオープン
import matplotlib.cm as cm
import numpy as np
import plotly
import plotly.io as pio
import plotly.express as px
print('------------------------------------------------------------')
# グラフ保存用の関数
def save(fig, config, save_name):
pio.orca.config.executable = '/Applications/orca.app/Contents/MacOS/orca'
pio.write_html(fig, f"{save_name}.html", config=config,)
pio.write_image(fig, f"{save_name}.png")
# scatter_matrix用の関数
def scatter_matrix(df, save_name,
update_traces={}, update_layout={}, **kwargs):
fig = px.scatter_matrix(df, **kwargs)
fig.update_traces(**update_traces) # 追加でfigの修正・設定を行う用
fig.update_layout(**update_layout) # 追加でlayoutの修正・設定を行う用
# fig.show()
prefix = 'px-scattermatrix'
save(fig=fig, config=None, save_name=f"{prefix}_{save_name}")
print('------------------------------------------------------------')
# irisのデータでプロット
df = px.data.iris()
print(df)
# sepal_length sepal_width ... species species_id
# 0 5.1 3.5 ... setosa 1
# 1 4.9 3.0 ... setosa 1
# 2 4.7 3.2 ... setosa 1
# 3 4.6 3.1 ... setosa 1
# 4 5.0 3.6 ... setosa 1
# .. ... ... ... ... ...
# 145 6.7 3.0 ... virginica 3
# 146 6.3 2.5 ... virginica 3
# 147 6.5 3.0 ... virginica 3
# 148 6.2 3.4 ... virginica 3
# 149 5.9 3.0 ... virginica 3
# [150 rows x 6 columns]
print(df.columns.values)
# ['sepal_length' 'sepal_width' 'petal_length' 'petal_width' 'species'
# 'species_id']
print(df['species'].unique())
# ['setosa' 'versicolor' 'virginica']
print(df['species_id'].unique())
# [1 2 3]
# シンプルにグラフ化
scatter_matrix(df=df, save_name='iris')
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# 色分け
scatter_matrix(
df=df, save_name='iris_color',
color='species', # ヘッダ名speciesで色分け
)
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# グラフ化したいデータだけ使用
scatter_matrix(
df=df, save_name='iris_species_dimensions',
color='species',
# グラフ化したいヘッダ名を指定
dimensions=['sepal_width', 'sepal_length', 'petal_width', 'petal_length'],
)
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# 対角線のグラフは同じデータなので削除
scatter_matrix(
df=df, save_name='iris_species_dimensions_diagonal_visible',
color='species',
dimensions=['sepal_width', 'sepal_length', 'petal_width', 'petal_length'],
# 対角線のデータは削除
update_traces=dict(diagonal_visible=False),
)
print('------------------------------------------------------------')
# gapminderのデータを使ってみる
df = px.data.gapminder()
print(df)
# country continent year ... gdpPercap iso_alpha iso_num
# 0 Afghanistan Asia 1952 ... 779.445314 AFG 4
# 1 Afghanistan Asia 1957 ... 820.853030 AFG 4
# 2 Afghanistan Asia 1962 ... 853.100710 AFG 4
# 3 Afghanistan Asia 1967 ... 836.197138 AFG 4
# 4 Afghanistan Asia 1972 ... 739.981106 AFG 4
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 706.157306 ZWE 716
# 1700 Zimbabwe Africa 1992 ... 693.420786 ZWE 716
# 1701 Zimbabwe Africa 1997 ... 792.449960 ZWE 716
# 1702 Zimbabwe Africa 2002 ... 672.038623 ZWE 716
# 1703 Zimbabwe Africa 2007 ... 469.709298 ZWE 716
# [1704 rows x 8 columns]
for header in df:
print(header)
print(df[header].unique())
print() # 1行あける
# country
# ['Afghanistan' 'Albania' 'Algeria' 'Angola' 'Argentina' 'Australia'
# 'Austria' 'Bahrain' 'Bangladesh' 'Belgium' 'Benin' 'Bolivia'
# 'Bosnia and Herzegovina' 'Botswana' 'Brazil' 'Bulgaria' 'Burkina Faso'
# 'Burundi' 'Cambodia' 'Cameroon' 'Canada' 'Central African Republic'
# 'Chad' 'Chile' 'China' 'Colombia' 'Comoros' 'Congo, Dem. Rep.'
# 'Congo, Rep.' 'Costa Rica' "Cote d'Ivoire" 'Croatia' 'Cuba'
# 'Czech Republic' 'Denmark' 'Djibouti' 'Dominican Republic' 'Ecuador'
# 'Egypt' 'El Salvador' 'Equatorial Guinea' 'Eritrea' 'Ethiopia' 'Finland'
# 'France' 'Gabon' 'Gambia' 'Germany' 'Ghana' 'Greece' 'Guatemala' 'Guinea'
# 'Guinea-Bissau' 'Haiti' 'Honduras' 'Hong Kong, China' 'Hungary' 'Iceland'
# 'India' 'Indonesia' 'Iran' 'Iraq' 'Ireland' 'Israel' 'Italy' 'Jamaica'
# 'Japan' 'Jordan' 'Kenya' 'Korea, Dem. Rep.' 'Korea, Rep.' 'Kuwait'
# 'Lebanon' 'Lesotho' 'Liberia' 'Libya' 'Madagascar' 'Malawi' 'Malaysia'
# 'Mali' 'Mauritania' 'Mauritius' 'Mexico' 'Mongolia' 'Montenegro'
# 'Morocco' 'Mozambique' 'Myanmar' 'Namibia' 'Nepal' 'Netherlands'
# 'New Zealand' 'Nicaragua' 'Niger' 'Nigeria' 'Norway' 'Oman' 'Pakistan'
# 'Panama' 'Paraguay' 'Peru' 'Philippines' 'Poland' 'Portugal'
# 'Puerto Rico' 'Reunion' 'Romania' 'Rwanda' 'Sao Tome and Principe'
# 'Saudi Arabia' 'Senegal' 'Serbia' 'Sierra Leone' 'Singapore'
# 'Slovak Republic' 'Slovenia' 'Somalia' 'South Africa' 'Spain' 'Sri Lanka'
# 'Sudan' 'Swaziland' 'Sweden' 'Switzerland' 'Syria' 'Taiwan' 'Tanzania'
# 'Thailand' 'Togo' 'Trinidad and Tobago' 'Tunisia' 'Turkey' 'Uganda'
# 'United Kingdom' 'United States' 'Uruguay' 'Venezuela' 'Vietnam'
# 'West Bank and Gaza' 'Yemen, Rep.' 'Zambia' 'Zimbabwe']
# continent
# ['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']
# year
# [1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007]
# lifeExp
# [28.801 30.332 31.997 ... 46.809 39.989 43.487]
# pop
# [ 8425333 9240934 10267083 ... 11404948 11926563 12311143]
# gdpPercap
# [779.4453145 820.8530296 853.10071 ... 792.4499603 672.0386227
# 469.7092981]
# iso_alpha
# ['AFG' 'ALB' 'DZA' 'AGO' 'ARG' 'AUS' 'AUT' 'BHR' 'BGD' 'BEL' 'BEN' 'BOL'
# 'BIH' 'BWA' 'BRA' 'BGR' 'BFA' 'BDI' 'KHM' 'CMR' 'CAN' 'CAF' 'TCD' 'CHL'
# 'CHN' 'COL' 'COM' 'COD' 'COG' 'CRI' 'CIV' 'HRV' 'CUB' 'CZE' 'DNK' 'DJI'
# 'DOM' 'ECU' 'EGY' 'SLV' 'GNQ' 'ERI' 'ETH' 'FIN' 'FRA' 'GAB' 'GMB' 'DEU'
# 'GHA' 'GRC' 'GTM' 'GIN' 'GNB' 'HTI' 'HND' 'HKG' 'HUN' 'ISL' 'IND' 'IDN'
# 'IRN' 'IRQ' 'IRL' 'ISR' 'ITA' 'JAM' 'JPN' 'JOR' 'KEN' 'KOR' 'KWT' 'LBN'
# 'LSO' 'LBR' 'LBY' 'MDG' 'MWI' 'MYS' 'MLI' 'MRT' 'MUS' 'MEX' 'MNG' 'MNE'
# 'MAR' 'MOZ' 'MMR' 'NAM' 'NPL' 'NLD' 'NZL' 'NIC' 'NER' 'NGA' 'NOR' 'OMN'
# 'PAK' 'PAN' 'PRY' 'PER' 'PHL' 'POL' 'PRT' 'PRI' 'REU' 'ROU' 'RWA' 'STP'
# 'SAU' 'SEN' 'SRB' 'SLE' 'SGP' 'SVK' 'SVN' 'SOM' 'ZAF' 'ESP' 'LKA' 'SDN'
# 'SWZ' 'SWE' 'CHE' 'SYR' 'TWN' 'TZA' 'THA' 'TGO' 'TTO' 'TUN' 'TUR' 'UGA'
# 'GBR' 'USA' 'URY' 'VEN' 'VNM' 'PSE' 'YEM' 'ZMB' 'ZWE']
# iso_num
# [ 4 8 12 24 32 36 40 48 50 56 204 68 70 72 76 100 854 108
# 116 120 124 140 148 152 156 170 174 180 178 188 384 191 192 203 208 262
# 214 218 818 222 226 232 231 246 250 266 270 276 288 300 320 324 624 332
# 340 344 348 352 356 360 364 368 372 376 380 388 392 400 404 410 414 422
# 426 430 434 450 454 458 466 478 480 484 496 499 504 508 104 516 524 528
# 554 558 562 566 578 512 586 591 600 604 608 616 620 630 638 642 646 678
# 682 686 688 694 702 703 705 706 710 724 144 736 748 752 756 760 158 834
# 764 768 780 788 792 800 826 840 858 862 704 275 887 894 716]
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# そのままだとごちゃごちゃ
scatter_matrix(df=df, save_name='gapminder',)
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# 大陸ごとに色分け
scatter_matrix(
df=df, save_name='gapminder_color', color='continent',
update_traces=dict(diagonal_visible=False),
)
print(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
# 国ごとに色分けするために国の数だけカラースケールを作成
# plotlyのカラースケールだと色が足りない
countries = df['country'].unique()
color = []
for num, _ in enumerate(countries):
cmap = cm.jet(num / len(countries))
rgb = f"rgb{plotly.colors.convert_to_RGB_255(cmap)}"
color.append(rgb)
print(color)
# ['rgb(0, 0, 128)', 'rgb(0, 0, 132)', 'rgb(0, 0, 141)', 'rgb(0, 0, 150)', 'rgb(0, 0, 159)', 'rgb(0, 0, 168)', 'rgb(0, 0, 173)', 'rgb(0, 0, 182)', 'rgb(0, 0, 191)', 'rgb(0, 0, 200)', 'rgb(0, 0, 209)', 'rgb(0, 0, 214)', 'rgb(0, 0, 223)', 'rgb(0, 0, 232)', 'rgb(0, 0, 241)', 'rgb(0, 0, 250)', 'rgb(0, 0, 255)', 'rgb(0, 0, 255)', 'rgb(0, 0, 255)', 'rgb(0, 8, 255)', 'rgb(0, 16, 255)', 'rgb(0, 20, 255)', 'rgb(0, 28, 255)', 'rgb(0, 36, 255)', 'rgb(0, 44, 255)', 'rgb(0, 52, 255)', 'rgb(0, 56, 255)', 'rgb(0, 64, 255)', 'rgb(0, 72, 255)', 'rgb(0, 80, 255)', 'rgb(0, 88, 255)', 'rgb(0, 92, 255)', 'rgb(0, 100, 255)', 'rgb(0, 108, 255)', 'rgb(0, 116, 255)', 'rgb(0, 124, 255)', 'rgb(0, 128, 255)', 'rgb(0, 136, 255)', 'rgb(0, 144, 255)', 'rgb(0, 152, 255)', 'rgb(0, 160, 255)', 'rgb(0, 164, 255)', 'rgb(0, 172, 255)', 'rgb(0, 180, 255)', 'rgb(0, 188, 255)', 'rgb(0, 196, 255)', 'rgb(0, 200, 255)', 'rgb(0, 208, 255)', 'rgb(0, 216, 255)', 'rgb(0, 224, 251)', 'rgb(2, 232, 244)', 'rgb(6, 236, 241)', 'rgb(12, 244, 235)', 'rgb(19, 252, 228)', 'rgb(25, 255, 222)', 'rgb(31, 255, 215)', 'rgb(35, 255, 212)', 'rgb(41, 255, 206)', 'rgb(48, 255, 199)', 'rgb(54, 255, 193)', 'rgb(60, 255, 186)', 'rgb(64, 255, 183)', 'rgb(70, 255, 177)', 'rgb(77, 255, 170)', 'rgb(83, 255, 164)', 'rgb(90, 255, 157)', 'rgb(93, 255, 154)', 'rgb(99, 255, 148)', 'rgb(106, 255, 141)', 'rgb(112, 255, 135)', 'rgb(119, 255, 128)', 'rgb(125, 255, 122)', 'rgb(128, 255, 119)', 'rgb(135, 255, 112)', 'rgb(141, 255, 106)', 'rgb(148, 255, 99)', 'rgb(154, 255, 93)', 'rgb(157, 255, 90)', 'rgb(164, 255, 83)', 'rgb(170, 255, 77)', 'rgb(177, 255, 70)', 'rgb(183, 255, 64)', 'rgb(186, 255, 60)', 'rgb(193, 255, 54)', 'rgb(199, 255, 48)', 'rgb(206, 255, 41)', 'rgb(212, 255, 35)', 'rgb(215, 255, 31)', 'rgb(222, 255, 25)', 'rgb(228, 255, 19)', 'rgb(235, 255, 12)', 'rgb(241, 252, 6)', 'rgb(244, 248, 2)', 'rgb(251, 241, 0)', 'rgb(255, 234, 0)', 'rgb(255, 226, 0)', 'rgb(255, 219, 0)', 'rgb(255, 215, 0)', 'rgb(255, 208, 0)', 'rgb(255, 200, 0)', 'rgb(255, 193, 0)', 'rgb(255, 185, 0)', 'rgb(255, 182, 0)', 'rgb(255, 174, 0)', 'rgb(255, 167, 0)', 'rgb(255, 159, 0)', 'rgb(255, 152, 0)', 'rgb(255, 148, 0)', 'rgb(255, 141, 0)', 'rgb(255, 134, 0)', 'rgb(255, 126, 0)', 'rgb(255, 119, 0)', 'rgb(255, 115, 0)', 'rgb(255, 108, 0)', 'rgb(255, 100, 0)', 'rgb(255, 93, 0)', 'rgb(255, 85, 0)', 'rgb(255, 82, 0)', 'rgb(255, 74, 0)', 'rgb(255, 67, 0)', 'rgb(255, 59, 0)', 'rgb(255, 52, 0)', 'rgb(255, 48, 0)', 'rgb(255, 41, 0)', 'rgb(255, 34, 0)', 'rgb(255, 26, 0)', 'rgb(255, 19, 0)', 'rgb(250, 15, 0)', 'rgb(241, 8, 0)', 'rgb(232, 0, 0)', 'rgb(223, 0, 0)', 'rgb(214, 0, 0)', 'rgb(209, 0, 0)', 'rgb(200, 0, 0)', 'rgb(191, 0, 0)', 'rgb(182, 0, 0)', 'rgb(173, 0, 0)', 'rgb(168, 0, 0)', 'rgb(159, 0, 0)', 'rgb(150, 0, 0)', 'rgb(141, 0, 0)', 'rgb(132, 0, 0)']
scatter_matrix(
df=df, save_name='gapminder_color_discrete_sequence', color='country',
dimensions=['continent', 'year', 'lifeExp', 'pop', 'gdpPercap'],
color_discrete_sequence=color, # 色分けの指定
hover_data=df, # ホバーで表示する内容はdfの内容全部とする
update_traces=dict(diagonal_visible=False),
)
print('------------------------------------------------------------')
# gapminderのデータにlog列を追加
df = px.data.gapminder()
print(df)
# country continent year ... gdpPercap iso_alpha iso_num
# 0 Afghanistan Asia 1952 ... 779.445314 AFG 4
# 1 Afghanistan Asia 1957 ... 820.853030 AFG 4
# 2 Afghanistan Asia 1962 ... 853.100710 AFG 4
# 3 Afghanistan Asia 1967 ... 836.197138 AFG 4
# 4 Afghanistan Asia 1972 ... 739.981106 AFG 4
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 706.157306 ZWE 716
# 1700 Zimbabwe Africa 1992 ... 693.420786 ZWE 716
# 1701 Zimbabwe Africa 1997 ... 792.449960 ZWE 716
# 1702 Zimbabwe Africa 2002 ... 672.038623 ZWE 716
# 1703 Zimbabwe Africa 2007 ... 469.709298 ZWE 716
# [1704 rows x 8 columns]
# 人口と1人あたりGDPの値にlogでとる
df['log_pop'] = np.log10(df['pop'])
df['log_gdpPercap'] = np.log10(df['gdpPercap'])
print(df)
# country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [1704 rows x 10 columns]
scatter_matrix(
df=df, save_name='gapminder_logscale', color='country',
dimensions=['continent', 'year', 'lifeExp', 'log_pop', 'log_gdpPercap', ],
color_discrete_sequence=color,
hover_data=df,
update_traces=dict(
diagonal_visible=False,
# マーカーのサイズを小さめに
marker_size=3,
),
)
print('------------------------------------------------------------')
# とある年のデータで作成
print(df)
# country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [1704 rows x 10 columns]
# 元のデータフレームにある年でフィルターをかける
single_year = {}
for year in df['year'].unique():
# queryを使用時に変数を使用したいなら@をつける
single_year[year] = df.query('year == @year')
print(single_year)
# {1952: country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 12 Albania Europe 1952 ... 8 6.108124 3.204407
# 24 Algeria Africa 1952 ... 12 6.967526 3.388990
# 36 Angola Africa 1952 ... 24 6.626555 3.546618
# 48 Argentina Americas 1952 ... 32 7.252294 3.771684
# ... ... ... ... ... ... ... ...
# 1644 Vietnam Asia 1952 ... 704 7.419077 2.781803
# 1656 West Bank and Gaza Asia 1952 ... 275 6.013084 3.180582
# 1668 Yemen, Rep. Asia 1952 ... 887 6.695817 2.893050
# 1680 Zambia Africa 1952 ... 894 6.426836 3.059711
# 1692 Zimbabwe Africa 1952 ... 716 6.488679 2.609471
# [142 rows x 10 columns], 1957: country continent year ... iso_num log_pop log_gdpPercap
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 13 Albania Europe 1957 ... 8 6.169235 3.288313
# 25 Algeria Africa 1957 ... 12 7.011607 3.479140
# 37 Angola Africa 1957 ... 24 6.659094 3.582965
# 49 Argentina Americas 1957 ... 32 7.292490 3.836125
# ... ... ... ... ... ... ... ...
# 1645 Vietnam Asia 1957 ... 704 7.462376 2.830130
# 1657 West Bank and Gaza Asia 1957 ... 275 6.029562 3.261755
# 1669 Yemen, Rep. Asia 1957 ... 887 6.740212 2.905704
# 1681 Zambia Africa 1957 ... 894 6.479431 3.117920
# 1693 Zimbabwe Africa 1957 ... 716 6.561857 2.714970
# [142 rows x 10 columns], 1962: country continent year ... iso_num log_pop log_gdpPercap
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 14 Albania Europe 1962 ... 8 6.237578 3.364155
# 26 Algeria Africa 1962 ... 12 7.041430 3.406679
# 38 Angola Africa 1962 ... 24 6.683589 3.630354
# 50 Argentina Americas 1962 ... 32 7.328049 3.853282
# ... ... ... ... ... ... ... ...
# 1646 Vietnam Asia 1962 ... 704 7.528867 2.887645
# 1658 West Bank and Gaza Asia 1962 ... 275 6.054281 3.342217
# 1670 Yemen, Rep. Asia 1962 ... 887 6.786757 2.916782
# 1682 Zambia Africa 1962 ... 894 6.534153 3.162184
# 1694 Zimbabwe Africa 1962 ... 716 6.631214 2.722035
# [142 rows x 10 columns], 1967: country continent year ... iso_num log_pop log_gdpPercap
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 15 Albania Europe 1967 ... 8 6.297555 3.440940
# 27 Algeria Africa 1967 ... 12 7.105868 3.511481
# 39 Angola Africa 1967 ... 24 6.719950 3.742157
# 51 Argentina Americas 1967 ... 32 7.360484 3.905955
# ... ... ... ... ... ... ... ...
# 1647 Vietnam Asia 1967 ... 704 7.596200 2.804223
# 1659 West Bank and Gaza Asia 1967 ... 275 6.057908 3.423199
# 1671 Yemen, Rep. Asia 1967 ... 887 6.828710 2.935730
# 1683 Zambia Africa 1967 ... 894 6.591065 3.249706
# 1695 Zimbabwe Africa 1967 ... 716 6.698573 2.755719
# [142 rows x 10 columns], 1972: country continent year ... iso_num log_pop log_gdpPercap
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# 16 Albania Europe 1972 ... 8 6.354791 3.520277
# 28 Algeria Africa 1972 ... 12 7.169110 3.621453
# 40 Angola Africa 1972 ... 24 6.770473 3.738248
# 52 Argentina Americas 1972 ... 32 7.394098 3.975112
# ... ... ... ... ... ... ... ...
# 1648 Vietnam Asia 1972 ... 704 7.649870 2.844789
# 1660 West Bank and Gaza Asia 1972 ... 275 6.037256 3.496017
# 1672 Yemen, Rep. Asia 1972 ... 887 6.869647 3.102107
# 1684 Zambia Africa 1972 ... 894 6.653839 3.248831
# 1696 Zimbabwe Africa 1972 ... 716 6.767982 2.902744
# [142 rows x 10 columns], 1977: country continent year ... iso_num log_pop log_gdpPercap
# 5 Afghanistan Asia 1977 ... 4 7.172614 2.895485
# 17 Albania Europe 1977 ... 8 6.399509 3.548144
# 29 Algeria Africa 1977 ... 12 7.234335 3.691118
# 41 Angola Africa 1977 ... 24 6.789769 3.478371
# 53 Argentina Americas 1977 ... 32 7.431104 4.003419
# ... ... ... ... ... ... ... ...
# 1649 Vietnam Asia 1977 ... 704 7.703579 2.853417
# 1661 West Bank and Gaza Asia 1977 ... 275 6.100746 3.566182
# 1673 Yemen, Rep. Asia 1977 ... 887 6.924486 3.262395
# 1685 Zambia Africa 1977 ... 894 6.717383 3.201039
# 1697 Zimbabwe Africa 1977 ... 716 6.822306 2.836063
# [142 rows x 10 columns], 1982: country continent year ... iso_num log_pop log_gdpPercap
# 6 Afghanistan Asia 1982 ... 4 7.109977 2.990344
# 18 Albania Europe 1982 ... 8 6.444060 3.560012
# 30 Algeria Africa 1982 ... 12 7.301762 3.759302
# 42 Angola Africa 1982 ... 24 6.846113 3.440429
# 54 Argentina Americas 1982 ... 32 7.467480 3.954141
# ... ... ... ... ... ... ... ...
# 1650 Vietnam Asia 1982 ... 704 7.749289 2.849564
# 1662 West Bank and Gaza Asia 1982 ... 275 6.154082 3.637092
# 1674 Yemen, Rep. Asia 1982 ... 887 6.984870 3.296129
# 1686 Zambia Africa 1982 ... 894 6.785359 3.148812
# 1698 Zimbabwe Africa 1982 ... 716 6.882896 2.896997
# [142 rows x 10 columns], 1987: country continent year ... iso_num log_pop log_gdpPercap
# 7 Afghanistan Asia 1987 ... 4 7.142012 2.930641
# 19 Albania Europe 1987 ... 8 6.487890 3.572748
# 31 Algeria Africa 1987 ... 12 7.366516 3.754452
# 43 Angola Africa 1987 ... 24 6.896208 3.385644
# 55 Argentina Americas 1987 ... 32 7.499974 3.960931
# ... ... ... ... ... ... ... ...
# 1651 Vietnam Asia 1987 ... 704 7.798143 2.914237
# 1663 West Bank and Gaza Asia 1987 ... 275 6.228198 3.708183
# 1675 Yemen, Rep. Asia 1987 ... 887 7.049967 3.294850
# 1687 Zambia Africa 1987 ... 894 6.861678 3.083974
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# [142 rows x 10 columns], 1992: country continent year ... iso_num log_pop log_gdpPercap
# 8 Afghanistan Asia 1992 ... 4 7.212665 2.812473
# 20 Albania Europe 1992 ... 8 6.521987 3.397495
# 32 Algeria Africa 1992 ... 12 7.419929 3.700982
# 44 Angola Africa 1992 ... 24 6.941312 3.419600
# 56 Argentina Americas 1992 ... 32 7.530954 3.968876
# ... ... ... ... ... ... ... ...
# 1652 Vietnam Asia 1992 ... 704 7.844730 2.995206
# 1664 West Bank and Gaza Asia 1992 ... 275 6.323207 3.779427
# 1676 Yemen, Rep. Asia 1992 ... 887 7.126066 3.274042
# 1688 Zambia Africa 1992 ... 894 6.923304 3.083103
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# [142 rows x 10 columns], 1997: country continent year ... iso_num log_pop log_gdpPercap
# 9 Afghanistan Asia 1997 ... 4 7.346889 2.803007
# 21 Albania Europe 1997 ... 8 6.535046 3.504206
# 33 Algeria Africa 1997 ... 12 7.463475 3.680996
# 45 Angola Africa 1997 ... 24 6.994538 3.357390
# 57 Argentina Americas 1997 ... 32 7.558750 4.040099
# ... ... ... ... ... ... ... ...
# 1653 Vietnam Asia 1997 ... 704 7.881093 3.141731
# 1665 West Bank and Gaza Asia 1997 ... 275 6.451179 3.851910
# 1677 Yemen, Rep. Asia 1997 ... 887 7.199385 3.325820
# 1689 Zambia Africa 1997 ... 894 6.973949 3.029933
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# [142 rows x 10 columns], 2002: country continent year ... iso_num log_pop log_gdpPercap
# 10 Afghanistan Asia 2002 ... 4 7.402578 2.861376
# 22 Albania Europe 2002 ... 8 6.545123 3.663155
# 34 Algeria Africa 2002 ... 12 7.495366 3.723295
# 46 Angola Africa 2002 ... 24 7.036074 3.442995
# 58 Argentina Americas 2002 ... 32 7.583552 3.944366
# ... ... ... ... ... ... ... ...
# 1654 Vietnam Asia 2002 ... 704 7.907992 3.246611
# 1666 West Bank and Gaza Asia 2002 ... 275 6.530146 3.654705
# 1678 Yemen, Rep. Asia 2002 ... 887 7.271871 3.349243
# 1690 Zambia Africa 2002 ... 894 7.025134 3.030038
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# [142 rows x 10 columns], 2007: country continent year ... iso_num log_pop log_gdpPercap
# 11 Afghanistan Asia 2007 ... 4 7.503653 2.988818
# 23 Albania Europe 2007 ... 8 6.556366 3.773569
# 35 Algeria Africa 2007 ... 12 7.522877 3.794025
# 47 Angola Africa 2007 ... 24 7.094138 3.680991
# 59 Argentina Americas 2007 ... 32 7.605326 4.106510
# ... ... ... ... ... ... ... ...
# 1655 Vietnam Asia 2007 ... 704 7.930757 3.387670
# 1667 West Bank and Gaza Asia 2007 ... 275 6.604046 3.480776
# 1679 Yemen, Rep. Asia 2007 ... 887 7.346583 3.358081
# 1691 Zambia Africa 2007 ... 894 7.069891 3.104218
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [142 rows x 10 columns]}
# レイアウト設定
layouts = {
'xaxis1': dict(range=(-0.5, 4.5)), # continentの表示範囲(index扱い)
'xaxis2': dict(range=(20, 85)), # lifeExpの表示範囲
'xaxis3': dict(range=(4, 10)), # log_popの表示範囲
'xaxis4': dict(range=(2, 5)), # log_gdpPercapの表示範囲
'legend': dict(
x=0, y=-0.2, xanchor='left', yanchor='top', # 位置関係
orientation='h' # 凡例の表示方向
),
'height': 1000, # 高さを上げる
}
# グラフ化
years = (1952, 2007)
for year in years:
scatter_matrix(
df=single_year[year], save_name=f"gapminder_{year}", color='country',
dimensions=['continent', 'lifeExp', 'log_pop', 'log_gdpPercap', ],
color_discrete_sequence=color,
hover_data=single_year[year],
update_traces=dict(diagonal_visible=False),
title=year, # グラフタイトル
size='pop', size_max=30, # マーカーのサイズを人口で分ける
symbol='continent', # マーカーの形状を大陸で分ける
# レイアウトで表示範囲の固定
update_layout=layouts,
)
下準備
import matplotlib.cm as cm
import numpy as np
import plotly
import plotly.io as pio
import plotly.express as px
まずは下準備としてのimport
関連。メインはplotly
だけど、plotly
のカラースケールは数が少なくて使い勝手が良くないのでmatplotlib
を使用する。np
はデータのlogを計算するために使用。
あとはplotly
関連。orca
はグラフ保存用。以下の記事で解説。
-
-
【plotly&orca】plotlyで静止画保存(orca)
続きを見る
グラフを作成するための関数たち
ここでは本記事で使用する関数を定義していく。予め関数を定義しておくことで、メインの処理内容の見通しがよくなったり繰り返し同じ処理をするときの修正が楽になったりする。
今回はグラフ保存用の関数とscatter_matrix
をグラフ化するための関数を定義。
-
-
【plotly&工夫】楽にグラフを描くためのplotlyの関数化
続きを見る
グラフ保存用の関数
# グラフ保存用の関数
def save(fig, config, save_name):
pio.orca.config.executable = '/Applications/orca.app/Contents/MacOS/orca'
pio.write_html(fig, f"{save_name}.html", config=config,)
pio.write_image(fig, f"{save_name}.png")
グラフの保存用の関数を定義。保存形式はhtmlとpngにした。html保存の時にconfig
を適用することで、保存した後でもconfig
の設定を引き継げる。
config
はグラフの右上に表示されるツールバーのこと。ここの編集をすることで、余計な機能を省いたり逆に機能を追加したりできる。
scatter_matrix
プロット用の関数
# scatter_matrix用の関数
def scatter_matrix(df, save_name,
update_traces={}, update_layout={}, **kwargs):
fig = px.scatter_matrix(df, **kwargs)
fig.update_traces(**update_traces) # 追加でfigの修正・設定を行う用
fig.update_layout(**update_layout) # 追加でlayoutの修正・設定を行う用
fig.show()
prefix = 'px-scattermatrix'
save(fig=fig, config=None, save_name=f"{prefix}_{save_name}")
メインとなるscatter_matrix
プロット用の関数を定義。px
の場合はpandas
のデータフレームをベースとしてプロットする。
kwargs
は可変長キーワード引数で後から設定を追加したりする際に便利。**
は展開って意味だけど、update_layout
とupdate_traces
はただのdict
を展開しているだけだから注意。まあ設定を追加するって点では同じ意味。
**
は1つの関数に1回しか使用できないから仕方なくdict
で補っている。kwargs
もupdate_traces
もupdate_layout
も同じイメージ。
なお、わざわざ分けているのには理由がある。px.scatter_matrix
の引数にあるものはkwargs
、ないものはupdate_traces
やupdate_layout
で対応する。px
はサクッとグラフを作る事ができるが、引数が少ないのが問題点。
あとはグラフの表示と保存。必要最低限の機能はこの関数で使えるようにしているので、追加でグラフにしたい情報をkwargs
などで実現する。
簡単なデータでscatter_matrix
df = px.data.iris()
print(df)
# sepal_length sepal_width ... species species_id
# 0 5.1 3.5 ... setosa 1
# 1 4.9 3.0 ... setosa 1
# 2 4.7 3.2 ... setosa 1
# 3 4.6 3.1 ... setosa 1
# 4 5.0 3.6 ... setosa 1
# .. ... ... ... ... ...
# 145 6.7 3.0 ... virginica 3
# 146 6.3 2.5 ... virginica 3
# 147 6.5 3.0 ... virginica 3
# 148 6.2 3.4 ... virginica 3
# 149 5.9 3.0 ... virginica 3
# [150 rows x 6 columns]
print(df.columns.values)
# ['sepal_length' 'sepal_width' 'petal_length' 'petal_width' 'species'
# 'species_id']
print(df['species'].unique())
# ['setosa' 'versicolor' 'virginica']
print(df['species_id'].unique())
# [1 2 3]
まずはよく公式のグラフ例でよく出てくるiris
のデータを使用してグラフを作成してみる。データフレームの中身は上。
公式のグラフでは基本的に3種類のspecies
でデータを分けることが多い。
シンプルにグラフ化

まずはシンプルに定義したscatter_matrix
関数でプロット。横軸、縦軸ともに使用したデータフレームの全ヘッダーをマトリックス状に配置してグラフを作成している。
なので、対角線を跨いで対照的な位置にあるグラフは横軸と縦軸が入れ替わったようなグラフとなる。軸の反転。
全ヘッダでグラフを作成するので網羅性はハンパない。ただ、これだと何が何のデータか側わかりにくい。ということで、次で色分けをする。
# シンプルにグラフ化
scatter_matrix(df=df, save_name='iris')
color
で色分け

というわけで色分けをする。ここではspecies
を対象に色分けを行なってみた。こうすると各グラフでspecies
の位置関係がわかりやすい。
しかしspecies
で色分けしているけどspecies
やspecies_id
という、ここではあまり活用しないだろうヘッダもグラフ化されている。なので次は欲しいデータだけグラフ化する。
# 色分け
scatter_matrix(
df=df, save_name='iris_color',
color='species', # ヘッダ名speciesで色分け
)
dimensions
で欲しいデータだけグラフ化

引数dimensions
でヘッダ名を指定することで、指定したヘッダ名だけでグラフを作成することが可能になる。ここではwidth
とlength
のデータのみをグラフ化した。
しかし、よくよく考えると左上から右下にかけての対角線のグラフは同じデータなので必ずに直線となる。ここのグラフはいらないので次で削除する。
# グラフ化したいデータだけ使用
scatter_matrix(
df=df, save_name='iris_species_dimensions',
color='species',
# グラフ化したいヘッダ名を指定
dimensions=['sepal_width', 'sepal_length', 'petal_width', 'petal_length'],
)
fig.update_trace
で対角線を削除
残念ながらscatter_matrix
だけでは対角線のグラフを削除することはできない。しかし、作成したfig
を修正するfig.update_traces
の引数diagonal_visible
で対角線のグラフの表示を変更できるのでここをイジる。
自作のscatter_matrix
関数の引数update_traces
でdiagonal_visible=False
を指定し、対角線上のグラフを削除した。
# 対角線のグラフは同じデータなので削除
scatter_matrix(
df=df, save_name='iris_species_dimensions_diagonal_visible',
color='species',
dimensions=['sepal_width', 'sepal_length', 'petal_width', 'petal_length'],
# 対角線のデータは削除
update_traces=dict(diagonal_visible=False),
)
データが多いデータフレームでscatter_matrix
を作成
df = px.data.gapminder()
print(df)
# country continent year ... gdpPercap iso_alpha iso_num
# 0 Afghanistan Asia 1952 ... 779.445314 AFG 4
# 1 Afghanistan Asia 1957 ... 820.853030 AFG 4
# 2 Afghanistan Asia 1962 ... 853.100710 AFG 4
# 3 Afghanistan Asia 1967 ... 836.197138 AFG 4
# 4 Afghanistan Asia 1972 ... 739.981106 AFG 4
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 706.157306 ZWE 716
# 1700 Zimbabwe Africa 1992 ... 693.420786 ZWE 716
# 1701 Zimbabwe Africa 1997 ... 792.449960 ZWE 716
# 1702 Zimbabwe Africa 2002 ... 672.038623 ZWE 716
# 1703 Zimbabwe Africa 2007 ... 469.709298 ZWE 716
# [1704 rows x 8 columns]
お次もよく公式のグラフ紹介で出てくるgapminder
のデータ。これには以下のデータが入っている。
- 国名
- 大陸名
- 年
- 平均寿命
- 人口
- 1人あたりGDP
- ISOでの国名
- ISOでの国コード
+ クリックでオープン
for header in df:
print(header)
print(df[header].unique())
print() # 1行あける
# country
# ['Afghanistan' 'Albania' 'Algeria' 'Angola' 'Argentina' 'Australia'
# 'Austria' 'Bahrain' 'Bangladesh' 'Belgium' 'Benin' 'Bolivia'
# 'Bosnia and Herzegovina' 'Botswana' 'Brazil' 'Bulgaria' 'Burkina Faso'
# 'Burundi' 'Cambodia' 'Cameroon' 'Canada' 'Central African Republic'
# 'Chad' 'Chile' 'China' 'Colombia' 'Comoros' 'Congo, Dem. Rep.'
# 'Congo, Rep.' 'Costa Rica' "Cote d'Ivoire" 'Croatia' 'Cuba'
# 'Czech Republic' 'Denmark' 'Djibouti' 'Dominican Republic' 'Ecuador'
# 'Egypt' 'El Salvador' 'Equatorial Guinea' 'Eritrea' 'Ethiopia' 'Finland'
# 'France' 'Gabon' 'Gambia' 'Germany' 'Ghana' 'Greece' 'Guatemala' 'Guinea'
# 'Guinea-Bissau' 'Haiti' 'Honduras' 'Hong Kong, China' 'Hungary' 'Iceland'
# 'India' 'Indonesia' 'Iran' 'Iraq' 'Ireland' 'Israel' 'Italy' 'Jamaica'
# 'Japan' 'Jordan' 'Kenya' 'Korea, Dem. Rep.' 'Korea, Rep.' 'Kuwait'
# 'Lebanon' 'Lesotho' 'Liberia' 'Libya' 'Madagascar' 'Malawi' 'Malaysia'
# 'Mali' 'Mauritania' 'Mauritius' 'Mexico' 'Mongolia' 'Montenegro'
# 'Morocco' 'Mozambique' 'Myanmar' 'Namibia' 'Nepal' 'Netherlands'
# 'New Zealand' 'Nicaragua' 'Niger' 'Nigeria' 'Norway' 'Oman' 'Pakistan'
# 'Panama' 'Paraguay' 'Peru' 'Philippines' 'Poland' 'Portugal'
# 'Puerto Rico' 'Reunion' 'Romania' 'Rwanda' 'Sao Tome and Principe'
# 'Saudi Arabia' 'Senegal' 'Serbia' 'Sierra Leone' 'Singapore'
# 'Slovak Republic' 'Slovenia' 'Somalia' 'South Africa' 'Spain' 'Sri Lanka'
# 'Sudan' 'Swaziland' 'Sweden' 'Switzerland' 'Syria' 'Taiwan' 'Tanzania'
# 'Thailand' 'Togo' 'Trinidad and Tobago' 'Tunisia' 'Turkey' 'Uganda'
# 'United Kingdom' 'United States' 'Uruguay' 'Venezuela' 'Vietnam'
# 'West Bank and Gaza' 'Yemen, Rep.' 'Zambia' 'Zimbabwe']
# continent
# ['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']
# year
# [1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007]
# lifeExp
# [28.801 30.332 31.997 ... 46.809 39.989 43.487]
# pop
# [ 8425333 9240934 10267083 ... 11404948 11926563 12311143]
# gdpPercap
# [779.4453145 820.8530296 853.10071 ... 792.4499603 672.0386227
# 469.7092981]
# iso_alpha
# ['AFG' 'ALB' 'DZA' 'AGO' 'ARG' 'AUS' 'AUT' 'BHR' 'BGD' 'BEL' 'BEN' 'BOL'
# 'BIH' 'BWA' 'BRA' 'BGR' 'BFA' 'BDI' 'KHM' 'CMR' 'CAN' 'CAF' 'TCD' 'CHL'
# 'CHN' 'COL' 'COM' 'COD' 'COG' 'CRI' 'CIV' 'HRV' 'CUB' 'CZE' 'DNK' 'DJI'
# 'DOM' 'ECU' 'EGY' 'SLV' 'GNQ' 'ERI' 'ETH' 'FIN' 'FRA' 'GAB' 'GMB' 'DEU'
# 'GHA' 'GRC' 'GTM' 'GIN' 'GNB' 'HTI' 'HND' 'HKG' 'HUN' 'ISL' 'IND' 'IDN'
# 'IRN' 'IRQ' 'IRL' 'ISR' 'ITA' 'JAM' 'JPN' 'JOR' 'KEN' 'KOR' 'KWT' 'LBN'
# 'LSO' 'LBR' 'LBY' 'MDG' 'MWI' 'MYS' 'MLI' 'MRT' 'MUS' 'MEX' 'MNG' 'MNE'
# 'MAR' 'MOZ' 'MMR' 'NAM' 'NPL' 'NLD' 'NZL' 'NIC' 'NER' 'NGA' 'NOR' 'OMN'
# 'PAK' 'PAN' 'PRY' 'PER' 'PHL' 'POL' 'PRT' 'PRI' 'REU' 'ROU' 'RWA' 'STP'
# 'SAU' 'SEN' 'SRB' 'SLE' 'SGP' 'SVK' 'SVN' 'SOM' 'ZAF' 'ESP' 'LKA' 'SDN'
# 'SWZ' 'SWE' 'CHE' 'SYR' 'TWN' 'TZA' 'THA' 'TGO' 'TTO' 'TUN' 'TUR' 'UGA'
# 'GBR' 'USA' 'URY' 'VEN' 'VNM' 'PSE' 'YEM' 'ZMB' 'ZWE']
# iso_num
# [ 4 8 12 24 32 36 40 48 50 56 204 68 70 72 76 100 854 108
# 116 120 124 140 148 152 156 170 174 180 178 188 384 191 192 203 208 262
# 214 218 818 222 226 232 231 246 250 266 270 276 288 300 320 324 624 332
# 340 344 348 352 356 360 364 368 372 376 380 388 392 400 404 410 414 422
# 426 430 434 450 454 458 466 478 480 484 496 499 504 508 104 516 524 528
# 554 558 562 566 578 512 586 591 600 604 608 616 620 630 638 642 646 678
# 682 686 688 694 702 703 705 706 710 724 144 736 748 752 756 760 158 834
# 764 768 780 788 792 800 826 840 858 862 704 275 887 894 716]
さっきより格段にデータ量が多くなるが、作成する要領はさっきと同じ。
もちろんシンプルにグラフ化するとごちゃごちゃ

当たり前だけど、シンプルにデータフレームを入れただけだとデータはごちゃごちゃになる。全てが青色で何が何やら。
# そのままだとごちゃごちゃ
scatter_matrix(df=df, save_name='gapminder',)
大陸で色分け

続いては大陸で色分け行ってみた。こうしてみると横軸を平均寿命(lifeExp
)、縦軸を人口(pop
)にした時ではアジアの人口が多そうに見える。
ただし、ホバーで表示される情報は大陸名と平均寿命と人口なので何年のどの国のデータかがわからない。あと、iso_num
もここでは使わないので省略したい。ということで次で色々とカスタムする。
# 大陸ごとに色分け
scatter_matrix(
df=df, save_name='gapminder_color', color='continent',
update_traces=dict(diagonal_visible=False),
)
国ごとに色分け+その他の設定

ということでガッツリと修正してみた。具体的には以下。
- 色分けを国ごとに変更
- 大陸、年、平均寿命、人口、1人あたりGDPでグラフを作成
- 色分けを
matplotlib
のカラースケールのJet
で設定
- ホバー時の表示データをデータフレームの全ヘッダ情報に
色分けを国ごとに変更したので凡例がハンパなく多くなる。ということでカラースケールの連続色を使用するわけだが、plotly
のカラースケールだと色が6種類くらいしかないので足りない。
なので、ここではmatplotlib.cm
で色を計算で算出しつつ、plotly
用に改良した。色については以下。matplotlib
では色は0-1の範囲で作成するが、plotly
では0-255なので調節が必要。統一してほしかった。
-
-
【plotly&色の自動調節】入力したRGBをrgba(R, G, B, a)に自動変換する
続きを見る
# 国ごとに色分けするために国の数だけカラースケールを作成
countries = df['country'].unique()
color = []
for num, _ in enumerate(countries):
cmap = cm.jet(num / len(countries))
rgb = f"rgb{plotly.colors.convert_to_RGB_255(cmap)}"
color.append(rgb)
print(color)
# ['rgb(0, 0, 128)', 'rgb(0, 0, 132)', 'rgb(0, 0, 141)', 'rgb(0, 0, 150)', 'rgb(0, 0, 159)', 'rgb(0, 0, 168)', 'rgb(0, 0, 173)', 'rgb(0, 0, 182)', 'rgb(0, 0, 191)', 'rgb(0, 0, 200)', 'rgb(0, 0, 209)', 'rgb(0, 0, 214)', 'rgb(0, 0, 223)', 'rgb(0, 0, 232)', 'rgb(0, 0, 241)', 'rgb(0, 0, 250)', 'rgb(0, 0, 255)', 'rgb(0, 0, 255)', 'rgb(0, 0, 255)', 'rgb(0, 8, 255)', 'rgb(0, 16, 255)', 'rgb(0, 20, 255)', 'rgb(0, 28, 255)', 'rgb(0, 36, 255)', 'rgb(0, 44, 255)', 'rgb(0, 52, 255)', 'rgb(0, 56, 255)', 'rgb(0, 64, 255)', 'rgb(0, 72, 255)', 'rgb(0, 80, 255)', 'rgb(0, 88, 255)', 'rgb(0, 92, 255)', 'rgb(0, 100, 255)', 'rgb(0, 108, 255)', 'rgb(0, 116, 255)', 'rgb(0, 124, 255)', 'rgb(0, 128, 255)', 'rgb(0, 136, 255)', 'rgb(0, 144, 255)', 'rgb(0, 152, 255)', 'rgb(0, 160, 255)', 'rgb(0, 164, 255)', 'rgb(0, 172, 255)', 'rgb(0, 180, 255)', 'rgb(0, 188, 255)', 'rgb(0, 196, 255)', 'rgb(0, 200, 255)', 'rgb(0, 208, 255)', 'rgb(0, 216, 255)', 'rgb(0, 224, 251)', 'rgb(2, 232, 244)', 'rgb(6, 236, 241)', 'rgb(12, 244, 235)', 'rgb(19, 252, 228)', 'rgb(25, 255, 222)', 'rgb(31, 255, 215)', 'rgb(35, 255, 212)', 'rgb(41, 255, 206)', 'rgb(48, 255, 199)', 'rgb(54, 255, 193)', 'rgb(60, 255, 186)', 'rgb(64, 255, 183)', 'rgb(70, 255, 177)', 'rgb(77, 255, 170)', 'rgb(83, 255, 164)', 'rgb(90, 255, 157)', 'rgb(93, 255, 154)', 'rgb(99, 255, 148)', 'rgb(106, 255, 141)', 'rgb(112, 255, 135)', 'rgb(119, 255, 128)', 'rgb(125, 255, 122)', 'rgb(128, 255, 119)', 'rgb(135, 255, 112)', 'rgb(141, 255, 106)', 'rgb(148, 255, 99)', 'rgb(154, 255, 93)', 'rgb(157, 255, 90)', 'rgb(164, 255, 83)', 'rgb(170, 255, 77)', 'rgb(177, 255, 70)', 'rgb(183, 255, 64)', 'rgb(186, 255, 60)', 'rgb(193, 255, 54)', 'rgb(199, 255, 48)', 'rgb(206, 255, 41)', 'rgb(212, 255, 35)', 'rgb(215, 255, 31)', 'rgb(222, 255, 25)', 'rgb(228, 255, 19)', 'rgb(235, 255, 12)', 'rgb(241, 252, 6)', 'rgb(244, 248, 2)', 'rgb(251, 241, 0)', 'rgb(255, 234, 0)', 'rgb(255, 226, 0)', 'rgb(255, 219, 0)', 'rgb(255, 215, 0)', 'rgb(255, 208, 0)', 'rgb(255, 200, 0)', 'rgb(255, 193, 0)', 'rgb(255, 185, 0)', 'rgb(255, 182, 0)', 'rgb(255, 174, 0)', 'rgb(255, 167, 0)', 'rgb(255, 159, 0)', 'rgb(255, 152, 0)', 'rgb(255, 148, 0)', 'rgb(255, 141, 0)', 'rgb(255, 134, 0)', 'rgb(255, 126, 0)', 'rgb(255, 119, 0)', 'rgb(255, 115, 0)', 'rgb(255, 108, 0)', 'rgb(255, 100, 0)', 'rgb(255, 93, 0)', 'rgb(255, 85, 0)', 'rgb(255, 82, 0)', 'rgb(255, 74, 0)', 'rgb(255, 67, 0)', 'rgb(255, 59, 0)', 'rgb(255, 52, 0)', 'rgb(255, 48, 0)', 'rgb(255, 41, 0)', 'rgb(255, 34, 0)', 'rgb(255, 26, 0)', 'rgb(255, 19, 0)', 'rgb(250, 15, 0)', 'rgb(241, 8, 0)', 'rgb(232, 0, 0)', 'rgb(223, 0, 0)', 'rgb(214, 0, 0)', 'rgb(209, 0, 0)', 'rgb(200, 0, 0)', 'rgb(191, 0, 0)', 'rgb(182, 0, 0)', 'rgb(173, 0, 0)', 'rgb(168, 0, 0)', 'rgb(159, 0, 0)', 'rgb(150, 0, 0)', 'rgb(141, 0, 0)', 'rgb(132, 0, 0)']
あとはscatter_matrix
に必要な引数を設定すればいい。色はcolor_discrete_sequence
で、ホバーはhover_data
で指定する。
ある程度見やすくはなってきたが、人口やGDPの部分で極端に高い値を拾っているのでその他がのっぺりして判別がつきにくい。ということで、ここのデータのlogをとって値の差を小さくする。
scatter_matrix(
df=df, save_name='gapminder_color_discrete_sequence', color='country',
dimensions=['continent', 'year', 'lifeExp', 'pop', 'gdpPercap'],
color_discrete_sequence=color, # 色分けの指定
hover_data=df, # ホバーで表示する内容はdfの内容全部とする
update_traces=dict(diagonal_visible=False),
)
人口とGDPの値のlogをとって見やすく
ということで、人口とGDPのデータのlogをとるわけだが、とりあえず現状のデータフレームは以下。
df = px.data.gapminder()
print(df)
# country continent year ... gdpPercap iso_alpha iso_num
# 0 Afghanistan Asia 1952 ... 779.445314 AFG 4
# 1 Afghanistan Asia 1957 ... 820.853030 AFG 4
# 2 Afghanistan Asia 1962 ... 853.100710 AFG 4
# 3 Afghanistan Asia 1967 ... 836.197138 AFG 4
# 4 Afghanistan Asia 1972 ... 739.981106 AFG 4
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 706.157306 ZWE 716
# 1700 Zimbabwe Africa 1992 ... 693.420786 ZWE 716
# 1701 Zimbabwe Africa 1997 ... 792.449960 ZWE 716
# 1702 Zimbabwe Africa 2002 ... 672.038623 ZWE 716
# 1703 Zimbabwe Africa 2007 ... 469.709298 ZWE 716
# [1704 rows x 8 columns]
logをとるのは簡単で、numpy
のnp.log10
で底10のlogをとることができる。大体のデータは底10を使用するのが一般的、な気がする。
あとはこのlogを撮ったデータをデータフレームに追加したらいい。追加はシンプルにヘッダ名とそのデータを使えばいい。logを使ったデータ2列分を追加したので合計10列となった。
# 人口と1人あたりGDPの値にlogでとる
df['log_pop'] = np.log10(df['pop'])
df['log_gdpPercap'] = np.log10(df['gdpPercap'])
print(df)
# country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [1704 rows x 10 columns]
最後にpop
の代わりにlog_pop
を、gdpPercap
の代わりにlog_gdpPercap
をdimensions
で指定してグラフを作成したらおしまい。
データ同士が広がって違いがわかりやすいようになった。これだと極端な値を持つデータに引っ張られることなくデータを見ることが出来る。
んだけど、このグラフでは全ての年のデータが1つにまとめられている。なのである年のデータだけ抽出したいんだけど、その有効的な手段であるアニメーションにscatter_matrix
は非対応。
なので、最後に単年のデータフレームを抽出して、その年でグラフを作成する方向にシフトする。
scatter_matrix(
df=df, save_name='gapminder_logscale', color='country',
dimensions=['continent', 'year', 'lifeExp', 'log_pop', 'log_gdpPercap', ],
color_discrete_sequence=color,
hover_data=df,
update_traces=dict(
diagonal_visible=False,
# マーカーのサイズを小さめに
marker_size=3,
),
)
gapminder
の単年のデータをscatter_matrix
print(df)
# country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# ... ... ... ... ... ... ... ...
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [1704 rows x 10 columns]
# 元のデータフレームにある年でフィルターをかける
single_year = {}
for year in df['year'].unique():
# queryを使用時に変数を使用したいなら@をつける
single_year[year] = df.query('year == @year')
ということで、単年でグラフを作成する。本当はアニメーションを使って1つのグラフで作成できれば一番いいんだけどできないから仕方ない。
一応、go
を使ってアニメーションをくっつけるってこともできないこともないだろうけど、go
のアニメーションは面倒なのでスルーする。
単年のデータは年ごとにdict
に格納した。中身は以下。
+ クリックでオープン
print(single_year)
# {1952: country continent year ... iso_num log_pop log_gdpPercap
# 0 Afghanistan Asia 1952 ... 4 6.925587 2.891786
# 12 Albania Europe 1952 ... 8 6.108124 3.204407
# 24 Algeria Africa 1952 ... 12 6.967526 3.388990
# 36 Angola Africa 1952 ... 24 6.626555 3.546618
# 48 Argentina Americas 1952 ... 32 7.252294 3.771684
# ... ... ... ... ... ... ... ...
# 1644 Vietnam Asia 1952 ... 704 7.419077 2.781803
# 1656 West Bank and Gaza Asia 1952 ... 275 6.013084 3.180582
# 1668 Yemen, Rep. Asia 1952 ... 887 6.695817 2.893050
# 1680 Zambia Africa 1952 ... 894 6.426836 3.059711
# 1692 Zimbabwe Africa 1952 ... 716 6.488679 2.609471
# [142 rows x 10 columns], 1957: country continent year ... iso_num log_pop log_gdpPercap
# 1 Afghanistan Asia 1957 ... 4 6.965716 2.914265
# 13 Albania Europe 1957 ... 8 6.169235 3.288313
# 25 Algeria Africa 1957 ... 12 7.011607 3.479140
# 37 Angola Africa 1957 ... 24 6.659094 3.582965
# 49 Argentina Americas 1957 ... 32 7.292490 3.836125
# ... ... ... ... ... ... ... ...
# 1645 Vietnam Asia 1957 ... 704 7.462376 2.830130
# 1657 West Bank and Gaza Asia 1957 ... 275 6.029562 3.261755
# 1669 Yemen, Rep. Asia 1957 ... 887 6.740212 2.905704
# 1681 Zambia Africa 1957 ... 894 6.479431 3.117920
# 1693 Zimbabwe Africa 1957 ... 716 6.561857 2.714970
# [142 rows x 10 columns], 1962: country continent year ... iso_num log_pop log_gdpPercap
# 2 Afghanistan Asia 1962 ... 4 7.011447 2.931000
# 14 Albania Europe 1962 ... 8 6.237578 3.364155
# 26 Algeria Africa 1962 ... 12 7.041430 3.406679
# 38 Angola Africa 1962 ... 24 6.683589 3.630354
# 50 Argentina Americas 1962 ... 32 7.328049 3.853282
# ... ... ... ... ... ... ... ...
# 1646 Vietnam Asia 1962 ... 704 7.528867 2.887645
# 1658 West Bank and Gaza Asia 1962 ... 275 6.054281 3.342217
# 1670 Yemen, Rep. Asia 1962 ... 887 6.786757 2.916782
# 1682 Zambia Africa 1962 ... 894 6.534153 3.162184
# 1694 Zimbabwe Africa 1962 ... 716 6.631214 2.722035
# [142 rows x 10 columns], 1967: country continent year ... iso_num log_pop log_gdpPercap
# 3 Afghanistan Asia 1967 ... 4 7.062129 2.922309
# 15 Albania Europe 1967 ... 8 6.297555 3.440940
# 27 Algeria Africa 1967 ... 12 7.105868 3.511481
# 39 Angola Africa 1967 ... 24 6.719950 3.742157
# 51 Argentina Americas 1967 ... 32 7.360484 3.905955
# ... ... ... ... ... ... ... ...
# 1647 Vietnam Asia 1967 ... 704 7.596200 2.804223
# 1659 West Bank and Gaza Asia 1967 ... 275 6.057908 3.423199
# 1671 Yemen, Rep. Asia 1967 ... 887 6.828710 2.935730
# 1683 Zambia Africa 1967 ... 894 6.591065 3.249706
# 1695 Zimbabwe Africa 1967 ... 716 6.698573 2.755719
# [142 rows x 10 columns], 1972: country continent year ... iso_num log_pop log_gdpPercap
# 4 Afghanistan Asia 1972 ... 4 7.116590 2.869221
# 16 Albania Europe 1972 ... 8 6.354791 3.520277
# 28 Algeria Africa 1972 ... 12 7.169110 3.621453
# 40 Angola Africa 1972 ... 24 6.770473 3.738248
# 52 Argentina Americas 1972 ... 32 7.394098 3.975112
# ... ... ... ... ... ... ... ...
# 1648 Vietnam Asia 1972 ... 704 7.649870 2.844789
# 1660 West Bank and Gaza Asia 1972 ... 275 6.037256 3.496017
# 1672 Yemen, Rep. Asia 1972 ... 887 6.869647 3.102107
# 1684 Zambia Africa 1972 ... 894 6.653839 3.248831
# 1696 Zimbabwe Africa 1972 ... 716 6.767982 2.902744
# [142 rows x 10 columns], 1977: country continent year ... iso_num log_pop log_gdpPercap
# 5 Afghanistan Asia 1977 ... 4 7.172614 2.895485
# 17 Albania Europe 1977 ... 8 6.399509 3.548144
# 29 Algeria Africa 1977 ... 12 7.234335 3.691118
# 41 Angola Africa 1977 ... 24 6.789769 3.478371
# 53 Argentina Americas 1977 ... 32 7.431104 4.003419
# ... ... ... ... ... ... ... ...
# 1649 Vietnam Asia 1977 ... 704 7.703579 2.853417
# 1661 West Bank and Gaza Asia 1977 ... 275 6.100746 3.566182
# 1673 Yemen, Rep. Asia 1977 ... 887 6.924486 3.262395
# 1685 Zambia Africa 1977 ... 894 6.717383 3.201039
# 1697 Zimbabwe Africa 1977 ... 716 6.822306 2.836063
# [142 rows x 10 columns], 1982: country continent year ... iso_num log_pop log_gdpPercap
# 6 Afghanistan Asia 1982 ... 4 7.109977 2.990344
# 18 Albania Europe 1982 ... 8 6.444060 3.560012
# 30 Algeria Africa 1982 ... 12 7.301762 3.759302
# 42 Angola Africa 1982 ... 24 6.846113 3.440429
# 54 Argentina Americas 1982 ... 32 7.467480 3.954141
# ... ... ... ... ... ... ... ...
# 1650 Vietnam Asia 1982 ... 704 7.749289 2.849564
# 1662 West Bank and Gaza Asia 1982 ... 275 6.154082 3.637092
# 1674 Yemen, Rep. Asia 1982 ... 887 6.984870 3.296129
# 1686 Zambia Africa 1982 ... 894 6.785359 3.148812
# 1698 Zimbabwe Africa 1982 ... 716 6.882896 2.896997
# [142 rows x 10 columns], 1987: country continent year ... iso_num log_pop log_gdpPercap
# 7 Afghanistan Asia 1987 ... 4 7.142012 2.930641
# 19 Albania Europe 1987 ... 8 6.487890 3.572748
# 31 Algeria Africa 1987 ... 12 7.366516 3.754452
# 43 Angola Africa 1987 ... 24 6.896208 3.385644
# 55 Argentina Americas 1987 ... 32 7.499974 3.960931
# ... ... ... ... ... ... ... ...
# 1651 Vietnam Asia 1987 ... 704 7.798143 2.914237
# 1663 West Bank and Gaza Asia 1987 ... 275 6.228198 3.708183
# 1675 Yemen, Rep. Asia 1987 ... 887 7.049967 3.294850
# 1687 Zambia Africa 1987 ... 894 6.861678 3.083974
# 1699 Zimbabwe Africa 1987 ... 716 6.964562 2.848901
# [142 rows x 10 columns], 1992: country continent year ... iso_num log_pop log_gdpPercap
# 8 Afghanistan Asia 1992 ... 4 7.212665 2.812473
# 20 Albania Europe 1992 ... 8 6.521987 3.397495
# 32 Algeria Africa 1992 ... 12 7.419929 3.700982
# 44 Angola Africa 1992 ... 24 6.941312 3.419600
# 56 Argentina Americas 1992 ... 32 7.530954 3.968876
# ... ... ... ... ... ... ... ...
# 1652 Vietnam Asia 1992 ... 704 7.844730 2.995206
# 1664 West Bank and Gaza Asia 1992 ... 275 6.323207 3.779427
# 1676 Yemen, Rep. Asia 1992 ... 887 7.126066 3.274042
# 1688 Zambia Africa 1992 ... 894 6.923304 3.083103
# 1700 Zimbabwe Africa 1992 ... 716 7.029560 2.840997
# [142 rows x 10 columns], 1997: country continent year ... iso_num log_pop log_gdpPercap
# 9 Afghanistan Asia 1997 ... 4 7.346889 2.803007
# 21 Albania Europe 1997 ... 8 6.535046 3.504206
# 33 Algeria Africa 1997 ... 12 7.463475 3.680996
# 45 Angola Africa 1997 ... 24 6.994538 3.357390
# 57 Argentina Americas 1997 ... 32 7.558750 4.040099
# ... ... ... ... ... ... ... ...
# 1653 Vietnam Asia 1997 ... 704 7.881093 3.141731
# 1665 West Bank and Gaza Asia 1997 ... 275 6.451179 3.851910
# 1677 Yemen, Rep. Asia 1997 ... 887 7.199385 3.325820
# 1689 Zambia Africa 1997 ... 894 6.973949 3.029933
# 1701 Zimbabwe Africa 1997 ... 716 7.057093 2.898972
# [142 rows x 10 columns], 2002: country continent year ... iso_num log_pop log_gdpPercap
# 10 Afghanistan Asia 2002 ... 4 7.402578 2.861376
# 22 Albania Europe 2002 ... 8 6.545123 3.663155
# 34 Algeria Africa 2002 ... 12 7.495366 3.723295
# 46 Angola Africa 2002 ... 24 7.036074 3.442995
# 58 Argentina Americas 2002 ... 32 7.583552 3.944366
# ... ... ... ... ... ... ... ...
# 1654 Vietnam Asia 2002 ... 704 7.907992 3.246611
# 1666 West Bank and Gaza Asia 2002 ... 275 6.530146 3.654705
# 1678 Yemen, Rep. Asia 2002 ... 887 7.271871 3.349243
# 1690 Zambia Africa 2002 ... 894 7.025134 3.030038
# 1702 Zimbabwe Africa 2002 ... 716 7.076515 2.827394
# [142 rows x 10 columns], 2007: country continent year ... iso_num log_pop log_gdpPercap
# 11 Afghanistan Asia 2007 ... 4 7.503653 2.988818
# 23 Albania Europe 2007 ... 8 6.556366 3.773569
# 35 Algeria Africa 2007 ... 12 7.522877 3.794025
# 47 Angola Africa 2007 ... 24 7.094138 3.680991
# 59 Argentina Americas 2007 ... 32 7.605326 4.106510
# ... ... ... ... ... ... ... ...
# 1655 Vietnam Asia 2007 ... 704 7.930757 3.387670
# 1667 West Bank and Gaza Asia 2007 ... 275 6.604046 3.480776
# 1679 Yemen, Rep. Asia 2007 ... 887 7.346583 3.358081
# 1691 Zambia Africa 2007 ... 894 7.069891 3.104218
# 1703 Zimbabwe Africa 2007 ... 716 7.090298 2.671829
# [142 rows x 10 columns]}
あとはこのdict
から好きな年のデータを引っ張ってきてグラフを作成するだけ。ただし、以下に示すように色々とカスタムしておいた。
軸の表示範囲、凡例、高さは自作のscatter_matrix
関数の引数layouts
でdict
を指定することで関数内で展開してくれる。
- グラフタイトルを年に
- マーカーのサイズは人口に
- マーカーの最大サイズは30に
- マーカーの形状は大陸ごとに
- それぞれの軸の表示範囲を設定
- 凡例はグラフの下に横続きで作成
- グラフの高さを指定
ただし、軸の表示範囲についてはaxis
の番号がdimensions
の順番になっているんだけど、size
をlog_pop
に変更するとaxis1
がlog_pop
になってaxis2
以降がdimensions
の順番に変わるので注意。
axis
の値が軸の値に合っていないと初期状態でちゃんと表示されなくなる。ややこしいけど、自分で色々操作してみると実感が湧く。
今回は一番古い1952年と一番新しい2007年のデータでグラフを作成した。プロット点が小さすぎて数が少なく感じるかもしれないけどホバーしたらちゃんとプロットされていることが確認できる。
プロット点についてはマーカーサイズ専用でデータフレームに列を追加してもいいかもしれない。
# レイアウト設定
layouts = {
'xaxis1': dict(range=(-0.5, 4.5)), # continentの表示範囲(index扱い)
'xaxis2': dict(range=(20, 85)), # lifeExpの表示範囲
'xaxis3': dict(range=(4, 10)), # log_popの表示範囲
'xaxis4': dict(range=(2, 5)), # log_gdpPercapの表示範囲
'legend': dict(
x=0, y=-0.2, xanchor='left', yanchor='top', # 位置関係
orientation='h' # 凡例の表示方向
),
'height': 1000, # 高さを上げる
}
# グラフ化
years = (1952, 2007)
for year in years:
scatter_matrix(
df=single_year[year], save_name=f"gapminder_{year}", color='country',
dimensions=['continent', 'lifeExp', 'log_pop', 'log_gdpPercap', ],
color_discrete_sequence=color,
hover_data=single_year[year],
update_traces=dict(diagonal_visible=False),
title=year, # グラフタイトル
size='pop', size_max=30, # マーカーのサイズを人口で分ける
symbol='continent', # マーカーの形状を大陸で分ける
# レイアウトで表示範囲の固定
update_layout=layouts,
)
パッと見で欲しいデータが見つかる
今回はpx
のscatter_matrix
を使って複数種の軸を持つsubplot
グラフを作成した。シンプルなsubplot
では単一な軸でしか作成できなかったが、scatter_matrix
だと複数種類が簡単にできる。
scatter_matrix
の大きな特徴は複数種の軸だが、これのおかげでパッと見でどういう傾向があるかを知ることができる。
あとはアニメーションがついてくれたらもっと利便性が上がると思う。また、go
では上半分を非表示にするという引数もあるので、px
でも実装してほしい。
関連記事
-
-
【plotly&subplot】goでアニメーションのサブプロット
続きを見る
-
-
【px&facet】plotly.expressでsubplotsを描く
続きを見る
-
-
【plotly&円グラフ】subplotsやボタンの適用
続きを見る
-
-
【plotly&table+scatter】subplotsで表と散布図を同時にグラフ化
続きを見る
-
-
【plotly&make_subplots】pythonのplotlyで複数グラフを1つの画像に描く
続きを見る
-
-
【plt&subplot】pythonのpltで複数グラフを1つの画像に描く
続きを見る
-
-
【plotly&pandas】df.plot()でPlotlyのグラフを作成
続きを見る