- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事一个项目,在该项目中我使用形状文件制作美国的等值线图。为此,我下载了标准形状文件 here来自美国人口普查局。经过一点点清理(通过更改图的轴限制删除了一些无关的岛屿区域),我能够使连续状态整齐地适应 matplotlib 图形的边界。如需引用,请参阅 编辑 4 以下。
编辑 1:我正在使用 cb_2018_us_state_500k.zip
[3.2 MB] 形状文件。
现在唯一的问题是,通过设置轴限制,我现在无法再查看阿拉斯加和夏威夷(因为这些显然通过限制轴限制而被剪掉了)。我现在想将这两个多边形重新添加到我的 map 中,但现在向绘图图的下部添加(大多数其他此类 map 给出的处理),尽管其地理不准确。
更具体地说,我有兴趣选择代表阿拉斯加和夏威夷的多边形形状并将它们移动到我的图形的左下方。这是可能的吗?
我可以使用以下方法创建 bool 掩码:
mask = df['STUSPS'] == 'AK'
自行获取此状态的多边形;但是,我现在对如何移动/重新定位选择后有点卡住了。
geometry
表示dtype,我可以对多边形中的每个点应用转换吗?对于阿拉斯加,几何列显示:
27 MULTIPOLYGON (((179.48246 51.98283, 179.48656 ...
Name: geometry, dtype: geometry
会说将这个列表中的每个数字乘以相同的常数来完成这个吗?
(-125, 27)
附近的某个地方。地区和旁边的夏威夷
(-112, 27)
.
import geopandas as gpd
import matplotlib.pyplot as plt
# import the United States shape file
df = gpd.read_file('Desktop/cb_2018_us_state_500k/cb_2018_us_state_500k.shp')
# exclude the values that we would not like to display
exclude = df[~df['STUSPS'].isin(['PR', 'AS', 'VI', 'MP', 'GU','AK'])]
# create a plot figure
fig, ax = plt.subplots(1, figsize=(20, 10))
exclude.plot(column="NAME", ax=ax)
_ = ax.set_xlim([-130, -64])
_ = ax.set_ylim([22, 53])
我现在拥有的示例图:
最佳答案
您可以通过 ax.inset_axes()
轻松实现最终目标,然后在插图上指定不同的经度/纬度界限。
这是一个简单的方法:
# import the United States shape file
df = gpd.read_file('cb_2018_us_state_500k/cb_2018_us_state_500k.shp')
# set state code as index, exclude states that we will never display
df = df.set_index('STUSPS').drop(index=['PR', 'VI', 'MP', 'GU', 'AS'])
# create an axis with 2 insets − this defines the inset sizes
fig, continental_ax = plt.subplots(figsize=(20, 10))
alaska_ax = continental_ax.inset_axes([.08, .01, .20, .28])
hawaii_ax = continental_ax.inset_axes([.28, .01, .15, .19])
# Set bounds to fit desired areas in each plot
continental_ax.set_xlim(-130, -64)
continental_ax.set_ylim(22, 53)
alaska_ax.set_ylim(51, 72)
alaska_ax.set_xlim(-180, -127)
hawaii_ax.set_ylim(18.8, 22.5)
hawaii_ax.set_xlim(-160, -154.6)
# Plot the data per area - requires passing the same choropleth parameters to each call
# because different data is used in each call, so automatically setting bounds won’t work
vmin, vmax = df['ALAND'].agg(['min', 'max'])
df.drop(index=['HI', 'AK']).plot(column="ALAND", ax=continental_ax, vmin=vmin, vmax=vmax)
df.loc[['AK']].plot(column="ALAND", ax=alaska_ax, vmin=vmin, vmax=vmax)
df.loc[['HI']].plot(column="ALAND", ax=hawaii_ax, vmin=vmin, vmax=vmax)
# remove ticks
for ax in [continental_ax, alaska_ax, hawaii_ax]:
ax.set_yticks([])
ax.set_xticks([])
这是结果,正如您所看到的,颜色与每个州的土地质量成正比:
关于pandas - 为 matplotlib 绘图重新排列 geopandas 中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69278742/
我使用来自 OpenStreetMap (link) 的形状文件创建了一个简单的 geopandas map 。 import geopandas as gpd map_gdf = gpd.read_
我正在尝试更改 geopandas 数据框的 CRS。目前的 CRS 是: Name: unknown Axis Info [ellipsoidal]: - lon[east]: Longitude
是否有在 Windows PC 上安装 geopandas 的教程(逐步)?或者哪种方式效果更好? 我已经使用 anaconda 尝试了文档中的解决方案,但最终收到错误(甚至按照建议创建环境)。我什至
我正在尝试按照描述设置 geopandas 对象的 crs here . 示例文件可以从here下载。 import geopandas as gdp df = pd.read_pickle('myS
我目前在 Python 中结合使用 Pandas 和 ArcPy 来完成我的 GIS 工作。我最近听说了 Geopandas 并且有兴趣学习使用它作为 ArcPy 的替代品来进行基本的地理处理操作(空
我读了 .csv文件作为数据框,如下所示: import pandas as pd df = pd.read_csv('myFile.csv') df.head() BoroName g
将 geojson 文件加载到 GeoPandas 后,我得到了预期的名为“几何”的列/特征,其中包含多边形和多边形。 如何将此对象作为 json 或 Python 字典再次取出? 在当前形式下,它是
我正在尝试使用 ANACONDA spyder 运行一个简单的 geopandas 代码。但是,我遇到了错误。 我已经包含了代码和错误如下: —— 这是代码: import geopandas as
我有一个点,我想知道哪个多边形最接近该点。我有点和多边形的地理数据。 最佳答案 您可以使用 distance查找到每个多边形的距离并对它们进行排序以检索最近的。 示例 >>> from shapely
如果我有下面的图,如何将颜色图/图例转换为对数刻度? import geopandas as gpd import matplotlib.pyplot as plt world = gpd.read_
我有一个 geopandas 数据框 geometry idx 0 POLYGON ((-74.2555
我在 python 中使用 GeoPandas 并且有一个有效的多边形 GeoDataframe。 0 POLYGON Z ((68.70999999999999 623.1 0, 3
假设我有以下包含 3 个多边形对象的地理数据框。 import geopandas as gpd from shapely.geometry import Polygon p1=Polygon([(0
我有一个 geopandas 数据框,由 id 和由 2D 点填充的几何列组成。我想连接每个唯一 id 的点来创建一个多边形,以便我的新数据框将多边形作为其几何形状。我的代码目前看起来像这样: pol
我想使用geopandas.rotate()函数根据列中的属性旋转geodataframe中的所有要素。如果我只选择一项功能,我就会成功。然而,对于多行,我相信我需要创建一个 for 循环。 new_
我正在尝试在我的 Mac OS X 上安装 geopandas。 我做了什么: • 下载并安装了适用于 Python 2.7 的 Anaconda here .图形安装程序。 • python --v
我一直收到这个错误,并且已经阅读了其他人的在线访问, ImportError: geopandas, pyshp and shapely must be installed for this figu
您好,我正在尝试将 X 和 Y 坐标列表转换为直线。我想通过 groupby ID 和时间来映射这些数据。只要我 grouby 一列,我的代码就可以成功执行,但是两列是我遇到错误的地方。我引用了这个q
我在使用 read_file 函数时遇到了一些关于 geopandas 和 fiona 的问题。这是我的回溯: df = gpd.read_file('/Users/danqing0703/githu
我有一个在 mapshaper 上看起来像这样的 shapefile: 但是当我尝试用下面的代码在 pandas 中绘制它时 police = gpd.read_file('srilanka_poli
我是一名优秀的程序员,十分优秀!