我试图在Python中绘制5个不同大小和方向的子图。我已经看到使用gridspec改变列宽,plt.figure(figsize = [])等,但每个似乎都略微偏离了我想要的(例如没有统一的列宽,所有的一个图,所以不能使用plt.figure())。
我目前所拥有的是一个子图(3,2,2),前5个图填充使得它看起来像一个3x2网格而没有填充右下角。我想要的是一个带有单个图的2x2网格在它下面。下面的图也应该比它上面的四个更大(可能是两倍宽)。
This is what I have versus what I would like.
这是我的代码(对不起行号):
plot3 = plt.figure(1)
plt.subplot(321)
#gs = gridspec.GridSpec(1,2,3,4,5,6,width_ratios=[1,1,1,1,2,0])
plt.scatter(0.001*posEncUm[:,0],err[:,0], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,0],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (X), mm', fontsize=7)
plt.ylabel('Laser Error (X), um', fontsize=7)
plt.subplots_adjust(hspace = 1.0, wspace = 0.5)
plt.grid()
plt.subplot(322)
plt.scatter(0.001*posEncUm[:,1],err[:,0], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,1],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (Y), mm', fontsize=7)
plt.ylabel('Laser Error (X), um', fontsize=7)
plt.grid()
plt.subplot(323)
plt.scatter(0.001*posEncUm[:,0],err[:,1], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,0],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (X), mm', fontsize=7)
plt.ylabel('Laser Error (Y), um', fontsize=7)
plt.grid()
plt.subplot(324)
plt.scatter(0.001*posEncUm[:,1],err[:,1], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,1],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (Y), mm', fontsize=7)
plt.ylabel('Laser Error (Y), um', fontsize=7)
plt.grid()
plt.subplot(325)
plt.quiver(0.001*X,0.001*Y,errX,errY)
plt.grid()
plt.xlabel('Encoder Pos (X), mm')
plt.ylabel('Encoder Pos (Y), mm')
plt.gca().set_aspect('equal', adjustable = 'box')
答案 0 :(得分:1)
这将提供您想要的内容,只需修改具体内容即可。这是你在想什么?
import pylab as pl
fig = pl.figure(figsize=(3.25, 4.5))
gs = pl.GridSpec(3, 2)
gs.update(left=0.08, right=0.925,
top=0.95, bottom=0.05,
hspace=0.3, wspace=0.1)
# create primary axes
ax0 = pl.subplot(gs[0, 0])
ax1 = pl.subplot(gs[0, 1])
ax2 = pl.subplot(gs[1, 0])
ax3 = pl.subplot(gs[1, 1])
ax4 = pl.subplot(gs[2, :])
我不知道你的意思"底部应该是它上面四个的两倍宽。"
答案 1 :(得分:0)
#https://python-graph-gallery.com/125-small-multiples-for-line-chart/
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
# Initialize the figure
plt.style.use('seaborn-darkgrid')
# create a color palette
palette = plt.get_cmap('Set1')
plt.figure(figsize=(10,10))
plt.suptitle("PLOT TITLE",fontsize=20)
gridspec.GridSpec(3,3)
plt.subplots_adjust(hspace=0.4)
# multiple line plot
num=0
for column in df.drop('ODD COLUMN NAME', axis=1):
num+=1
# Find the right spot on the plot
if num==7: # adjustment to fit ODD COLUMN
plt.subplot2grid((3,3),(2,0),colspan=3)
else:
plt.subplot(3,3, num)
# plot every groups, but discreet
for v in df.drop('ODD COLUMN', axis=1):
plt.plot(df['ODD COLUMN'], df[v], marker='', color='grey', linewidth=0.6, alpha=0.3)
# Plot the lineplot
plt.plot(df['ODD COLUMN'], df[column], marker='', color=palette(num), linewidth=2.4, alpha=0.9, label=column)
# Same limits for everybody!
plt.xlim(10,100)
plt.ylim(1,100)
# Not ticks everywhere
if num in range(4) :
plt.tick_params(labelbottom='off')
if num not in [1,4,7] :
plt.tick_params(labelleft='off')
# Add title
plt.title(column, loc='left', fontsize=12, fontweight=0, color=palette(num))