【问题标题】:overlapping two plots in matplotlib在 matplotlib 中重叠两个图
【发布时间】:2023-01-20 16:31:55
【问题描述】:

我有两个使用 matplotlib 生成的图。第一个代表我的背景,第二个代表我想展示的一组要点。有没有办法重叠这两个地块?

背景:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize = (10,10))
grid_duomo = gpd.read_file('/content/Griglia_2m-SS.shp')  
grid_duomo.to_crs(epsg=32632).plot(ax=ax, color='lightgrey')

要点:

fig = plt.figure(figsize=(10, 10))
ids = traj_collection_df_new_app['id'].unique()
for id_ in ids:
  self_id = traj_collection_df_new_app[traj_collection_df_new_app['id'] == id_]
  plt.plot(
            self_id['lon'],
            self_id['lat'],
            # markers= 'o',
            # markersize=12
        )

【问题讨论】:

  • 如果您只是删除对 plt.figure 的调用,并一次运行这两个部分,matplotlib 通常会很好地组合它们。

标签: matplotlib


【解决方案1】:

plt.plot() 将始终采用 matplotlib 找到的最新轴并将其用于绘图。

它实际上与 plt.gca().plot() 相同,其中 plt.gca() 代表“获取当前轴”。

要完全控制使用哪个轴,您应该这样做: (zorder参数用于设置艺术家的“垂直堆叠”,例如zorder=2将绘制在zorder=1之上)

f = plt.figure()            # create a figure
ax = f.add_subplot( ... )   # create an axis in the figure f

ax.plot(..., zorder=0)
grid_duomo.plot(ax=ax, ..., zorder=1)

# you can then continue to add more axes to the same figure using 
# f.add_subplot() or f.add_axes()

(如果不清楚,可以查看quick_start matplotlib 指南?)

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 2018-10-25
    • 2012-12-09
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多