如何在 matplotlib.pyplot 中手动定位一个子图 [英] How to manually position one subplot graph in matplotlib.pyplot
本文介绍了如何在 matplotlib.pyplot 中手动定位一个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何重新定位第二行中的图形,使其相对于整个图形居中对齐.更一般地说,如何在同一行中的任何位置手动重新定位图形?
将 matplotlib.pyplot 导入为 plt米=0对于范围内的 i (0,2):对于范围内的 j (0,2):如果 m <= 2:ax = plt.subplot2grid((2,2), (i,j))ax.plot(范围(0,10),范围(0,10))m = m+1plt.show()
谢谢.
解决方案
使用
How do I reposition the graph in the second row such that it is center aligned with respect to the WHOLE figure. On a more general note how to manually reposition the graph anywhere in the same row?
import matplotlib.pyplot as plt
m=0
for i in range(0,2):
for j in range(0,2):
if m <= 2:
ax = plt.subplot2grid((2,2), (i,j))
ax.plot(range(0,10),range(0,10))
m = m+1
plt.show()
Thank you.
解决方案
Using GridSpec, you can create a grid of any dimension that can be used for subplot placement.
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
gs = gridspec.GridSpec(4, 4)
ax1 = plt.subplot(gs[:2, :2])
ax1.plot(range(0,10), range(0,10))
ax2 = plt.subplot(gs[:2, 2:])
ax2.plot(range(0,10), range(0,10))
ax3 = plt.subplot(gs[2:4, 1:3])
ax3.plot(range(0,10), range(0,10))
plt.show()
This will create a 4x4 grid, with each subplot taking up a 2x2 sub-grid. This allows you to properly center the bottom subplot.
To use the same same for
loop method that you use in your question, you could do the following:
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
gs = gridspec.GridSpec(4, 4)
m = 0
for i in range(0, 4, 2):
for j in range(0, 4, 2):
if m < 3:
ax = plt.subplot(gs[i:i+2, j:j+2])
ax.plot(range(0, 10), range(0, 10))
m+=1
else:
ax = plt.subplot(gs[i:i+2, 1:3])
ax.plot(range(0, 10), range(0, 10))
plt.show()
The result of both of these code snippets is:
这篇关于如何在 matplotlib.pyplot 中手动定位一个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文