强制绘图具有特殊大小或增加子图之间的距离

时间:2013-05-03 21:05:00

标签: python matplotlib plot

我正在尝试通过matplotlib生成一个图,其中三个图形连续,轴有名称。不幸的是,维度是标签进入其他图表的方式。我怎么能避免这个?

注意:我刚刚更改了代码,使其成为模板,以便显示我正在讨论的问题!

from matplotlib import pyplot as plt
from bisect import bisect_left,bisect_right
import numpy as np
global ranOnce
ranOnce=False
def threeLines(x):
    """Draws a function which is a combination of two lines intersecting in
    a point one with a large slope and one with a small slope.
    """
    start=0
    mid=5
    end=20
    global ranOnce,slopes,intervals,intercepts;
    if(not ranOnce):
        slopes=np.array([5,0.2,1]);
        intervals=[start,mid,end]
        intercepts=[start,(mid-start)*slopes[0]+start,(end-mid)*slopes[1]+(mid-start)*slopes[0]+start]
#        plt.plot(X,Y)
#        plt.axis('equal')
#        plt.show()
        ranOnce=True;
    place=bisect_left(intervals,x)
    if place==0:
        y=(x-intervals[place])*slopes[place]+intercepts[place];
    else:    
        y=(x-intervals[place-1])*slopes[place-1]+intercepts[place-1];
    return y;
def threeLinesDrawer(minimum,maximum):
    t=np.arange(minimum,maximum,1)
    fig=plt.subplot(131)
    markerSize=400;
    fig.scatter([minimum,maximum],[threeLines(minimum),threeLines(maximum)],marker='+',s=markerSize)
#    fig.set_xlim(minimum-1,maximum+1)
#    fig.set_ylim(nLines(minimum)-1,nLines(maximum)+1) 

    y=np.zeros(len(t));
    for i in range(len(t)):
        y[i]=int(threeLines(t[i]))
    fig.scatter(t,y)
    fig.grid(True)
    fig.set_xlabel('Y')
    fig.set_ylabel('X')
def threeLinesDrawer2(minimum,maximum):
    t=np.arange(minimum,maximum,1)
    fig=plt.subplot(132)
    markerSize=400;
    fig.scatter([minimum,maximum],[threeLines(minimum),threeLines(maximum)],marker='+',s=markerSize)
#    fig.set_xlim(minimum-1,maximum+1)
#    fig.set_ylim(nLines(minimum)-1,nLines(maximum)+1) 

    y=np.zeros(len(t));
    for i in range(len(t)):
        y[i]=int(threeLines(t[i]))
    fig.scatter(t,y)
    fig.grid(True)
    fig.set_xlabel('Y')
    fig.set_ylabel('X')
def threeLinesDrawer3(minimum,maximum):
    t=np.arange(minimum,maximum,1)
    fig=plt.subplot(133)
    markerSize=400;
    fig.scatter([minimum,maximum],[threeLines(minimum),threeLines(maximum)],marker='+',s=markerSize)
#    fig.set_xlim(minimum-1,maximum+1)
#    fig.set_ylim(nLines(minimum)-1,nLines(maximum)+1) 

    y=np.zeros(len(t));
    for i in range(len(t)):
        y[i]=int(threeLines(t[i]))
    fig.scatter(t,y)
    fig.grid(True)
    fig.set_xlabel('Y')
    fig.set_ylabel('X')
threeLinesDrawer(0,20)
threeLinesDrawer2(0,20)
threeLinesDrawer3(0,20)
plt.show()

1 个答案:

答案 0 :(得分:2)

您可以调整绘图之间的填充。在脚本的最后一行之前,添加类似

的内容
plt.tight_layout(w_pad=0.4)

你可以玩0.4,根据你的需要进行调整。这里,w用于宽度。在其他情况下,您可能还希望使用h_pad调整高度填充,或者同时使用pad调整两者。

这是一个相对较新的命令(v1.1中的新功能),因此如果你没有它,你可能需要升级matplotlib。