使用 Matplotlib 自定义子图方向和大小
Customizing Subplot Orientation and Size with Maplotlib
我正在尝试在 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')
这将给你想要的,只是针对细节进行修改。这是你的想法吗?
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, :])
我不知道你说的"bottom should be twice as wide as four above it."
是什么意思
#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))
我正在尝试在 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')
这将给你想要的,只是针对细节进行修改。这是你的想法吗?
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, :])
我不知道你说的"bottom should be twice as wide as four above it."
是什么意思#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))