gpt4 book ai didi

pandas - 为 matplotlib 绘图重新排列 geopandas 中的多边形

转载 作者:行者123 更新时间:2023-12-04 14:49:33 25 4
gpt4 key购买 nike

我正在从事一个项目,在该项目中我使用形状文件制作美国的等值线图。为此,我下载了标准形状文件 here来自美国人口普查局。经过一点点清理(通过更改图的轴限制删除了一些无关的岛屿区域),我能够使连续状态整齐地适应 matplotlib 图形的边界。如需引用,请参阅 编辑 4 以下。
编辑 1:我正在使用 cb_2018_us_state_500k.zip
[3.2 MB]
形状文件。
现在唯一的问题是,通过设置轴限制,我现在无法再查看阿拉斯加和夏威夷(因为这些显然通过限制轴限制而被剪掉了)。我现在想将这两个多边形重新添加到我的 map 中,但现在向绘图图的下部添加(大多数其他此类 map 给出的处理),尽管其地理不准确。
更具体地说,我有兴趣选择代表阿拉斯加和夏威夷的多边形形状并将它们移动到我的图形的左下方。这是可能的吗?
我可以使用以下方法创建 bool 掩码:

mask = df['STUSPS'] == 'AK'
自行获取此状态的多边形;但是,我现在对如何移动/重新定位选择后有点卡住了。
编辑 2:由于每个状态都由 geometry 表示dtype,我可以对多边形中的每个点应用转换吗?对于阿拉斯加,几何列显示:
27    MULTIPOLYGON (((179.48246 51.98283, 179.48656 ...
Name: geometry, dtype: geometry
会说将这个列表中的每个数字乘以相同的常数来完成这个吗?
我想把阿拉斯加放在左下角 (-125, 27) 附近的某个地方。地区和旁边的夏威夷 (-112, 27) .
编辑 3:
我的代码:
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])
我现在拥有的示例图:
enter image description here
任何对资源、解释或示例的见解或链接将不胜感激。
注:从技术上讲,我可以通过简单地使用已经在此位置包含阿拉斯加和夏威夷的形状文件来避免这种情况,例如内政部提供的形状文件;但是,如果我想说添加关岛或波多黎各,这将不起作用。
预期结果:
编辑 4:我想做的是类似于 this question 的事情,但在 python 而不是 R 中。
enter image description here
图片来源: Murphy

最佳答案

您可以通过 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([])
这是结果,正如您所看到的,颜色与每个州的土地质量成正比:
enter image description here

关于pandas - 为 matplotlib 绘图重新排列 geopandas 中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69278742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com