在Python中使用plotly时,映射值不会按州显示在地图上

发布于 2025-01-15 15:41:27 字数 5870 浏览 1 评论 0原文

我是 python 新手,甚至是plotly 新手。我正在尝试利用plotly.express 将每个州的房地产百分比变化值(“Change_From_2010”列)从“ST_FIPS_Merge.csv”文件映射到“gz_2010_us_040_00_500k (1).json”文件。我计划对数据框的每一年(2011-2019)执行此操作。我先尝试2011年。代码运行,但地图上没有显示。

我的代码如下:

import csv
import os
import re
import numpy as np
import pandas as pd
import plotly.express as px
import json

IN_PATH = os.path.join("data", "ST_Fips_Merge.csv")
IN_PATH_JSON = os.path.join("data", "gz_2010_us_040_00_500k (1).json")

state_fips = pd.read_csv(IN_PATH,dtype={'FIPS':str})

with open (IN_PATH_JSON) as response:
    states = json.load(response)

twenty_eleven = state_fips[state_fips['YEAR']== 2011]

figure1 = px.choropleth(twenty_eleven, geojson=states, locations = 'FIPS', color = 'Change_From_2010', color_continuous_scale= "Bluered_r", scope = "usa", labels={'Change_From_2010':'Percent Change from 2010 in Housing Price'})

figure1.show()

文件的最小工作示例(文本):

百分比变化csv文件:

0,0,Alabama,2010,100870.33,,0.0, 01
1,1,Alabama,2011,99030.45,-1.824005136098994,-1.824005136098994, 01
2,2,Alabama,2012,102378.52,3.3808490216898024,1.4951770257914276, 01
3,3,Alabama,2013,104504.46,2.076548869821515,3.602773977243867, 01
4,4,Alabama,2014,108094.88,3.4356619803594945,7.1622150933778, 01
5,5,Alabama,2015,111645.37,3.2846051542866794,10.682070733782666, 01
6,6,Alabama,2016,115763.57,3.688643783436807,14.764738055283466, 01
7,7,Alabama,2017,120995.84,4.519789774969785,19.951862951177013, 01
8,8,Alabama,2018,128110.49,5.880078191117977,27.0051262844089, 01
9,9,Alabama,2019,139715.88999999998,9.058899080005055,38.5103925009465, 01
10,10,Alaska,2010,53768.14,-61.51608811281236,0.0, 02
11,11,Alaska,2011,53398.4,-0.6876562960890897,-0.6876562960890897, 02
12,12,Alaska,2012,54604.61,2.258887906753748,1.5556982257522778, 02
13,13,Alaska,2013,53232.060000000005,-2.513615608645492,-0.9970216563191414, 02
14,14,Alaska,2014,53669.59,0.8219294913629049,-0.1832869799848091, 02
15,15,Alaska,2015,55600.7,3.598145616540016,3.4082637041191965, 02
16,16,Alaska,2016,56822.3,2.1970946408948144,5.680241124204777, 02
17,17,Alaska,2017,59728.99,5.11540363554448,11.086212020724528, 02
18,18,Alaska,2018,59907.630000000005,0.2990842470298016,11.418453381500647, 02
19,19,Alaska,2019,63144.34,5.402834330117878,17.438207830882746, 02
20,20,Arizona,2010,148820.94999999998,135.68375249468124,0.0, 04
21,21,Arizona,2011,137570.46000000002,-7.5597488122471805,-7.5597488122471805, 04
22,22,Arizona,2012,163001.9,18.486119767281405,9.5288667354966, 04
23,23,Arizona,2013,185951.99,14.079645697381448,24.9501431082116, 04
24,24,Arizona,2014,193656.54,4.1433006444297815,30.127203192830063, 04
25,25,Arizona,2015,205662.34,6.199532429940136,38.194481354943655, 04
26,26,Arizona,2016,218744.6,6.36103819493643,46.98508509722592, 04
27,27,Arizona,2017,233267.0,6.638975316419238,56.74338861564856, 04
28,28,Arizona,2018,250722.01,7.482845837602414,68.47225474639158, 04
29,29,Arizona,2019,265948.48,6.073048792166258,78.70365697840258, 04
30,30,Arkansas,2010,94877.82,-64.3247368813689,0.0, 05
31,31,Arkansas,2011,93502.92,-1.4491268876118737,-1.4491268876118737, 05
32,32,Arkansas,2012,97052.22,3.795924234237824,2.291789587914228, 05
33,33,Arkansas,2013,100177.74,3.220451835104865,5.586047402859817, 05
34,34,Arkansas,2014,103544.28,3.3605669283415684,9.134337192823372, 05
35,35,Arkansas,2015,106316.81,2.6776273880121604,12.05654809522394, 05
36,36,Arkansas,2016,112474.33,5.7916711383646735,18.54649484990276, 05
37,37,Arkansas,2017,119074.55,5.868201215335089,25.503041701421903, 05
38,38,Arkansas,2018,124853.9,4.853556028555217,31.594402147941423, 05
39,39,Arkansas,2019,130611.97,4.611846325985813,37.663333748604266, 05
40,40,California,2010,364643.65,179.18088211976283,0.0, 06
41,41,California,2011,341790.78,-6.267178929346484,-6.267178929346484, 06
42,42,California,2012,371021.58,8.552249419952162,1.7490857169732754, 06
43,43,California,2013,452425.86,21.94057822728261,24.07342346425065, 06
44,44,California,2014,477829.79,5.615048176070214,31.040205965467926, 06
45,45,California,2015,517227.09,8.245048932591658,41.84453506868966, 06
46,46,California,2016,546494.11,5.658446853586119,49.87073270026778, 06
47,47,California,2017,590828.29,8.112471697087464,62.02895347279463, 06
48,48,California,2018,625712.21,5.9042399611568985,71.59553169237967, 06
49,49,California,2019,625482.18,-0.0367629073436015,71.53244818605783, 06
50,50,Colorado,2010,234039.38,-62.58256630108952,0.0, 08
51,51,Colorado,2011,227443.94,-2.8180898445381297,-2.8180898445381297, 08
52,52,Colorado,2012,239979.33,5.511419649167171,2.538013047205978, 08
53,53,Colorado,2013,261850.41,9.113734920420026,11.883055748994042, 08
54,54,Colorado,2014,282964.91000000003,8.063573396734425,20.90482806782348, 08
55,55,Colorado,2015,316897.18,11.99168829802959,35.40335818698546, 08
56,56,Colorado,2016,344887.69,8.832678788747828,47.36310188481956, 08
57,57,Colorado,2017,371727.33,7.782139165361346,58.83110355188943, 08
58,58,Colorado,2018,398224.71,7.128176451271417,70.15286487256974, 08
59,59,Colorado,2019,409393.18,2.8045647895631465,74.9249122092188, 08
60,60,Connecticut,2010,278533.73,-31.96424767017369,0.0, 09
61,61,Connecticut,2011,262539.16,-5.74241762389065,-5.74241762389065, 09
62,62,Connecticut,2012,255956.9,-2.5071535994858785,-8.105599993221645, 09
63,63,Connecticut,2013,266649.99,4.177691634802572,-4.266535331286447, 09
64,64,Connecticut,2014,264386.3,-0.8489368403876596,-5.079251981438659, 09
65,65,Connecticut,2015,265357.5,0.3673412729782166,-4.730568897346821, 09
66,66,Connecticut,2016,266986.32,0.613820977360735,-4.14578514422651, 09
67,67,Connecticut,2017,276065.26,3.400526289137207,-0.8862373688098613, 09
68,68,Connecticut,2018,280677.12,1.670568763342395,0.769526189880132, 09
69,69,Connecticut,2019,281601.71,0.3294140968811421,1.1014752145099391, 09

我做错了什么?

太感谢了!

I'm new to python and even newer to plotly. I'm trying to utilize plotly.express to map real estate percentage change values in each state (the column 'Change_From_2010') from the "ST_FIPS_Merge.csv" file to the "gz_2010_us_040_00_500k (1).json" file. I plan to do this for every year the data frame has (2011-2019). I'm trying first 2011. The code runs, but there is no display on the map.

My code is as follows:

import csv
import os
import re
import numpy as np
import pandas as pd
import plotly.express as px
import json

IN_PATH = os.path.join("data", "ST_Fips_Merge.csv")
IN_PATH_JSON = os.path.join("data", "gz_2010_us_040_00_500k (1).json")

state_fips = pd.read_csv(IN_PATH,dtype={'FIPS':str})

with open (IN_PATH_JSON) as response:
    states = json.load(response)

twenty_eleven = state_fips[state_fips['YEAR']== 2011]

figure1 = px.choropleth(twenty_eleven, geojson=states, locations = 'FIPS', color = 'Change_From_2010', color_continuous_scale= "Bluered_r", scope = "usa", labels={'Change_From_2010':'Percent Change from 2010 in Housing Price'})

figure1.show()

a minimum working example of the files (text):

percentage change csv file:

0,0,Alabama,2010,100870.33,,0.0, 01
1,1,Alabama,2011,99030.45,-1.824005136098994,-1.824005136098994, 01
2,2,Alabama,2012,102378.52,3.3808490216898024,1.4951770257914276, 01
3,3,Alabama,2013,104504.46,2.076548869821515,3.602773977243867, 01
4,4,Alabama,2014,108094.88,3.4356619803594945,7.1622150933778, 01
5,5,Alabama,2015,111645.37,3.2846051542866794,10.682070733782666, 01
6,6,Alabama,2016,115763.57,3.688643783436807,14.764738055283466, 01
7,7,Alabama,2017,120995.84,4.519789774969785,19.951862951177013, 01
8,8,Alabama,2018,128110.49,5.880078191117977,27.0051262844089, 01
9,9,Alabama,2019,139715.88999999998,9.058899080005055,38.5103925009465, 01
10,10,Alaska,2010,53768.14,-61.51608811281236,0.0, 02
11,11,Alaska,2011,53398.4,-0.6876562960890897,-0.6876562960890897, 02
12,12,Alaska,2012,54604.61,2.258887906753748,1.5556982257522778, 02
13,13,Alaska,2013,53232.060000000005,-2.513615608645492,-0.9970216563191414, 02
14,14,Alaska,2014,53669.59,0.8219294913629049,-0.1832869799848091, 02
15,15,Alaska,2015,55600.7,3.598145616540016,3.4082637041191965, 02
16,16,Alaska,2016,56822.3,2.1970946408948144,5.680241124204777, 02
17,17,Alaska,2017,59728.99,5.11540363554448,11.086212020724528, 02
18,18,Alaska,2018,59907.630000000005,0.2990842470298016,11.418453381500647, 02
19,19,Alaska,2019,63144.34,5.402834330117878,17.438207830882746, 02
20,20,Arizona,2010,148820.94999999998,135.68375249468124,0.0, 04
21,21,Arizona,2011,137570.46000000002,-7.5597488122471805,-7.5597488122471805, 04
22,22,Arizona,2012,163001.9,18.486119767281405,9.5288667354966, 04
23,23,Arizona,2013,185951.99,14.079645697381448,24.9501431082116, 04
24,24,Arizona,2014,193656.54,4.1433006444297815,30.127203192830063, 04
25,25,Arizona,2015,205662.34,6.199532429940136,38.194481354943655, 04
26,26,Arizona,2016,218744.6,6.36103819493643,46.98508509722592, 04
27,27,Arizona,2017,233267.0,6.638975316419238,56.74338861564856, 04
28,28,Arizona,2018,250722.01,7.482845837602414,68.47225474639158, 04
29,29,Arizona,2019,265948.48,6.073048792166258,78.70365697840258, 04
30,30,Arkansas,2010,94877.82,-64.3247368813689,0.0, 05
31,31,Arkansas,2011,93502.92,-1.4491268876118737,-1.4491268876118737, 05
32,32,Arkansas,2012,97052.22,3.795924234237824,2.291789587914228, 05
33,33,Arkansas,2013,100177.74,3.220451835104865,5.586047402859817, 05
34,34,Arkansas,2014,103544.28,3.3605669283415684,9.134337192823372, 05
35,35,Arkansas,2015,106316.81,2.6776273880121604,12.05654809522394, 05
36,36,Arkansas,2016,112474.33,5.7916711383646735,18.54649484990276, 05
37,37,Arkansas,2017,119074.55,5.868201215335089,25.503041701421903, 05
38,38,Arkansas,2018,124853.9,4.853556028555217,31.594402147941423, 05
39,39,Arkansas,2019,130611.97,4.611846325985813,37.663333748604266, 05
40,40,California,2010,364643.65,179.18088211976283,0.0, 06
41,41,California,2011,341790.78,-6.267178929346484,-6.267178929346484, 06
42,42,California,2012,371021.58,8.552249419952162,1.7490857169732754, 06
43,43,California,2013,452425.86,21.94057822728261,24.07342346425065, 06
44,44,California,2014,477829.79,5.615048176070214,31.040205965467926, 06
45,45,California,2015,517227.09,8.245048932591658,41.84453506868966, 06
46,46,California,2016,546494.11,5.658446853586119,49.87073270026778, 06
47,47,California,2017,590828.29,8.112471697087464,62.02895347279463, 06
48,48,California,2018,625712.21,5.9042399611568985,71.59553169237967, 06
49,49,California,2019,625482.18,-0.0367629073436015,71.53244818605783, 06
50,50,Colorado,2010,234039.38,-62.58256630108952,0.0, 08
51,51,Colorado,2011,227443.94,-2.8180898445381297,-2.8180898445381297, 08
52,52,Colorado,2012,239979.33,5.511419649167171,2.538013047205978, 08
53,53,Colorado,2013,261850.41,9.113734920420026,11.883055748994042, 08
54,54,Colorado,2014,282964.91000000003,8.063573396734425,20.90482806782348, 08
55,55,Colorado,2015,316897.18,11.99168829802959,35.40335818698546, 08
56,56,Colorado,2016,344887.69,8.832678788747828,47.36310188481956, 08
57,57,Colorado,2017,371727.33,7.782139165361346,58.83110355188943, 08
58,58,Colorado,2018,398224.71,7.128176451271417,70.15286487256974, 08
59,59,Colorado,2019,409393.18,2.8045647895631465,74.9249122092188, 08
60,60,Connecticut,2010,278533.73,-31.96424767017369,0.0, 09
61,61,Connecticut,2011,262539.16,-5.74241762389065,-5.74241762389065, 09
62,62,Connecticut,2012,255956.9,-2.5071535994858785,-8.105599993221645, 09
63,63,Connecticut,2013,266649.99,4.177691634802572,-4.266535331286447, 09
64,64,Connecticut,2014,264386.3,-0.8489368403876596,-5.079251981438659, 09
65,65,Connecticut,2015,265357.5,0.3673412729782166,-4.730568897346821, 09
66,66,Connecticut,2016,266986.32,0.613820977360735,-4.14578514422651, 09
67,67,Connecticut,2017,276065.26,3.400526289137207,-0.8862373688098613, 09
68,68,Connecticut,2018,280677.12,1.670568763342395,0.769526189880132, 09
69,69,Connecticut,2019,281601.71,0.3294140968811421,1.1014752145099391, 09

What am I doing incorrectly?

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文