Reputation: 11637
I am trying to generate a plot by matplotlib that three graphs are in a row and axes have names. Unfortunately the dimension is in a way that labels run into the other graphs. How can I avoid this?
Note: I just changed my code to make it a template in order to show the problem I am discussing of!
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()
Upvotes: 0
Views: 158
Reputation: 20196
You can adjust the padding between plots. Just before the last line of your script, add something like
plt.tight_layout(w_pad=0.4)
You can play with 0.4, to adjust it as you see fit. Here, the w
is for width. In other cases, you might also want to adjust the height padding with h_pad
, or both at the same time with pad
.
This is a relatively new command (new in v1.1), so you may have to upgrade matplotlib if you don't have it.
Upvotes: 2