{"id":235457,"date":"2022-02-14T08:06:42","date_gmt":"2022-02-14T00:06:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=235457"},"modified":"2022-02-08T10:07:11","modified_gmt":"2022-02-08T02:07:11","slug":"python-draw-sankey-diagram","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-draw-sankey-diagram.html","title":{"rendered":"Python\u7ed8\u5236\u6851\u57fa\u56fe"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u5fc5\u987b\u53ef\u89c6\u5316\u6570\u636e\u5982\u4f55\u5728\u5b9e\u4f53\u4e4b\u95f4\u6d41\u52a8\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4ee5\u5c45\u6c11\u5982\u4f55\u4ece\u4e00\u4e2a\u56fd\u5bb6\u8fc1\u79fb\u5230\u53e6\u4e00\u4e2a\u56fd\u5bb6\u4e3a\u4f8b\u3002\u8fd9\u91cc\u6f14\u793a\u4e86\u6709\u591a\u5c11\u5c45\u6c11\u4ece\u82f1\u683c\u5170\u8fc1\u79fb\u5230\u5317\u7231\u5c14\u5170\u3001\u82cf\u683c\u5170\u548c\u5a01\u5c14\u58eb\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6851\u57fa\u56fe\u7b80\u4ecb<\/strong><\/div>\n

\u4ece\u8fd9\u4e2a \u6851\u57fa\u56fe (Sankey)\u53ef\u89c6\u5316\u4e2d\u53ef\u4ee5\u660e\u663e\u770b\u51fa\uff0c\u4eceEngland\u8fc1\u79fb\u5230Wales\u7684\u5c45\u6c11\u591a\u4e8e\u4eceScotland\u6216Northern Ireland\u8fc1\u79fb\u7684\u5c45\u6c11\u3002<\/p>\n

\"\"<\/p>\n

\u4ec0\u4e48\u662f\u6851\u57fa\u56fe?<\/strong><\/span><\/div>\n

\u6851\u57fa\u56fe\u901a\u5e38\u63cf\u7ed8 \u4ece\u4e00\u4e2a\u5b9e\u4f53(\u6216\u8282\u70b9)\u5230\u53e6\u4e00\u4e2a\u5b9e\u4f53(\u6216\u8282\u70b9)\u7684\u6570\u636e\u6d41\u3002<\/p>\n

\u6570\u636e\u6d41\u5411\u7684\u5b9e\u4f53\u88ab\u79f0\u4e3a\u8282\u70b9\uff0c\u6570\u636e\u6d41\u8d77\u6e90\u7684\u8282\u70b9\u662f\u6e90\u8282\u70b9(\u4f8b\u5982\u5de6\u4fa7\u7684England)\uff0c\u6d41\u7ed3\u675f\u7684\u8282\u70b9\u662f \u76ee\u6807\u8282\u70b9(\u4f8b\u5982\u53f3\u4fa7\u7684Wales)\u3002\u6e90\u8282\u70b9\u548c\u76ee\u6807\u8282\u70b9\u901a\u5e38\u8868\u793a\u4e3a\u5e26\u6709\u6807\u7b7e\u7684\u77e9\u5f62\u3002<\/p>\n

\u6d41\u52a8\u672c\u8eab\u7531\u76f4\u7ebf\u6216\u66f2\u7ebf\u8def\u5f84\u8868\u793a\uff0c\u79f0\u4e3a\u94fe\u63a5\u3002\u6d41\/\u94fe\u63a5\u7684\u5bbd\u5ea6\u4e0e\u6d41\u7684\u91cf\/\u6570\u91cf\u6210\u6b63\u6bd4\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4ece\u82f1\u683c\u5170\u5230\u5a01\u5c14\u58eb\u7684\u6d41\u52a8(\u5373\u5c45\u6c11\u8fc1\u79fb)\u6bd4\u4ece\u82f1\u683c\u5170\u5230\u82cf\u683c\u5170\u6216\u5317\u7231\u5c14\u5170\u7684\u6d41\u52a8(\u5373\u5c45\u6c11\u8fc1\u79fb)\u66f4\u5e7f\u6cdb(\u66f4\u591a)\uff0c\u8868\u660e\u8fc1\u79fb\u5230\u5a01\u5c14\u58eb\u7684\u5c45\u6c11\u6570\u91cf\u591a\u4e8e\u5176\u4ed6\u56fd\u5bb6\u3002<\/p>\n

\u6851\u57fa\u56fe\u53ef\u7528\u4e8e\u8868\u793a\u80fd\u91cf\u3001\u91d1\u94b1\u3001\u6210\u672c\u7684\u6d41\u52a8\uff0c\u4ee5\u53ca\u4efb\u4f55\u5177\u6709\u6d41\u52a8\u6982\u5ff5\u7684\u4e8b\u7269\u3002<\/p>\n

\u7c73\u7eb3\u5c14\u5173\u4e8e\u62ff\u7834\u4ed1\u5165\u4fb5\u4fc4\u7f57\u65af\u7684\u7ecf\u5178\u56fe\u8868\u53ef\u80fd\u662f\u6851\u57fa\u56fe\u8868\u6700\u8457\u540d\u7684\u4f8b\u5b50\u3002\u8fd9\u79cd\u4f7f\u7528\u6851\u57fa\u56fe\u7684\u53ef\u89c6\u5316\u975e\u5e38\u6709\u6548\u5730\u663e\u793a\u4e86\u6cd5\u56fd\u519b\u961f\u5728\u524d\u5f80\u4fc4\u7f57\u65af\u548c\u8fd4\u56de\u7684\u9014\u4e2d\u662f\u5982\u4f55\u8fdb\u6b65(\u6216\u51cf\u5c11?)\u7684\u3002<\/p>\n

\"\"<\/p>\n

\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 python \u7684 plotly \u7ed8\u5236\u6851\u57fa\u56fe\u3002<\/p>\n

\u5982\u4f55\u7ed8\u5236\u6851\u57fa\u56fe?<\/strong><\/div>\n

\u672c\u6587\u4f7f\u7528 2021 \u5e74\u5965\u8fd0\u4f1a\u6570\u636e\u96c6\u7ed8\u5236\u6851\u57fa\u56fe\u3002\u8be5\u6570\u636e\u96c6\u5305\u542b\u6709\u5173\u5956\u724c\u603b\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\u2014\u2014\u56fd\u5bb6\u3001\u5956\u724c\u603b\u6570\u4ee5\u53ca\u91d1\u724c\u3001\u94f6\u724c\u548c\u94dc\u724c\u7684\u5355\u9879\u603b\u6570\u3002\u6211\u4eec\u901a\u8fc7\u7ed8\u5236\u4e00\u4e2a\u6851\u57fa\u56fe\u6765\u4e86\u89e3\u4e00\u4e2a\u56fd\u5bb6\u8d62\u5f97\u7684\u91d1\u724c\u3001\u94f6\u724c\u548c\u94dc\u724c\u6570\u3002<\/p>\n

\r\ndf_medals = pd.read_excel(\"data\/Medals.xlsx\")\r\nprint(df_medals.info())\r\ndf_medals.rename(columns={'Team\/NOC':'Country', 'Total': 'Total Medals', 'Gold':'Gold Medals', 'Silver': 'Silver Medals', 'Bronze': 'Bronze Medals'}, inplace=True)\r\ndf_medals.drop(columns=['Unnamed: 7','Unnamed: 8','Rank by Total'], inplace=True)\r\n\r\ndf_medals\r\n<\/pre>\n
\r\n\r\nRangeIndex: 93 entries, 0 to 92\r\nData columns (total 9 columns):\r\n #   Column         Non-Null Count  Dtype  \r\n---  ------         --------------  -----  \r\n 0   Rank           93 non-null     int64  \r\n 1   Team\/NOC       93 non-null     object \r\n 2   Gold           93 non-null     int64  \r\n 3   Silver         93 non-null     int64  \r\n 4   Bronze         93 non-null     int64  \r\n 5   Total          93 non-null     int64  \r\n 6   Rank by Total  93 non-null     int64  \r\n 7   Unnamed: 7     0 non-null      float64\r\n 8   Unnamed: 8     1 non-null      float64\r\ndtypes: float64(2), int64(6), object(1)\r\nmemory usage: 6.7+ KB\r\nNone\r\n<\/class><\/pre>\n

\"\"<\/p>\n

\u6851\u57fa\u56fe\u7ed8\u56fe\u57fa\u7840<\/strong><\/span><\/div>\n

\u4f7f\u7528 plotly \u7684 go.Sankey\uff0c\u8be5\u65b9\u6cd5\u5e26\u67092 \u4e2a\u53c2\u6570 \u2014\u2014nodes \u548c links (\u8282\u70b9\u548c\u94fe\u63a5)\u3002<\/p>\n

\u6ce8\u610f\uff1a\u6240\u6709\u8282\u70b9\u2014\u2014\u6e90\u548c\u76ee\u6807\u90fd\u5e94\u8be5\u6709\u552f\u4e00\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n

\u5728\u672c\u6587\u5965\u6797\u5339\u514b\u5956\u724c\u6570\u636e\u96c6\u60c5\u51b5\u4e2d\uff1a<\/p>\n

Source\u662f\u56fd\u5bb6\u3002\u5c06\u524d 3 \u4e2a\u56fd\u5bb6(\u7f8e\u56fd\u3001\u4e2d\u56fd\u548c\u65e5\u672c)\u89c6\u4e3a\u6e90\u8282\u70b9\u3002\u7528\u4ee5\u4e0b(\u552f\u4e00\u7684)\u6807\u8bc6\u7b26\u3001\u6807\u7b7e\u548c\u989c\u8272\u6765\u6807\u8bb0\u8fd9\u4e9b\u6e90\u8282\u70b9\uff1a<\/p>\n

    0\uff1a\u7f8e\u56fd\uff1a\u7eff\u8272<\/ol>\n
      1\uff1a\u4e2d\u56fd\uff1a\u84dd\u8272<\/ol>\n
        2\uff1a\u65e5\u672c\uff1a\u6a59\u8272<\/ol>\n

        Target\u662f\u91d1\u724c\u3001\u94f6\u724c\u6216\u94dc\u724c\u3002\u7528\u4ee5\u4e0b(\u552f\u4e00\u7684)\u6807\u8bc6\u7b26\u3001\u6807\u7b7e\u548c\u989c\u8272\u6765\u6807\u8bb0\u8fd9\u4e9b\u76ee\u6807\u8282\u70b9\uff1a<\/p>\n

          3\uff1a\u91d1\u724c\uff1a\u91d1\u8272<\/ol>\n
            4\uff1a\u94f6\u724c\uff1a\u94f6\u8272<\/ol>\n
              5\uff1a\u94dc\u724c\uff1a\u68d5\u8272<\/ol>\n

              Link(\u6e90\u8282\u70b9\u548c\u76ee\u6807\u8282\u70b9\u4e4b\u95f4)\u662f\u6bcf\u79cd\u7c7b\u578b\u5956\u724c\u7684\u6570\u91cf\u3002\u5728\u6bcf\u4e2a\u6e90\u4e2d\u67093\u4e2a\u94fe\u63a5\uff0c\u6bcf\u4e2a\u94fe\u63a5\u90fd\u4ee5\u76ee\u6807\u7ed3\u5c3e\u2014\u2014\u91d1\u724c\u3001\u94f6\u724c\u548c\u94dc\u724c\u3002\u6240\u4ee5\u603b\u5171\u67099\u4e2a\u94fe\u63a5\u3002\u6bcf\u4e2a\u73af\u8282\u7684\u5bbd\u5ea6\u5e94\u4e3a\u91d1\u724c\u3001\u94f6\u724c\u548c\u94dc\u724c\u7684\u6570\u91cf\u3002\u7528\u4ee5\u4e0b\u6e90\u6807\u8bb0\u8fd9\u4e9b\u94fe\u63a5\u5230\u76ee\u6807\u3001\u503c\u548c\u989c\u8272\uff1a<\/p>\n

                0 (\u7f8e\u56fd) \u81f3 3,4,5 : 39, 41, 33<\/ol>\n
                  1 (\u4e2d\u56fd) \u81f3 3,4,5 : 38, 32, 18<\/ol>\n
                    2 (\u65e5\u672c) \u81f3 3,4,5 : 27, 14, 17<\/ol>\n

                    \u9700\u8981\u5b9e\u4f8b\u5316 2 \u4e2a python dict \u5bf9\u8c61\u6765\u8868\u793a<\/p>\n

                      nodes (\u6e90\u548c\u76ee\u6807)\uff1a\u6807\u7b7e\u548c\u989c\u8272\u4f5c\u4e3a\u5355\u72ec\u7684\u5217\u8868\u548c<\/ol>\n
                        links\uff1a\u6e90\u8282\u70b9\u3001\u76ee\u6807\u8282\u70b9\u3001\u503c(\u5bbd\u5ea6)\u548c\u94fe\u63a5\u7684\u989c\u8272\u4f5c\u4e3a\u5355\u72ec\u7684\u5217\u8868<\/ol>\n

                        \u5e76\u5c06\u5176\u4f20\u9012\u7ed9plotly\u7684 go.Sankey\u3002<\/p>\n

                        \u5217\u8868\u7684\u6bcf\u4e2a\u7d22\u5f15(\u6807\u7b7e\u3001\u6e90\u3001\u76ee\u6807\u3001\u503c\u548c\u989c\u8272)\u5206\u522b\u5bf9\u5e94\u4e00\u4e2a\u8282\u70b9\u6216\u94fe\u63a5\u3002<\/p>\n

                        \r\nNODES = dict( \r\n#         0                           1                             2        3       4         5                         \r\nlabel = [\"United States of America\", \"People's Republic of China\", \"Japan\", \"Gold\", \"Silver\", \"Bronze\"],\r\ncolor = [\"seagreen\",                 \"dodgerblue\",                 \"orange\", \"gold\", \"silver\", \"brown\" ],)\r\nLINKS = dict(   \r\n  source = [  0,  0,  0,  1,  1,  1,  2,  2,  2], # \u94fe\u63a5\u7684\u8d77\u70b9\u6216\u6e90\u8282\u70b9\r\n  target = [  3,  4,  5,  3,  4,  5,  3,  4,  5], # \u94fe\u63a5\u7684\u76ee\u7684\u5730\u6216\u76ee\u6807\u8282\u70b9\r\n  value =  [ 39, 41, 33, 38, 32, 18, 27, 14, 17], # \u94fe\u63a5\u7684\u5bbd\u5ea6\uff08\u6570\u91cf\uff09\r\n# \u94fe\u63a5\u7684\u989c\u8272\r\n# \u76ee\u6807\u8282\u70b9\uff1a       3-Gold          4-Silver        5-Bronze\r\n  color = [   \r\n  \"lightgreen\",   \"lightgreen\",   \"lightgreen\",      # \u6e90\u8282\u70b9\uff1a0 - \u7f8e\u56fd States of America\r\n  \"lightskyblue\", \"lightskyblue\", \"lightskyblue\",    # \u6e90\u8282\u70b9\uff1a1 - \u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fdChina\r\n  \"bisque\",       \"bisque\",       \"bisque\"],)        # \u6e90\u8282\u70b9\uff1a2 - \u65e5\u672c\r\ndata = go.Sankey(node = NODES, link = LINKS)\r\nfig = go.Figure(data)\r\nfig.show()\r\n<\/pre>\n

                        \"\"<\/p>\n

                        \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u57fa\u672c\u7684\u6851\u57fa\u56fe\u3002\u4f46\u662f\u5426\u6ce8\u610f\u5230\u56fe\u8868\u592a\u5bbd\u5e76\u4e14\u94f6\u724c\u51fa\u73b0\u5728\u91d1\u724c\u4e4b\u524d?<\/p>\n

                        \u63a5\u4e0b\u6765\u4ecb\u7ecd\u5982\u4f55\u8c03\u6574\u8282\u70b9\u7684\u4f4d\u7f6e\u548c\u5bbd\u5ea6\u3002<\/p>\n

                        \u8c03\u6574\u8282\u70b9\u4f4d\u7f6e\u548c\u56fe\u8868\u5bbd\u5ea6<\/strong><\/span><\/div>\n

                        \u4e3a\u8282\u70b9\u6dfb\u52a0 x \u548c y \u4f4d\u7f6e\u4ee5\u660e\u786e\u6307\u5b9a\u8282\u70b9\u7684\u4f4d\u7f6e\u3002\u503c\u5e94\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u3002<\/p>\n

                        \r\nNODES = dict( \r\n#         0                           1                             2        3       4         5                         \r\nlabel = [\"United States of America\", \"People's Republic of China\", \"Japan\", \"Gold\", \"Silver\", \"Bronze\"],\r\ncolor = [\"seagreen\",                 \"dodgerblue\",                 \"orange\", \"gold\", \"silver\", \"brown\" ],)\r\nx = [     0,                          0,                            0,        0.5,    0.5,      0.5],\r\ny = [     0,                          0.5,                          1,        0.1,    0.5,        1],)\r\ndata = go.Sankey(node = NODES, link = LINKS)\r\nfig = go.Figure(data)\r\nfig.update_layout(title=\"Olympics - 2021: Country &  Medals\",  font_size=16)\r\nfig.show()\r\n<\/pre>\n

                        \u4e8e\u662f\u5f97\u5230\u4e86\u4e00\u4e2a\u7d27\u51d1\u7684\u6851\u57fa\u56fe\uff1a<\/p>\n

                        \"\"<\/p>\n

                        \u4e0b\u9762\u770b\u770b\u4ee3\u7801\u4e2d\u4f20\u9012\u7684\u5404\u79cd\u53c2\u6570\u5982\u4f55\u6620\u5c04\u5230\u56fe\u4e2d\u7684\u8282\u70b9\u548c\u94fe\u63a5\u3002<\/p>\n

                        \"\"<\/p>\n

                        \u6dfb\u52a0\u6709\u610f\u4e49\u7684\u60ac\u505c\u6807\u7b7e<\/strong><\/span><\/div>\n

                        \u6211\u4eec\u90fd\u77e5\u9053plotly\u7ed8\u56fe\u662f\u4ea4\u4e92\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9f20\u6807\u60ac\u505c\u5728\u8282\u70b9\u548c\u94fe\u63a5\u4e0a\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n

                        \"\"<\/a><\/p>\n

                        \u5f53\u5c06\u9f20\u6807\u60ac\u505c\u5728\u56fe\u4e0a\uff0c\u5c06\u4f1a\u663e\u793a\u8be6\u7ec6\u4fe1\u606f\u3002\u60ac\u505c\u6807\u7b7e\u4e2d\u663e\u793a\u7684\u4fe1\u606f\u662f\u9ed8\u8ba4\u6587\u672c\uff1a\u8282\u70b9\u3001\u8282\u70b9\u540d\u79f0\u3001\u4f20\u5165\u6d41\u6570\u3001\u4f20\u51fa\u6d41\u6570\u548c\u603b\u503c\u3002<\/p>\n

                        \u4f8b\u5982\uff1a<\/p>\n

                          \u8282\u70b9\u7f8e\u56fd\u5171\u83b7\u5f9711\u679a\u5956\u724c(=39\u91d1+41\u94f6+33\u94dc)<\/ol>\n
                            \u8282\u70b9\u91d1\u724c\u5171\u6709104\u679a\u5956\u724c(=\u7f8e\u56fd39\u679a\uff0c\u4e2d\u56fd38\u679a\uff0c\u65e5\u672c27\u679a)<\/ol>\n

                            \u5982\u679c\u6211\u4eec\u89c9\u5f97\u8fd9\u4e9b\u6807\u7b7e\u592a\u5197\u957f\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6b64\u8fdb\u7a0b\u6539\u8fdb\u3002\u4f7f\u7528hovertemplate\u53c2\u6570\u6539\u8fdb\u60ac\u505c\u6807\u7b7e\u7684\u683c\u5f0f<\/p>\n

                              \u5bf9\u4e8e\u8282\u70b9\uff0c\u7531\u4e8ehoverlabels \u6ca1\u6709\u63d0\u4f9b\u65b0\u4fe1\u606f\uff0c\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u7a7ahovertemplate = \"\"\u6765\u53bb\u6389hoverlabel<\/ol>\n
                                \u5bf9\u4e8e\u94fe\u63a5\uff0c\u53ef\u4ee5\u4f7f\u6807\u7b7e\u7b80\u6d01\uff0c\u683c\u5f0f\u4e3a-<\/ol>\n
                                  \u5bf9\u4e8e\u8282\u70b9\u548c\u94fe\u63a5\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528\u540e\u7f00\"Medals\"\u663e\u793a\u503c\u3002\u4f8b\u5982 113 \u679a\u5956\u724c\u800c\u4e0d\u662f 113 \u679a\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5177\u6709\u9002\u5f53valueformat\u548cvaluesuffix\u7684update_traces\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/ol>\n
                                  \r\nNODES = dict( \r\n#         0                           1                               2        3       4           5\r\nlabel = [\"United States of America\", \"People's Republic of China\",   \"Japan\", \"Gold\", \"Silver\", \"Bronze\"],\r\ncolor = [                \"seagreen\",                 \"dodgerblue\",  \"orange\", \"gold\", \"silver\", \"brown\" ],\r\nx     = [                         0,                            0,         0,    0.5,      0.5,      0.5],\r\ny     = [                         0,                          0.5,         1,    0.1,      0.5,        1],\r\nhovertemplate=\" \",)\r\n\r\nLINK_LABELS = []\r\nfor country in [\"USA\",\"China\",\"Japan\"]:\r\n    for medal in [\"Gold\",\"Silver\",\"Bronze\"]:\r\n        LINK_LABELS.append(f\"{country}-{medal}\")\r\nLINKS = dict(source = [  0,  0,  0,  1,  1,  1,  2,  2,  2], \r\n       # \u94fe\u63a5\u7684\u8d77\u70b9\u6216\u6e90\u8282\u70b9\r\n       target = [  3,  4,  5,  3,  4,  5,  3,  4,  5], \r\n       # \u94fe\u63a5\u7684\u76ee\u7684\u5730\u6216\u76ee\u6807\u8282\u70b9\r\n       value =  [ 39, 41, 33, 38, 32, 18, 27, 14, 17], \r\n       # \u94fe\u63a5\u7684\u5bbd\u5ea6\uff08\u6570\u91cf\uff09 \r\n             # \u94fe\u63a5\u7684\u989c\u8272\r\n             # \u76ee\u6807\u8282\u70b9\uff1a3-Gold          4 -Silver        5-Bronze\r\n             color = [\"lightgreen\",   \"lightgreen\",   \"lightgreen\",   # \u6e90\u8282\u70b9\uff1a0 - \u7f8e\u56fd\r\n                      \"lightskyblue\", \"lightskyblue\", \"lightskyblue\", # \u6e90\u8282\u70b9\uff1a1 - \u4e2d\u56fd\r\n                      \"bisque\",       \"bisque\",       \"bisque\"],      # \u6e90\u8282\u70b9\uff1a2 - \u65e5\u672c\r\n             label = LINK_LABELS, \r\n             hovertemplate=\"%{label}\",)\r\n\r\ndata = go.Sankey(node = NODES, link = LINKS)\r\nfig = go.Figure(data)\r\nfig.update_layout(title=\"Olympics - 2021: Country &  Medals\",  \r\n                  font_size=16, width=1200, height=500,)\r\nfig.update_traces(valueformat='3d', \r\n                  valuesuffix='Medals', \r\n                  selector=dict(type='sankey'))\r\nfig.update_layout(hoverlabel=dict(bgcolor=\"lightgray\",\r\n                                  font_size=16,\r\n                                  font_family=\"Rockwell\"))\r\nfig.show(\"png\") #fig.show()\r\n<\/pre>\n

                                  \"\"<\/p>\n

                                  \u5bf9\u591a\u4e2a\u8282\u70b9\u548c\u7ea7\u522b\u8fdb\u884c\u6cdb\u5316\u76f8\u5bf9\u4e8e\u94fe\u63a5\uff0c\u8282\u70b9\u88ab\u79f0\u4e3a\u6e90\u548c\u76ee\u6807\u3002\u4f5c\u4e3a\u4e00\u4e2a\u94fe\u63a5\u76ee\u6807\u7684\u8282\u70b9\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u94fe\u63a5\u7684\u6e90\u3002<\/p>\n

                                  \u8be5\u4ee3\u7801\u53ef\u4ee5\u63a8\u5e7f\u5230\u5904\u7406\u6570\u636e\u96c6\u4e2d\u7684\u6240\u6709\u56fd\u5bb6\u3002<\/p>\n

                                  \u8fd8\u53ef\u4ee5\u5c06\u56fe\u8868\u6269\u5c55\u5230\u53e6\u4e00\u4e2a\u5c42\u6b21\uff0c\u4ee5\u53ef\u89c6\u5316\u5404\u56fd\u7684\u5956\u724c\u603b\u6570\u3002<\/p>\n

                                  \r\nNUM_COUNTRIES = 5\r\nX_POS, Y_POS = 0.5, 1\/(NUM_COUNTRIES-1)\r\nNODE_COLORS = [\"seagreen\", \"dodgerblue\", \"orange\", \"palevioletred\", \"darkcyan\"]\r\nLINK_COLORS = [\"lightgreen\", \"lightskyblue\", \"bisque\", \"pink\", \"lightcyan\"]\r\n\r\nsource = []\r\nnode_x_pos, node_y_pos = [], []\r\nnode_labels, node_colors = [], NODE_COLORS[0:NUM_COUNTRIES]\r\nlink_labels, link_colors, link_values = [], [], [] \r\n\r\n# \u7b2c\u4e00\u7ec4\u94fe\u63a5\u548c\u8282\u70b9\r\nfor i in range(NUM_COUNTRIES):\r\n    source.extend([i]*3)\r\n    node_x_pos.append(0.01)\r\n    node_y_pos.append(round(i*Y_POS+0.01,2))\r\n    country = df_medals['Country'][i]\r\n    node_labels.append(country) \r\n    for medal in [\"Gold\", \"Silver\", \"Bronze\"]:\r\n        link_labels.append(f\"{country}-{medal}\")\r\n        link_values.append(df_medals[f\"{medal} Medals\"][i])\r\n    link_colors.extend([LINK_COLORS[i]]*3)\r\n\r\nsource_last = max(source)+1\r\ntarget = [ source_last, source_last+1, source_last+2] * NUM_COUNTRIES\r\ntarget_last = max(target)+1\r\n\r\nnode_labels.extend([\"Gold\", \"Silver\", \"Bronze\"])\r\nnode_colors.extend([\"gold\", \"silver\", \"brown\"])\r\nnode_x_pos.extend([X_POS, X_POS, X_POS])\r\nnode_y_pos.extend([0.01, 0.5, 1])\r\n\r\n# \u6700\u540e\u4e00\u7ec4\u94fe\u63a5\u548c\u8282\u70b9\r\nsource.extend([ source_last, source_last+1, source_last+2])\r\ntarget.extend([target_last]*3)\r\nnode_labels.extend([\"Total Medals\"])\r\nnode_colors.extend([\"grey\"])\r\nnode_x_pos.extend([X_POS+0.25])\r\nnode_y_pos.extend([0.5])\r\n\r\nfor medal in [\"Gold\",\"Silver\",\"Bronze\"]:\r\n    link_labels.append(f\"{medal}\")\r\n    link_values.append(df_medals[f\"{medal} Medals\"][:i+1].sum())\r\nlink_colors.extend([\"gold\", \"silver\", \"brown\"])\r\n\r\nprint(\"node_labels\", node_labels)\r\nprint(\"node_x_pos\", node_x_pos); print(\"node_y_pos\", node_y_pos)\r\n<\/pre>\n
                                  \r\nnode_labels ['United States of America', \"People's Republic of China\", \r\n             'Japan', 'Great Britain', 'ROC', 'Gold', 'Silver', \r\n             'Bronze', 'Total Medals']\r\nnode_x_pos [0.01, 0.01, 0.01, 0.01, 0.01, 0.5, 0.5, 0.5, 0.75]\r\nnode_y_pos [0.01, 0.26, 0.51, 0.76, 1.01, 0.01, 0.5, 1, 0.5]\r\n<\/pre>\n
                                  \r\n# \u663e\u793a\u7684\u56fe\r\nNODES = dict(pad  = 20, thickness = 20, \r\n             line = dict(color = \"lightslategrey\",\r\n                         width = 0.5),\r\n             hovertemplate=\" \",\r\n             label = node_labels, \r\n             color = node_colors,\r\n             x = node_x_pos, \r\n             y = node_y_pos, )\r\nLINKS = dict(source = source, \r\n             target = target, \r\n             value = link_values, \r\n             label = link_labels, \r\n             color = link_colors,\r\n             hovertemplate=\"%{label}\",)\r\ndata = go.Sankey(arrangement='snap', \r\n                 node = NODES, \r\n                 link = LINKS)\r\nfig = go.Figure(data)\r\nfig.update_traces(valueformat='3d', \r\n                  valuesuffix=' Medals', \r\n                  selector=dict(type='sankey'))\r\nfig.update_layout(title=\"Olympics - 2021: Country &  Medals\",  \r\n                  font_size=16,  \r\n                  width=1200,\r\n                  height=500,)\r\nfig.update_layout(hoverlabel=dict(bgcolor=\"grey\", \r\n                                  font_size=14, \r\n                                  font_family=\"Rockwell\"))\r\nfig.show(\"png\") <\/pre>\n

                                  \"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

                                  \u4ece\u8fd9\u4e2a \u6851\u57fa\u56fe (Sankey)\u53ef\u89c6\u5316\u4e2d\u53ef\u4ee5\u660e\u663e\u770b\u51fa\uff0c\u4eceEngland\u8fc1\u79fb\u5230Wales\u7684\u5c45\u6c11\u591a\u4e8e\u4eceScotla […]<\/p>\n","protected":false},"author":1329,"featured_media":235486,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-235457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=235457"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235457\/revisions"}],"predecessor-version":[{"id":235487,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235457\/revisions\/235487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235486"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=235457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=235457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=235457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}