gpt4 book ai didi

python - 为什么图形大小(y 轴)在此示例中波动?

转载 作者:行者123 更新时间:2023-12-05 06:48:33 30 4
gpt4 key购买 nike

我有一张世界地图,我在 for 循环中迭代地绘制干旱区域。

为了可重复性,数据在这里:https://data.humdata.org/dataset/global-droughts-events-1980-2001

import pandas as pd
import geopandas as gpd

import matplotlib.pyplot as plt
import seaborn as sns

from IPython.display import clear_output
sns.set_theme(style='whitegrid')

dr_geometry = gpd.read_file('data/dr_events.shp')
world_geometry = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

for y in dr_geometry.year.unique():
clear_output(wait=True)
fig, ax = plt.subplots(1, 1, figsize=(15, 15))
world_geometry.plot(ax=ax)
dr_geometry[dr_geometry.year == y].plot(ax=ax, color='red', edgecolor='black', linewidth=0.1)
plt.show();

这工作正常,除了 y 轴在每次迭代中收缩或扩展一个很小但非常明显的量,导致动画断断续续。我怎样才能消除这种行为?

注意:明确设置 ylim 不会改变这一点。我还尝试将 subplots 实例移到 for 循环之外,但这会导致输出为空。

迭代输出:

enter image description here

最佳答案

ax.set_aspect('equal') 阻止了我这边的移动:

for y in dr_geometry.year.unique():
clear_output(wait=True)
fig, ax = plt.subplots(1, 1, figsize=(15, 15))
world_geometry.plot(ax=ax)
dr_geometry[dr_geometry.year == y].plot(ax=ax, color='red', edgecolor='black', linewidth=0.1)

# set aspect ratio explicitly
ax.set_aspect('equal')

plt.show();

感谢@martinfleis指出转移的原因:

.plot() now automatically determines whether GeoSeries (or GeoDataFrame) is in geographic or projected CRS and calculates aspect for geographic using 1/cos(s_y * pi/180) with s_y as the y coordinate of the mean of y-bounds of GeoSeries. This leads to better representation of the actual shapes than current hard-coded 'equal' aspect.

关于python - 为什么图形大小(y 轴)在此示例中波动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66802616/

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