繁体   English   中英

在matplotlib中对齐一行图

[英]Aligning a row of plots in matplotlib

我使用以下代码获得5个子图,

pylab.subplot(231)
pylab.scatter(x_a, y_a, color='darkorange')
make_diagonal(x_a, y_a)

pylab.subplot(232)
pylab.scatter(x_b, y_b, color='g')
make_diagonal(x_b, y_b)

pylab.subplot(233)
pylab.scatter(x_c, y_c, color='blue')
make_diagonal(x_c, y_c)

pylab.subplot(234)
pylab.scatter(x_d, y_d, color='r')
make_diagonal(x_d, y_d)

pylab.subplot(235)
pylab.scatter(x_e, y_e, color='purple')
make_diagonal(x_e, y_e)

plt.show()

得到下图,

在此处输入图片说明

我想居中对齐最后一行子图。 我真的很感谢您的帮助。 提前致谢。

这是使用gridspec为您提供的最低工作答案:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(figsize=(10, 6))

# Define the plots here
gs = gridspec.GridSpec(2, 6)
gs.update(wspace=0.5)
ax1 = plt.subplot(gs[0, :2], )
ax2 = plt.subplot(gs[0, 2:4])
ax3 = plt.subplot(gs[0, 4:], )
ax4 = plt.subplot(gs[1, 1:3])
ax5 = plt.subplot(gs[1, 3:5])

# Plot your data here
ax1.plot(x,y, 'darkorange')
ax2.plot(x,y, 'g')
ax3.plot(x,y, 'b')
ax4.plot(x,y,'r')
ax5.plot(x,y,'purple')

产量

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM