【问题标题】:Is there any way to give matplotlib pie chart a zorder?有没有办法给matplotlib饼图一个zorder?
【发布时间】:2025-12-15 01:10:01
【问题描述】:

我正在使用 matplotlib 饼图:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.pie.html

我正在生成一个使用这些饼图的网络图。我在饼图中间画了一条线来描绘两个不同的过程。我的问题是,当我在中间画这条线时,它会覆盖在所有饼图的顶部,所以如果它们重叠,这些线将不会正确分层:

我意识到 matplotlib 饼图没有zorder,但是有没有办法让它模拟 zorder?这样我就可以将zorder 用作该线,然后在该线的顶部叠加一个饼图以使其重叠。

【问题讨论】:

    标签: python matplotlib charts pie-chart


    【解决方案1】:

    pie() 返回补丁列表。这些单独的补丁具有zorder 属性,因此您可以遍历它们并调整它们的zorder

    fig,ax = plt.subplots()
    ax.set_aspect('equal')
    p1,t1 = plt.pie([50,50], center=(0,0))
    p2,t2 = plt.pie([1,1,1,1], center=(1.2,0)) # this pie-chart is over the first one
    
    [p.set_zorder(-1) for p in p2] # change the z-order of the patches so that the
                                   # 2nd pie-chart ends up below the first one
    

    【讨论】:

      最近更新 更多