简体   繁体   English

Plotly:如何为 Scattergeo 设置手动边界框?

[英]Plotly: How to set a manual bounding box for a Scattergeo?

I'm trying to calculate the mean center of population in the US for a variety of variables, so my first step was to compute them for the last 10 years of population by county and see if they match up against the Census maps of this figure (at least roughly, since the Census is using a more granular geographic resolution.) As you'd expect, it's a sort of extremely delayed Manifest Destiny:我正在尝试计算各种变量的美国人口平均中心,所以我的第一步是按县计算过去 10 年的人口中心,看看它们是否与该图的人口普查地图相匹配(至少粗略地说,因为人口普查使用的是更精细的地理分辨率。)正如你所料,这是一种极其延迟的清单命运:

在此处输入图像描述

(The formula for calculating these points based on many population centers is on page 2 here , if anyone is curious.) (如果有人好奇,根据许多人口中心计算这些点的公式在第 2 页。)

I'm migrating from R/RStudio to Jupyter, using pandas, plotly, numpy, etc. I'm normally a JavaScript engineer, but I'm comfortable with Python and like it quite a bit more than R, I was able to calculate what appear to be similar points, using the airport map example in this fantastic tutorial on plotly.graph_objects.Scattergeo , but I'm stuck at a national resolution so I can't tell how well I'm doing: I'm migrating from R/RStudio to Jupyter, using pandas, plotly, numpy, etc. I'm normally a JavaScript engineer, but I'm comfortable with Python and like it quite a bit more than R, I was able to calculate似乎是相似的点,在 plotly.graph_objects.Scattergeo 的这个精彩plotly.graph_objects.Scattergeo中使用机场 map 示例,但我被困在国家决议中,所以我不知道我做得如何:

在此处输入图像描述

I can manually zoom, of course, but I'd really like to understand how to focus on only Missouri by default.当然,我可以手动缩放,但我真的很想了解默认情况下如何只关注密苏里州。 I found this useful CodePen , but since there are so many different excellent mapping tools in Python I'm not sure how to port over the JS.我发现这个有用的 CodePen ,但由于 Python 中有很多不同的优秀映射工具,我不确定如何通过 JS 移植。

My current map, based on the above tutorial, sets up the basic plot and then modifies the layout's scope in a second line -- I'm not certain if this is standard for plotly , or just a convenience. My current map, based on the above tutorial, sets up the basic plot and then modifies the layout's scope in a second line -- I'm not certain if this is standard for plotly , or just a convenience.

As much as I admire Python, going straight to the docs for update_layout is a bit of a rabbit hole:) I don't mind dictating the lat/lng bounds of the viewport, though it would neat to just say "Missouri."尽管我很佩服 Python,但直接访问update_layout的文档有点像兔子洞:) 我不介意指定视口的纬度/经度范围,尽管说“密苏里州”会很好。 I realize this is simple -- just a bit of a learning curve.我意识到这很简单——只是一个学习曲线。

import pandas as pd
import plotly.graph_objects as go

meanCenters = pd.read_json('{"year": {"0": 2010, "1": 2011, "2": 2012, "3": 2013, "4": 2014, "5": 2015, "6": 2016, "7": 2017, "8": 2018, "9": 2019}, "lat": {"0": 37.52908501121699, "1": 37.51719645600817, "2": 37.50264465332917, "3": 37.489564543614605, "4": 37.47311004581999, "5": 37.45358003505096, "6": 37.43623735876329, "7": 37.423475510154134, "8": 37.4121869670822, "9": 37.40021167050047}, "lng": {"0": -92.1522086893934, "1": -92.17800419530532, "2": -92.20484620692078, "3": -92.23312370193283, "4": -92.26617930383293, "5": -92.30498885605378, "6": -92.3395596061908, "7": -92.36398254029461, "8": -92.38362683728195, "9": -92.40337680455285}}')

fig = go.Figure(data=go.Scattergeo(
    lon = meanCenters['lng'],
    lat = meanCenters['lat'],
    mode = 'markers'
))
fig.update_layout(
    geo_scope='usa',
    height=600
)
fig.show()

Data is included above -- thx, @vestland!数据包含在上面——谢谢,@vestland! -- but reprinted here for readability -- 但为了便于阅读,在此重印

year    lat lng
2010    37.52908501121699   -92.1522086893934
2011    37.51719645600817   -92.17800419530532
2012    37.50264465332917   -92.20484620692078
2013    37.489564543614605  -92.23312370193283
2014    37.47311004581999   -92.26617930383293
2015    37.45358003505096   -92.30498885605378
2016    37.43623735876329   -92.3395596061908
2017    37.423475510154134  -92.36398254029461
2018    37.4121869670822    -92.38362683728195
2019    37.40021167050047   -92.40337680455285

I haven't found a way to specify how to show a geographical area like Missouri directly, but you can specify how to focus on your data using:我还没有找到一种方法来指定如何直接显示像Missouri这样的地理区域,但是您可以使用以下方法指定如何专注于您的数据:

fig.update_geos(fitbounds='locations')

Which for your data sample will give you this:对于您的数据样本,哪个将为您提供:

在此处输入图像描述

So this simple approach would make much more sense for larger datasets that would include a bigger piece of the US map.因此,这种简单的方法对于更大的数据集更有意义,其中包括更大的美国 map。 But a very nice thing here is that you can build your figure using px.Line() and then add to that using, for example:但是这里非常好的事情是您可以使用px.Line()构建您的图形,然后使用例如:

fig.add_traces(go.Scattergeo(lat=[41,36], lon=[-96,-89]

And those coordinates just so happens to make the outline of the map include Missouri , so you'll get this (the markers are set to 100% transparent):这些坐标恰好使 map 的轮廓包括Missouri ,所以你会得到这个(标记设置为 100% 透明):

在此处输入图像描述

And if I'm understanding you correctly, this is more or less what you're looking for?如果我对您的理解正确,这或多或少就是您要找的东西? Perhaps the best take-away from this suggestion is that you can build a figure using using px.express , add data using fig.add_traces(go.Scattergeo()) and then edit the geographical features using fig.update_geos() and not only fig.update_layout() .也许从这个建议中最好的收获是,您可以使用px.express构建图形,使用fig.add_traces(go.Scattergeo())添加数据,然后使用fig.update_geos()编辑地理特征,而不仅仅是fig.update_layout()

Here's a complete code snippet to reproduce the second figure:这是重现第二个图的完整代码片段:

import plotly.express as px
import plotly.graph_objects as go
meanCenters = pd.read_json('{"year": {"0": 2010, "1": 2011, "2": 2012, "3": 2013, "4": 2014, "5": 2015, "6": 2016, "7": 2017, "8": 2018, "9": 2019}, "lat": {"0": 37.52908501121699, "1": 37.51719645600817, "2": 37.50264465332917, "3": 37.489564543614605, "4": 37.47311004581999, "5": 37.45358003505096, "6": 37.43623735876329, "7": 37.423475510154134, "8": 37.4121869670822, "9": 37.40021167050047}, "lng": {"0": -92.1522086893934, "1": -92.17800419530532, "2": -92.20484620692078, "3": -92.23312370193283, "4": -92.26617930383293, "5": -92.30498885605378, "6": -92.3395596061908, "7": -92.36398254029461, "8": -92.38362683728195, "9": -92.40337680455285}}')

fig = px.line_geo(lat=meanCenters['lat'], lon=meanCenters['lng'])

fig.add_traces(go.Scattergeo(lat=[41,36], lon=[-96,-89],
                             mode = 'markers',
                             marker = dict(size = 2,color = 'rgba(0, 0, 0, 0)'),
                             name='Missouri'))

fig.update_geos(visible=True, resolution=50, scope="north america",
                fitbounds='locations',
                showcountries=True, countrycolor="Black",
                showsubunits=True, subunitcolor="grey")

fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM