### DataVL2.txt Ver.2024.01.10 ### 作成者:Masaya Matsuura, Ehime University ### Pythonのスクリプト ### 同時配布のPDFファイル(DataVL.pdf)p7~p39で紹介しているグラフ・チャート作成用 ###Google Colaboratory上で動作確認 ###スクリプトを実行するためには、同時配布のExcelファイル(DataVL.xlsx)が必要 ###(DataVL.PDFのp114~p115参照) #################スライドP7(1. 折れ線グラフ)################################## import plotly.graph_objects as go fig=go.Figure(go.Scatter(x=[2010,2012,2014,2016,2018,2020,2022], y=[39,33,23,15,13,9,8],marker=dict(size=12))) fig.update_layout(width=650,height=450,margin=dict(pad=10), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='実店舗での直近の買い物において、
' '現金で支払った人の割合(スウェーデン)', xanchor='left',font_size=30,x=0.12,y=0.92), xaxis=dict(title='年(隔年)',tickangle=30), yaxis=dict(title='割合(%)',range=(0,40))) fig.show() #################スライドP8(2. 面グラフ)###################################### import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='TFRate') fig=go.Figure(go.Scatter(x=df['Year'],y=df['T'], name='area1',fill='tozeroy',mode='none')) fig.update_layout(width=780,height=680,margin=dict(pad=15), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの出生率(1970~2022)', font_size=40,x=0.54,y=0.95), xaxis=dict(title='年'), yaxis=dict(title='合計特殊出生率',range=(0,2.5))) fig.show() #################スライドP9(3. 積み上げ面グラフ)############################## import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='UTbgTS') c=['#636EFA','#EF553B','#00CC96','#AB63FA','#FFA15A', '#19D3F3','#FF6692','#B6E880','#FF97FF','#FECB52'] bg=['DB2DB','DB1FB1DB','DB2FB','FB'] n=['両親、本人とも国内生まれ','片親と本人が国内生まれ', '本人のみ国内生まれ','本人が外国生まれ'] sl=[True,False] gn=[None,'percent'] fig=make_subplots(rows=1,cols=2,subplot_titles=('人数(単位:100万人)','割合(%)')) fig.update_annotations(font_size=35) for i in range(2): for j in range(4): fig.add_trace(go.Scatter(x=df['Year'],y=df[bg[j]],name=n[j],line_color=c[j], showlegend=sl[i],groupnorm=gn[i],stackgroup='one'), 1,i+1) fig.update_layout(width=1400,height=600,margin=dict(pad=5), font_size=30,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン国民の出自別の人数と割合の推移(2002~2021)', font_size=40,y=0.98), xaxis1_title='年',xaxis2_title='年', yaxis1=dict(tickvals=[0,2500000,5000000,7500000,10000000], ticktext=['0','2.5','5.0','7.5','10.0'])) fig.show() #################スライドP10(4. 棒グラフ)##################################### import plotly.graph_objects as go x=['水力','原子力', '風力','火力','太陽光'] y=[73.425,50.992,27.108,15.573,1.507] fig = go.Figure(go.Bar(x=x,y=y,width=0.8)) fig.update_layout(width=980,height=580,margin=dict(pad=10), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの電源別の発電量(2021年)', font_size=40,x=0.50,y=0.95), yaxis=dict(title='発電量(TWh)',range=(0, 80),dtick=20)) fig.show() #################スライドP11(5. 集合棒グラフ)################################# import plotly.graph_objects as go x=['再エネ','原子力','火力'] y1=[73.425+27.108+1.507,50.992,15.573] y2=[85.81700866046+7.447923243+19.041908871613,67.76697598,681.509866144] data1=go.Bar(x=x,y=y1,name='スウェーデン') data2=go.Bar(x=x,y=y2,name='日本') fig=go.Figure([data1, data2]) fig.update_layout(width=950,height=650,margin=dict(pad=10), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンと日本の電源別の発電量(2021年)', font_size=40,x=0.50,y=0.95), yaxis=dict(title='発電量(TWh)',range=(0,800),dtick=200), legend=dict(font_size=35,bgcolor='rgb(229,236,246)',x=0.01, y=0.99,xanchor='left',yanchor='top',orientation='h')) fig.show() #################スライドP12(6. 積み上げ棒グラフ)############################# import plotly.graph_objects as go cn=['日本','スウェーデン'] m=['水力','風力','太陽光','原子力','火力','その他'] a1=[85.81700866046,7.447923243,19.041908871613,67.76697598,681.509866144,2.178787] a2=[73.425,27.108,1.507,50.992,15.573,0] bar=[] for i in range(6): bar.append(go.Bar(x=(a1[i],a2[i]),y=cn,name=m[i],orientation='h',width=0.8)) fig=go.Figure(bar) fig.update_layout(width=1180,height=430,margin=dict(pad=10,t=130),barmode='stack', font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンと日本の電源別の発電量(2021年)', font_size=40,x=0.55,y=0.95), xaxis=dict(title='発電量(TWh)'), legend=dict(y=1.35,xanchor='left',yanchor='top', orientation='h',traceorder=('normal'))) fig.show() #################スライドP13左(7. 滝グラフ)################################### import plotly.graph_objects as go x=['出生', '死亡', '入国', '出国', '調整', '増減計'] y=[114.263,-91.958,90.631,-48.284,8.379,73.031] measure=['absolute','relative','relative','relative','relative','total'] c=['#636EFA','#92C4FC','#FFC5CF'] fig = go.Figure(go.Waterfall(x=x,measure=measure,y=y, totals=dict(marker_color=c[0]), increasing=dict(marker_color=c[1]), decreasing=dict(marker_color=c[2]), connector=dict(line_width=3,line_color=c[0]))) fig.update_layout(width=800,height=600,margin=dict(pad=10), font_size=35,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの人口の増減(2021年)', font_size=45,x=0.50,y=0.95), yaxis=dict(title='人数(千人)')) fig.show() #################スライドP13右(7. 滝グラフ)################################### import plotly.graph_objects as go x=['出生', '死亡', '入国', '出国', '調整', '増減計'] y=[114.263,-91.958,90.631,-48.284,8.379,73.031] measure=['a','r','r','r','r','t'] c=['#636EFA','#92C4FC','#FFC5CF'] fig=go.Figure(go.Waterfall(x=x,measure=measure,y=y, totals=dict(marker_color=c[0]), increasing=dict(marker_color=c[1]), decreasing=dict(marker_color=c[2]), connector=dict(line_width=3,line_color=c[0]))) fig.update_layout(width=800,height=600,margin=dict(pad=10), font_size=35,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの人口の増減(2021年)', font_size=45,x=0.50,y=0.95), yaxis=dict(title='人数(千人)')) fig.show() #################スライドP14(8. ファンネルチャート)######################### from plotly import graph_objects as go x=[9177,6359,6140,4070] y=['無作為抽出された人','連絡が取れた人','回答可能だった人','回答した人'] fig=go.Figure(go.Funnel(x=x,y=y,textposition='inside', texttemplate='%{label}
%{value:,.0f}人(%{percentInitial:,.3p})')) fig.update_layout(width=800,height=700, font_size=30,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン統計庁の世論調査の回答者数', font_size=38,x=0.5,y=0.92), yaxis=dict(showticklabels=False), paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)') fig.show() #################スライドP15(9. 円グラフ)################################### import plotly.graph_objects as go from plotly.subplots import make_subplots cn=['スウェーデン','日本'] m=['水力','原子力','風力','火力','太陽光','その他'] a=[[73.425,50.992,27.108,15.573,1.507],[85.81700866046,67.76697598, 7.447923243,681.509866144,19.041908871613,1.967207+0.2115824]] c=['#636EFA','#FBB8FE','#B7FFB7','#F6998A','#FEEF26','#68E6FA'] fig=make_subplots(rows=1,cols=2,specs=[[{'type':'domain'}, {'type':'domain'}]], subplot_titles=(cn[0],cn[1]),horizontal_spacing=0.05) fig.update_annotations(font_size=35,y=-0.02) for i in range(2): fig.add_trace(go.Pie(labels=m,values=a[i],direction='clockwise',sort=False,name=cn[i], texttemplate = '%{percent:.0%}',textinfo='percent', marker=dict(colors=c,line_color='white',line_width=1), hovertemplate='%{label}
%{value:,.0f}TWh
%{percent:,.5p}'), 1,i+1) fig.update_layout(width=1100,height=660,margin=dict(l=5,b=20), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンと日本の電源別の発電量(2021年)', font_size=43,x=0.5,y=0.95), legend=dict(y=1,x=1.05,yanchor='top',traceorder=('normal'))) fig.show() #################スライドP16(10. ドーナツグラフ)############################ iimport plotly.graph_objects as go from plotly.subplots import make_subplots cn=['スウェーデン','日本'] m=['水力','原子力','風力','火力','太陽光','その他'] a=[[73.425,50.992,27.108,15.573,1.507],[85.81700866046,67.76697598, 7.447923243,681.509866144,19.041908871613,1.967207+0.2115824]] c=['#636EFA','#FBB8FE','#B7FFB7','#F6998A','#FEEF26','#68E6FA'] fig=make_subplots(rows=1,cols=2,specs=[[{'type':'domain'}, {'type':'domain'}]], subplot_titles=(cn[0],cn[1]),horizontal_spacing=0.05) fig.update_annotations(font_size=33,y=0.45) for i in range(2): fig.add_trace(go.Pie(labels=m,values=a[i],direction='clockwise',sort=False, name=cn[i],hole=0.48, texttemplate = '%{percent:.0%}',textinfo='percent', marker=dict(colors=c,line_color='white',line_width=1), hovertemplate='%{label}
%{value:,.0f}TWh
%{percent:,.5p}'), 1,i+1) fig.update_layout(width=1100,height=660,margin=dict(l=5,b=20), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンと日本の電源別の発電量(2021年)', font_size=43,x=0.5,y=0.95), legend=dict(y=1,x=1.05,yanchor='top',traceorder=('normal'))) fig.show() #################スライドP17(11. サンバーストグラフ)######################## import plotly.graph_objects as go l=['欧州','北欧','スウェーデン','デンマーク','フィンランド','ノルウェー', 'EU
(北欧以外)','ポーランド','ルーマニア','ドイツ','フランス', '欧州
(その他)','トルコ','英国', '欧州以外','アジア','アフガニスタン','インド','イラク','イラン', '中国','パキスタン','シリア', 'アフリカ','エリトリア','北米','米国','南米','ブラジル'] p=['',l[0],l[1],l[1],l[1],l[1],l[0],l[6],l[6],l[6],l[6],l[0],l[11],l[11], '',l[14],l[15],l[15],l[15],l[15],l[15],l[15],l[15], l[14],l[23],l[14],l[25],l[14],l[27]] v=[44055,15441,10480,1563,1635,1376,18332,3181,1562,3501,1281, 10282,2077,1409,46576,30213,2022,6017,2122,2443, 2025,3240,3538,9766,1165,3031,1898,2960,1235] fig =go.Figure(go.Sunburst(labels=l,parents=p,values=v,branchvalues='total', marker_colors=['#fc8d62'],insidetextfont_size=25,name='2021年', hovertemplate='%{label}
%{value:,.0f}人
%{percentRoot:.1%}')) fig.update_layout(width=600,height=700,margin=dict(pad=10,t=100,l=0,r=0,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンへの入国移民の
出生国の割合(2021年)', font_size=40,x=0.5,y=0.95)) fig.show() #################スライドP18(12. ツリーマップ)############################## import plotly.graph_objects as go l=['欧州','北欧','スウェーデン','デン
マーク','フィン
ランド','ノル
ウェー', 'EU(北欧以外)','ポーランド','ルー
マニア','ドイツ','フラ
ンス', '欧州(その他)','トルコ','英国', '欧州以外','アジア','アフガニ
スタン','インド','イラク','イラン', '中国','パキスタン','シリア', 'アフリカ','エリトリア','北米','米国','南米','ブラ
ジル'] p=['全体',l[0],l[1],l[1],l[1],l[1],l[0],l[6],l[6],l[6],l[6],l[0],l[11],l[11], '全体',l[14],l[15],l[15],l[15],l[15],l[15],l[15],l[15], l[14],l[23],l[14],l[25],l[14],l[27]] v=[44055,15441,10480,1563,1635,1376,18332,3181,1562,3501,1281, 10282,2077,1409,46576,30213,2022,6017,2122,2443, 2025,3240,3538,9766,1165,3031,1898,2960,1235] c=['#F7A699','#F69F90','#F48370','#979EFC','#8089FB','#636EFA'] mc=[c[0],c[1],c[2],c[2],c[2],c[2],c[1],c[2],c[2],c[2],c[2],c[1],c[2],c[2], c[3],c[4],c[5],c[5],c[5],c[5],c[5],c[5],c[5],c[4],c[5],c[4],c[5],c[4],c[5]] fig =go.Figure(go.Treemap(labels=l,parents=p,values=v,branchvalues='total', marker_colors=mc,insidetextfont_size=25,name='2021年', textfont_size=25, textfont_color='white',root_color="lightgrey", hovertemplate='%{label}
%{value:,.0f}人
%{percentRoot:.1%}')) fig.update_layout(width=1200,height=800,margin=dict(t=120,l=0,r=0,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンへの入国移民の出生国の割合(2021年)', font_size=45,x=0.5,y=0.95)) fig.show() #################スライドP19(13. ワッフルチャート)########################## import plotly.graph_objects as go from plotly.subplots import make_subplots import numpy as np p=[72,56] xy=['x1','x2','y1','y2'] t=['余暇にラケットスポーツをする人','余暇に筋トレをする人'] n=[str(i) for i in range(1,11)] fig=make_subplots(rows=1,cols=2,subplot_titles=(t[0],t[1]),horizontal_spacing=0.1) fig.update_annotations(font_size=35,y=-0.18) for i in range(2): w=np.zeros(100) w[:p[i]]=1 c=['rgb(240,220,180)','rgb(200,140,45)'] if p[i]>99: c[0]='rgb(200,140,45)' fig.add_trace(go.Heatmap(x=n,y=n,z=w.reshape(10,10),xaxis=xy[i],yaxis=xy[i+2], colorscale= [[0,c[0]],[0.5,c[0]],[1,c[1]]], xgap=3,ygap=3,showscale=False,name=t[i],meta=p[i], hovertemplate='%{meta}%'), 1,i+1) fig.add_annotation(x=4.5,y=9.3,text=str(p[i])+'%',xref=xy[i],yref=xy[i+2]) fig.update_layout(width=1100,height=660,margin=dict(t=80,b=100), plot_bgcolor='white', font_size=32,font_color=c[1],hoverlabel_font_size=25, title=dict(text='2019年にセムラを食べた人の割合(スウェーデン)', font_size=45,x=0.5,y=0.95), yaxis1_scaleanchor=xy[0],yaxis2_scaleanchor=xy[1]) fig.show() #################スライドP20(14. 散布図)#################################### import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='VaccinValUt') fig=go.Figure(go.Scatter(x=df['Valdeltagande'],y=df['vaccin'],mode='markers', marker_size=9,text=df['Kommunnamn'],name='各市のデータ', hovertemplate='%{text}
投票率: %{x:,.1f}%
' '接種率: %{y:,.1f}%')) fig.update_layout(width=800,height=750,margin=dict(t=110), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの市議会選挙投票率と
COVID-19ワクチン接種率', font_size=40,x=0.50,y=0.95), xaxis=dict(title='投票率(%)',range=(48,100)), yaxis=dict(title='ワクチン接種率(%)',range=(48,100))) fig.show() #################スライドP21(15. バブルチャート)############################ import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='VaccinValUt') df=df.sort_values(['fbr'],ascending=False) fig=go.Figure(go.Scatter(x=df['Valdeltagande'],y=df['vaccin'],mode='markers', marker=dict(size=df['fbr'],sizemode='area',sizeref=0.1), text=df['Kommunnamn'],name='各市のデータ', hovertemplate='%{text}
投票率: %{x:,.1f}%
' '接種率: %{y:,.1f}%
外国出生者率: %{marker.size:,.1f}%')) fig.update_layout(width=800,height=750,margin=dict(t=110), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン各市の投票率と
ワクチン接種率と外国出生者率', font_size=40,x=0.50,y=0.95), xaxis=dict(title='投票率(%)',range=(48,100)), yaxis=dict(title='ワクチン接種率(%)',range=(48,100))) fig.show() #################スライドP22(16. 散布図行列)################################ import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='VaccinValUt') fig=go.Figure(go.Splom(dimensions=[dict(label='外国出生者率(%)',values=df['fbr']), dict(label='投票率(%)',values=df['Valdeltagande']), dict(label='ワクチン接種率(%)',values=df['vaccin'])], marker=dict(opacity=0.8,size=5), showupperhalf=False,diagonal_visible=False, text=df['Kommunnamn'],name='各市のデータ', hovertemplate='%{text}
(%{x:,.1f},%{y:,.1f})')) fig.update_layout(width=700,height=650,margin=dict(t=120), font_size=26,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン各市の投票率と
ワクチン接種率と外国出生者率', font_size=40,x=0.50,y=0.93)) fig.show() #################スライドP23(17. 3次元散布図)############################### import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='VaccinValUt') l=['X:外国出生者率(%)','Y:投票率(%)','Z:ワクチン接種率(%)'] ca=[1.4,1.4,1.4,-1.4,1.4,-1.4] fig=make_subplots(rows=1,cols=2,specs=[[{'type':'scene'}, {'type':'scene'}]], horizontal_spacing=0) for i in [1,2]: fig.add_trace(go.Scatter3d(x=df['fbr'], y=df['Valdeltagande'], z=df['vaccin'], mode="markers", marker=dict(size=3,color='#636EFA'), name='各市のデータ'),1,i) fig.update_layout(margin=dict(t=50,l=0,r=0,b=0),width=1180,height=580,showlegend=False, font_size=18,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン各市の投票率とワクチン接種率と外国出生者率', font_size=40,x=0.50,y=0.98)) fig.update_layout({'scene'+str(i+1):dict(xaxis_title=l[0],yaxis_title=l[1],zaxis_title=l[2], camera_eye=dict(x=ca[0+i],y=ca[2+i],z=ca[4+i])) for i in range(2)}) fig.show()0 #################スライドP24(18. 三角図)#################################### import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='UTbgK') tv=[0,0.2, 0.4, 0.6,0.8,1] tt=[' 0 ',' 0.2 ', ' 0.4 ',' 0.6 ',' 0.8 ',' 1 '] c=['darkblue','#CC0066','darkgreen'] fig=go.Figure(go.Scatterternary(a=df['DDDR'],b=df['DDFR'],c=df['FBR'], mode='markers',marker_size=5)) fig.update_layout(width=800,height=700,margin=dict(t=50,l=150, r=150,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン各市の住民の出自の割合', font_size=40,x=0.50,y=0.95), ternary=dict(aaxis=dict(title='',tickvals=tv,ticktext=tt,tickfont_color=c[0]), baxis=dict(title='',tickvals=tv,ticktext=tt,tickfont_color=c[1]), caxis=dict(title='',tickvals=tv,ticktext=tt,tickfont_color=c[2])), annotations=[dict(text='A. 親・本人とも国内生まれ',x=0.5,y=0.95, font_color=c[0],showarrow=False), dict(text='B. 親は外国生まれ',x=-0.3,y=0.03, font_color=c[1],showarrow=False), dict(text='C. 本人が外国生まれ',x=1.3,y=0.03, font_color=c[2],showarrow=False)]) fig.show() #################スライドP25(19. ヒストグラム)############################## import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='Inkomst2') fig=go.Figure(go.Bar(x=df['Income'],y=df['A']/1000)) tv=['0','1–49','100–149','200–249','300–349','400–449','500–549','600–649', '700–749','800–849','900–949','1,000–1,049', '1,100–1,149','1,200–1,249', '1,300–1,349', '1,400–1,449','1,450-1,499','1,500-'] fig.update_layout(width=1080,height=600,margin=dict(t=80,l=100,r=100,b=0,pad=5), font_size=26,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン住民(20~64歳)の年収のヒストグラム(2020年)', font_size=36,x=0.51,y=0.97), xaxis=dict(tickvals=tv,ticktext=tv,tickangle=45,title='年収(千クローナ)'), yaxis_title='人数(千人)', barmode='relative',bargap=0) fig.show() #################スライドP26(20. 人口ピラミッド)############################ import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='UppsalaFolk') fig=go.Figure(go.Bar(y=df['Age'],x=df['M']*-1, name='男性',orientation='h', meta=df['M'], hovertemplate='%{y}歳:%{meta:,}人')) fig.add_trace(go.Bar(y=df['Age'],x=df['F'],name='女性',orientation='h', hovertemplate='%{y}歳:%{x:,}人',marker_color='yellowgreen')) fig.update_layout(width=825,height=800,margin=dict(t=110,l=100,r=100,b=0,pad=5), font_size=26,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='ウプサラ市(スウェーデン)の人口ピラミッド
' '(2021年12月31日現在)', font_size=38,x=0.51,y=0.95), xaxis=dict(tickvals=[-10000,-5000,0,5000,10000], ticktext=['10,000','5,000','0','5,000','10,000'], tickfont_size=30,title='人口(人)'), yaxis=dict(title='年齢(歳)',tickfont_size=22), barmode='relative',bargap=0) fig.show() #################スライドP27(21. 密度プロット)############################## import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='InkomstK') c=['#00CC96','#636EFA','#FF9616'] fig=go.Figure(go.Violin(x=df['F'],name='女性',marker_color=c[0],box_visible=False)) fig.add_trace(go.Violin(x=df['M'],name='男性',marker_color=c[1],box_visible=False)) fig.add_trace(go.Violin(x=df['T'],name='全体',marker_color=c[2],box_visible=False)) fig.update_layout(width=630,height=750,margin=dict(t=110,l=50,r=10,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン住民の年収
(市ごとの中央値)の分布', font_size=40,x=0.53,y=0.95), showlegend=False, xaxis=dict(title='年収の中央値(千クローナ)',title_standoff=20)) fig.update_traces(orientation='h',side='positive',width=3,points=False) fig.show() #################スライドP28(22. 箱ひげ図)################################## import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='InkomstK') c=['#00CC96','#636EFA','#FF9616'] fig=go.Figure(go.Box(y=df['F'],name='女性',marker_color=c[0])) fig.add_trace(go.Box(y=df['M'],name='男性',marker_color=c[1])) fig.add_trace(go.Box(y=df['T'],name='全体',marker_color=c[2])) fig.update_layout(width=630,height=700,margin=dict(t=110,l=50,r=10,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン住民の年収
(市ごとの中央値)の分布', font_size=40,x=0.57,y=0.95), showlegend=False, yaxis=dict(title='年収の中央値(千クローナ)',title_standoff=30)) fig.show() #################スライドP29(23. バイオリン図)############################## import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='InkomstK') c=['#00CC96','#636EFA','#FF9616'] s=['F','M','T','女性','男性','全体'] t='年収の中央値(千クローナ)' fig=make_subplots(rows=1,cols=2,horizontal_spacing=0.15) for i in range(3): fig.add_trace(go.Violin(x=df[s[i]],name=s[i+3],marker_color=c[i], box_visible=True),1,1) fig.add_trace(go.Violin(y=df[s[i]],name=s[i+3],marker_color=c[i], box_visible=True,points='all',marker_size=3),1,2) fig.update_layout(width=1260,height=750,margin=dict(t=80,l=80,r=10,b=0), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデン住民の年収(市ごとの中央値)の分布', font_size=45,x=0.53,y=0.97), showlegend=False, xaxis1=dict(title=t,title_standoff=30), yaxis2=dict(title=t,title_standoff=0)) fig.show() #################スライドP30(24. レーダーチャート)########################## import plotly.graph_objects as go cat=['高校','成人教育','障がい者
向け活動','高齢者介護', '社会的弱者への支援・援助',' 幼稚園','小中学校','高校'] r1=[86.2,77.4,82.6,58.6,56.2,87.4,76.3,86.2] r2=[74.1,76.5,66.2,46.2,42.9,84.4,72.2,74.1] fig=go.Figure(go.Scatterpolar(r=r1,theta=cat,name='ウプサラ県')) fig.add_trace(go.Scatterpolar(r=r2,theta=cat,name='ストックホルム県')) fig.update_layout(width=800,height=600,margin=dict(t=80,l=130,r=100,b=0), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの市政に対する住民の評価
(2022年)', font_size=40,xanchor='left',x=0.05,y=0.92), polar=dict(angularaxis_rotation=90,gridshape='linear', radialaxis=dict(linewidth=0,range=[0,100], tickfont_size=23))) fig.show() #################スライドP31(25. ポーラーチャート)########################## import plotly.graph_objects as go cat=['高校','成人教育','障がい者
向け活動','高齢者介護', '社会的弱者への支援・援助',' 幼稚園','小中学校'] r1=[48.1,54.1,46.8,37.1,31.1,52.5,48.4] r2=[26.0,22.5,19.5,9.1,11.8,31.9,23.8] fig=go.Figure(go.Barpolar(r=r1,theta=cat,name='とても良い', marker=dict(color='#5D9FFF',opacity = 0.5))) fig.add_trace(go.Barpolar(r=r2,theta=cat,name='まあまあ良い', marker=dict(color='#BEEE68',opacity = 0.6))) fig.update_layout(width=800,height=600,margin=dict(t=80,l=130,r=100,b=0), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='ウプサラの市政に対する住民の評価
(2022年)', font_size=40,xanchor='left',x=0.05,y=0.92), polar=dict(angularaxis_rotation=90, radialaxis=dict(linewidth=0,range=[0,100], tickfont_size=25))) fig.show() #################スライドP32(26. デンドログラム)############################ import plotly.figure_factory as ff import pandas as pd import scipy.stats df=pd.read_excel('DataVL.xlsx',sheet_name='Laen2') df2=df[['MAge','MRent','DMigration','IMigration','TEdu','WHours','TFR','FHHouse']] a=scipy.stats.zscore(df2.to_numpy()) fig=ff.create_dendrogram(a,labels=df['county'].values.tolist()) fig.update_layout(width=1000,height=800,margin=dict(t=110,r=10), font_size=32,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの21県のデンドログラム', font_size=45,x=0.53,y=0.95), xaxis=dict(ticks='',range=(0,210)), yaxis=dict(title='距離',tickvals=[0,2,4,6,8], mirror=True,ticks='inside',tickwidth=2)) fig.show() #################スライドP35(27. 平行座標プロット)########################## import plotly.graph_objects as go import pandas as pd df = pd.read_excel('DataVL.xlsx',sheet_name='Laen2') c=[[0,'red'],[0.5,'darkgreen'],[1,'#0066FF']] a=['高等教育学歴','海外転入出','家賃', '持ち家率','出生率', '平均年齢','週労働時間','国内転入出'] b=['TEdu','IMigration','MRent','FHHouse','TFR','MAge','WHours','DMigration'] rank=[21,20,11,9,2,1,7,16,15,18,3,17,5,8,10,6,13,14,4,19,12] color=[1,1,0,0,0,0,0,2,0,1,0,1,0,0,0,0,0,0,0,1,0] d=[dict(tickvals=rank,ticktext=df['county'],label='県',values=rank)] for i in range(8): d.append(dict(tickformat=',',label=a[i],values=df[b[i]])) fig=go.Figure(go.Parcoords(line=dict(color=color,colorscale=c),dimensions=d)) fig.update_layout(width=1200,height=680,margin=dict(l=170, r=80, t=140, b=40), font_size=24,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの21県の平行座標プロット', font_size=45,x=0.53,y=0.96)) fig.show() #################スライドP36(28. ヒートマップ)############################## import plotly.figure_factory as ff z=[[75.9,64.2,69.1,64.7],[86.6,77.6,74.6,73.8],[86.8,85.0,84.1,84.8], [87.9,89.3,89.7,91.7],[90.2,92.9,93.3,94.9],[91.0,95.9,96.5,96.7]] xlab=['18~29 ','30~49 ','50~64 ','65~   '] ylab=['無所得','0~20','21~40','41~60','61~80','81~100'] fig=ff.create_annotated_heatmap(x=xlab,y=ylab,z=z,xgap=0,ygap=0,showscale=True) fig.update_layout(width=850,height=680,margin=dict(t=100,l=220), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンの年齢・所得別国政選挙投票率
(2018年)', font_size=40,xanchor='left',x=0,y=0.92), xaxis=dict(title='年齢(歳)',titlefont_size=35,side='bottom'), yaxis=dict(title='所得帯(下位からの%)',titlefont_size=35)) fig.show() #################スライドP37(29. 階級区分図)################################ import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='SweExport2022c') df['Export']/=1000000 gt=['van der grinten4','orthographic'] fig=make_subplots(rows=1,cols=2,horizontal_spacing=0, specs=[[dict(type='choropleth'),dict(type='choropleth')]]) for i in [1,2]: fig.add_trace(go.Choropleth(locations=df['iso_alpha'],z=df['Export'], colorscale ='Blues',name='輸出2022'),1,i) fig.update_layout(width=1400,height=500,margin=dict(t=60,l=10,r=10,b=10), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンから世界各国・地域への輸出額' '(2022年、単位:10億クローナ)', font_size=39,x=0.5,y=0.98)) fig.update_layout({'geo'+str(i+1):dict(projection=(dict(type=gt[i], rotation=dict(lat=35.7,lon=139.8)))) for i in range(2)}) fig.show() #################スライドP38(30. 比例シンボルマップ)######################## import plotly.graph_objects as go import pandas as pd df=pd.read_excel('DataVL.xlsx',sheet_name='SweExport2022c') df['Export']/=1000000 df=df.sort_values(['Export'],ascending=False) fig=go.Figure(go.Scattergeo(locations=df['iso_alpha'], marker=dict(size=df['Export'],sizemode='area',sizeref=0.1, color=df['Export'],colorscale ='Bluered', showscale=True), name='輸出2022',showlegend=False,meta=df['Country'], hovertemplate='%{meta}
%{marker.size:,.1f}
')) fig.update_layout(width=1300,height=600,margin=dict(t=60,l=10,r=10,b=10), font_size=28,font_color='darkblue',hoverlabel_font_size=25, title=dict(text='スウェーデンから世界各国・地域への輸出額' '(2022年、単位:10億クローナ)', font_size=36,x=0.51,y=0.98), geo_projection=(dict(type='natural earth', rotation=dict(lat=35.7,lon=139.8))), geo_showcountries=True) fig.show() #################スライドP39(31. ワードクラウド)############################ import plotly.graph_objects as go import pandas as pd from wordcloud import WordCloud df=pd.read_excel('DataVL.xlsx',sheet_name='sampletext2') text=' '.join(t.lower() for t in df['Text']) sw=['en','det','med','ett','att','den','på','inom','är','var','i','vid','och', 'som','till','men','eller','för','om','denna','detta','från','av','vi','jag', 'du','ni','er','min','mitt','sin','sitt','under','sedan','enligt','har','inte', 'kan','ska','vara','gör','göra','han','hon','får','någon','något','några'] fig=go.Figure(go.Image(z=WordCloud(width=1200, height=800,background_color='white', stopwords=sw).generate(text),hoverinfo='none')) fig.update_layout(width=1200,height=850,margin=dict(l=0,r=0,t=100,b=0), font_color='darkblue', title=dict(text='スウェーデンの人名に関する法律の条文を可視化', font_size=54,x=0.50,y=0.98), xaxis_showticklabels=False,yaxis_showticklabels=False) fig.show() ##############################################################################