jch*_*d20 3 matplotlib python-3.x
我的目标是在每组4个子图周围放置一个边界框。我似乎无法弄清楚如何使用我的gridspec子图。我遇到的示例是指单个子图,而不是整个gridspec。
(我选择使用gridspec进行这样的绘制,以便可以控制子图组之间的间距)
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(16,16))
fig.suptitle(' title ', fontsize=12,
bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})
gs = gridspec.GridSpec(2, 2)
gs.update(top=.48,left=0.1, right=0.48, wspace=0.15,hspace=0.2)
ax1 = plt.subplot(gs[0])
ax1.set_title('axes title')
ax2 = plt.subplot(gs[1])
ax2.set_title('axes title')
ax3 = plt.subplot(gs[2])
ax3.set_title('axes title')
ax4 = plt.subplot(gs[3])
ax4.set_title('axes title')
#New Gridspec
gs1 = gridspec.GridSpec(2, 2)
gs1.update(bottom=.53,left=0.55, right=0.9, hspace=0.2, wspace=.15)
ax5 = plt.subplot(gs1[0])
ax5.set_title('axes title')
ax6 = plt.subplot(gs1[1])
ax6.set_title('axes title')
ax7 = plt.subplot(gs1[2])
ax7.set_title('axes title')
ax8 = plt.subplot(gs1[3])
ax8.set_title('axes title')
#New Gridspec
gs2 = gridspec.GridSpec(2, 2)
gs2.update(top=.48,left=0.55, right=0.9, hspace=0.2, wspace=.15)
ax9 = plt.subplot(gs2[0])
ax9.set_title('axes title')
ax10 = plt.subplot(gs2[1])
ax8.set_title('axes title')
ax11 = plt.subplot(gs2[2])
ax11.set_title('axes title')
ax12 = plt.subplot(gs2[3])
ax12.set_title('axes title')
#New Gridspec
gs3 = gridspec.GridSpec(2, 2)
gs3.update(bottom=.53,left=0.1, right=0.48, wspace=0.15,hspace=0.2)
ax13 = plt.subplot(gs3[0])
ax13.set_title('axes title')
ax14 = plt.subplot(gs3[1])
ax14.set_title('axes title')
ax15 = plt.subplot(gs3[2])
ax15.set_title('axes title')
ax16 = plt.subplot(gs3[3])
ax16.set_title('axes title')
plt.savefig('test.png',bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
在几个子图周围创建框架或边框的一种选择可能是在图形上添加另一个轴,该轴比相应的gridspec区域的范围大一点。
outergs = gridspec.GridSpec(1, 1)
outergs.update(bottom=.50,left=0.07, right=0.50,top=0.93)
outerax = fig.add_subplot(outergs[0])
outerax.tick_params(axis='both',which='both',bottom=0,left=0,
labelbottom=0, labelleft=0)
Run Code Online (Sandbox Code Playgroud)
为了使其正常工作,必须通过创建所有其他轴,
ax_i = fig.add_subplot(gs...) 而不是ax_i = plt.subplot(gs...)
然后,您当然可以outerax根据自己的喜好修改,例如,通过
outerax.set_facecolor(colors[i])
outerax.patch.set_alpha(0.3)
Run Code Online (Sandbox Code Playgroud)
完成上述代码的完整代码:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(9,9))
fig.suptitle(' title ', fontsize=12,
bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})
colors=["crimson", "indigo", "limegreen", "gold"]
for i in range(4):
#outer
outergs = gridspec.GridSpec(1, 1)
outergs.update(bottom=(i//2)*.47+0.01,left=(i%2)*.5+0.02,
top=(1+i//2)*.47-0.01, right=(1+i%2)*.5-0.02)
outerax = fig.add_subplot(outergs[0])
outerax.tick_params(axis='both',which='both',bottom=0,left=0,
labelbottom=0, labelleft=0)
outerax.set_facecolor(colors[i])
outerax.patch.set_alpha(0.3)
#inner
gs = gridspec.GridSpec(2, 2)
gs.update(bottom=(i//2)*.47+0.05,left=(i%2)*.5+0.08,
top=(1+i//2)*.47-0.05, right=(1+i%2)*.5-0.05,
wspace=0.35, hspace=0.35)
for k in range(4):
ax = fig.add_subplot(gs[k])
ax.set_title('Axes Title {}'.format(k+1), color=colors[i])
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1244 次 |
| 最近记录: |