Removing frame while keeping axes in pyplot subplots

后端 未结 3 1506
温柔的废话
温柔的废话 2021-02-01 17:39

I am creating a figure with 3 subplots, and was wondering if there is any way of removing the frame around them, while keeping the axes in place?

3条回答
  •  一个人的身影
    2021-02-01 17:43

    If you want to remove the axis spines, but not the other information (ticks, labels, etc.), you can do that like so:

    fig, ax = plt.subplots(7,1, sharex=True)
    
    t = np.arange(0, 1, 0.01)
    
    for i, a in enumerate(ax):
        a.plot(t, np.sin((i + 1) * 2 * np.pi * t))
        a.spines["top"].set_visible(False)
        a.spines["right"].set_visible(False)
        a.spines["bottom"].set_visible(False)
    

    or, more easily, using seaborn:

    fig, ax = plt.subplots(7,1, sharex=True)
    
    t = np.arange(0, 1, 0.01)
    
    for i, a in enumerate(ax):
        a.plot(t, np.sin((i + 1) * 2 * np.pi * t))
    
    seaborn.despine(left=True, bottom=True, right=True)
    

    Both approaches will give you:

    enter image description here

提交回复
热议问题