【问题标题】:title and axis gets over each other标题和轴相互超越
【发布时间】:2015-07-30 12:41:11
【问题描述】:

我正在尝试绘制几个图。我想为每个人添加一个标题。但是,在我的代码中,标题和轴相互重叠。有解决办法吗?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

randn = np.random.randn

fig = plt.figure(figsize=(15, 12))
train= df = pd.DataFrame(randn(10, 34))
for i in range(1, train.shape[1]):
    plt.subplot(6, 6, i)
    f = plt.gca()
    f.axes.get_yaxis().set_visible(False)
    f.set_title(train.columns.values[i])

    vals = np.size(train.iloc[:, i].unique())
    if vals < 10:
        bins = vals
    else:
        vals = 10
    plt.hist(train.iloc[:, i], bins=30, color='#3F5D7D')
plt.show()

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    你可以试试:

    plt.tight_layout()
    

    【讨论】:

      【解决方案2】:

      解决办法是:

      plt.tight_layout()
      

      这里有一些很好的文档,它有一个看起来像你的问题的例子。

      http://matplotlib.org/users/tight_layout_guide.html

      【讨论】:

        【解决方案3】:

        另一种解决方案是在图形中手动放置子图,以最大限度地提高布局设计的灵活性。我已经整理了一些代码来展示如何做到这一点。请注意,有很大一部分代码只是为了使 xticks 格式具有视觉吸引力。

        import pandas as pd
        import numpy as np
        import matplotlib.pyplot as plt
        
        plt.close('all')
        
        #------------------------------------------------------------- prepare data ----
        
        randn = np.random.randn
        train= df = pd.DataFrame(randn(10, 34))
        ngraphs = train.shape[1]
        
        #------------------------------------------------------------ create figure ----
        
        fwidth = 15
        fheight = 12
        
        fig = plt.figure(figsize=(fwidth, fheight))
        fig.patch.set_facecolor('white')
        
        left_margin  = 0.5 / fwidth
        right_margin = 0.5 / fwidth
        bottom_margin = 0.5 / fheight
        top_margin = 0.75 / fheight
        
        vinter_margin = 0.75 / fheight
        hinter_margin = 0.5 / fwidth
        
        #-------------------------------------------------------------- create axes ----
        
        ncol = 6
        nrow = int(np.ceil(ngraphs/float(ncol)))
        
        w0 = (1 - (left_margin + right_margin + (ncol-1) * hinter_margin)) / ncol
        h0 = (1 - (bottom_margin + top_margin + (nrow-1) * vinter_margin)) / nrow
        
        AX0 = [0] * ngraphs
        itot = 0
        y0 = 1 - top_margin - h0
        for row in range(nrow):
        
            x0 = left_margin
        
            for col in range(ncol):
        
                AX0[itot] = fig.add_axes([x0, y0, w0, h0], frameon=True)
        
                #-------------------------------------------------------- plot data ----
        
                vals = np.size(train.iloc[:, itot].unique())
                if vals < 10:
                    bins = vals
                else:
                    vals = 10
        
                AX0[itot].hist(train.iloc[:, itot], bins=30, color='#3F5D7D')
        
                #--------------------------------------------------------- set axis ----
        
                AX0[itot].axes.get_yaxis().set_visible(False)
                AX0[itot].set_title(train.columns.values[itot])
        
                #---- major ticks ----
        
                AX0[itot].tick_params(top='off', labeltop='off')
                AX0[itot].tick_params(axis='x', direction='out', labelsize=8) 
        
                trainmax = np.ceil(np.max(train.iloc[:, itot])/0.5)*0.5
                trainmin = np.floor(np.min(train.iloc[:, itot])/0.5)*0.5
        
                AX0[itot].set_xticks([trainmin,0, trainmax])
        
                #---- minor ticks ----
        
                AX0[itot].set_xticks(np.arange(trainmin, trainmax, 0.5), minor=True)
                AX0[itot].tick_params(axis='x', which='minor', direction='out',
                                      top='off', length=3)
        
                #---- axis limits ----
        
                AX0[itot].axis(xmin=trainmin, xmax=trainmax)               
        
                #---------------------------------------------------------- iterate ----
        
                x0 = x0 + w0 + hinter_margin                                   
                itot += 1
        
                if itot == ngraphs:
                    break
        
            y0 = y0 - h0 - vinter_margin
        
        plt.show(block=False)
        
        fig.savefig('subplot_layout.png')
        

        结果:

        【讨论】:

          猜你喜欢
          • 2011-06-01
          • 1970-01-01
          • 2011-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-05
          • 1970-01-01
          • 2016-02-26
          相关资源
          最近更新 更多