pandas:使用add_subplot和subplots()

时间:2017-06-15 14:25:50

标签: python matplotlib plot

我想用一个具有多个功能的df创建一个数字。我能够单独构建这些功能,但是将它们组合在一起会有问题。我认为主要原因是我使用subplots()和add_subplot()并且不知道如何组合它们。 这些是功能:

  1. 带有直方图的四张图
  2. 所有图表中的x轴断裂
  3. 此功能改编自here

    import numpy as np
    import matplotlib.pyplot as plt
    def breakX(ax1,ax2):
     ax=ax1
     ax2=ax2
     ax.set_ylim(.78, 1.)
     ax2.set_ylim(0, .22)
     ax.spines['bottom'].set_visible(False)
     ax2.spines['top'].set_visible(False)
     ax.xaxis.tick_top()
     ax.tick_params(labeltop='off')
     ax2.xaxis.tick_bottom()
     d = .015
     kwargs = dict(transform=ax.transAxes, color='black', clip_on=False )
     ax.plot((-d, +d), (-d, +d), **kwargs)
     ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
     kwargs.update(transform=ax2.transAxes)
     ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
     ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
    
     # breakX is used in this function to create a figure with  three histograms: 
    
    def figure2():
     fig=plt.figure()
     pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
     pts[[3, 14]] += .8
     ax=fig.add_subplot(221)
     ax2=fig.add_subplot(221)
     f, (ax, ax2) = plt.subplots(2, 1, sharex=True)
     ax.plot(pts)
     ax2.plot(pts)
     breakX(ax,ax2)
     ax3=fig.add_subplot(222)
     ax4=fig.add_subplot(222)
     f, (ax3, ax4) = plt.subplots(2, 1, sharex=True)
     ax3.plot(pts)
     ax4.plot(pts)
     breakX(ax3,ax4)
     ax5=fig.add_subplot(223)
     ax6=fig.add_subplot(223)
     f, (ax5, ax6) = plt.subplots(2, 1, sharex=True)
     ax5.plot(pts)
     ax6.plot(pts)
     breakX(ax5,ax6)
     plt.show()    
    

    我的问题是我得到四个数字而不是一个,表明add_subplot()和subplots()不能一起工作。我想要一个带有三个图形的图形:

    enter image description here

1 个答案:

答案 0 :(得分:0)

原则上你想要的是一个子图网格,有4次2个图。这可以使用plt.subplots(nrows=4, ncols=2)创建。

import numpy as np
import matplotlib.pyplot as plt

def breakX(ax1,ax2):
    ax=ax1
    ax2=ax2
    ax.set_ylim(.78, 1.)
    ax2.set_ylim(0, .22)
    ax.spines['bottom'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax.xaxis.tick_top()
    ax.tick_params(labeltop='off')
    ax2.xaxis.tick_bottom()
    d = .015
    kwargs = dict(transform=ax.transAxes, color='black', clip_on=False )
    ax.plot((-d, +d), (-d, +d), **kwargs)
    ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
    kwargs.update(transform=ax2.transAxes)
    ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
    ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)


def figure2():
    fig, ((ax, ax3), (ax2, ax4), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=4, ncols=2)
    pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
    pts[[3, 14]] += .8

    ax.plot(pts)
    ax2.plot(pts)
    breakX(ax,ax2)

    ax3.plot(pts)
    ax4.plot(pts)
    breakX(ax3,ax4)

    ax5.plot(pts)
    ax6.plot(pts)
    breakX(ax5,ax6)

    ax_.axis("off")
    ax__.axis("off")

    plt.show() 

figure2()

enter image description here

现在可能看起来有点挤压,所以为了增加空间,你可以在网格中引入另一排空轴,并将其作为其他行的五分之一。

def figure2():
    fig, ((ax, ax3), (ax2, ax4), (empty1, empty2), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=5, ncols=2, gridspec_kw={"height_ratios" : [5,5,1,5,5]})
    pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
    pts[[3, 14]] += .8

    ax.plot(pts)
    ax2.plot(pts)
    breakX(ax,ax2)

    ax3.plot(pts)
    ax4.plot(pts)
    breakX(ax3,ax4)

    ax5.plot(pts)
    ax6.plot(pts)
    breakX(ax5,ax6)

    for axq in (ax_, ax__, empty1, empty2):
        axq.axis("off")

    plt.show() 

enter image description here

对于更复杂的网格设计,您可以查看GridSpec page