为什么图形大小(y 轴)在此示例中波动?
Why is the figure size (y-axis) fluctuating in this example?
我有一张世界地图,我在 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 循环之外,但这会导致输出为空。
一次迭代输出:
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指出转移的原因:
- geopandas#1121 - Consider scaling y-axis for unprojected map plots
- geopandas#1290 - ENH: scaling y-axis for plots in geographic CRS
.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.
我有一张世界地图,我在 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 循环之外,但这会导致输出为空。
一次迭代输出:
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指出转移的原因:
- geopandas#1121 - Consider scaling y-axis for unprojected map plots
- geopandas#1290 - ENH: scaling y-axis for plots in geographic CRS
.plot()
now automatically determines whether GeoSeries (or GeoDataFrame) is in geographic or projected CRS and calculates aspect for geographic using1/cos(s_y * pi/180)
withs_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.