我试图在 matplotlib 中指定数字限制,但它们没有被强制执行。我在我的子图中设置了限制,我看到这些数字明显处于不同的程度,所以我打印了指定的和实际的限制以确认它们是不同的。
无论数据范围如何,如何强制数字限制?
import matplotlib.pyplot as plt
import contextily as ctx
def mymap(df, xlimits, ylimits):
mycolorsdict = {'A':'green', 'B':'orange', 'C':'blue'}
fig, ax = plt.subplots(figsize=(10,10))
for key, group in df.groupby('category'):
group.plot(ax=ax, kind='scatter',
x='long', y='lat',
color=mycolorsdict[key]
)
ctx.add_basemap(ax, crs='epsg:4326',
source=ctx.providers.Esri.WorldShadedRelief
)
ax.set_xlim = xlimits
ax.set_ylim = ylimits
ax.set_aspect('equal')
ax.ticklabel_format(useOffset=False, style='plain')
ax.tick_params(axis='both', labelsize=8)
print(ylimits, ax.get_ylim())
print(xlimits, ax.get_xlim())
当我为几个数据帧运行这个函数时,地图不在相同的范围内,也不在我指定的范围内。
xl = [-116.423639, -116.120922]
yl = [34.288848, 34.779539]
for cat in df.category.unique():
df2 = df[df.category == cat].copy()
mymap(df2, xl, yl)
它返回的限制与我指定的不同。我指定的在左边,实际在右边。
[34.288848, 34.779539] (34.26499695, 34.80380005000001)
[-116.423639, -116.120922] (-116.43791284999999, -116.10754215000003)
[34.288848, 34.779539] (34.327880050000005, 34.71235095)
[-116.423639, -116.120922] (-116.38095349999999, -116.1129605)