我在为python subplot函数输入参数时遇到了困难。
我想要的是用以下条件在同一图像文件上绘制4个图形
left
space
right
space
left
space
right我尝试了3个数字的不同方法,但输出不正确。
发布于 2011-02-20 09:22:31
你是说像这样的东西吗?
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(4,2,1)
ax2 = fig.add_subplot(4,2,4)
ax3 = fig.add_subplot(4,2,5)
ax4 = fig.add_subplot(4,2,8)
fig.subplots_adjust(hspace=1)
plt.show()

发布于 2014-08-06 17:51:35
关于sublot函数模板的不易找到的文档如下所示:
subplot (number_of_graphs_horizontal, number of graphs_vertical, index)让我们研究一下Joe Kington上面的代码:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(4,2,1)
ax2 = fig.add_subplot(4,2,4)
ax3 = fig.add_subplot(4,2,5)
ax4 = fig.add_subplot(4,2,8)
fig.subplots_adjust(hspace=1)
plt.show()您告诉matplotlib,您需要一个网格,其中包含4行2列的图。ax1、ax2等是您在索引位置添加的图形,您可以将其作为第三个参数读取。您以行的方式从左到右计数。
我希望这能有所帮助:)
发布于 2011-02-20 10:30:49
Matplotlib提供了几种方法来处理在单个页面上故意放置绘图的问题;我认为最好的是gridspec,我相信它最早出现在1.0版本中。顺便说一下,另外两个是(i)直接索引子图和(ii)新的ImageGrid工具包)。
GridSpec的工作原理类似于图形用户界面工具包中的基于网格的打包器,用于将小部件放置在父框架中,因此至少出于这个原因,它似乎是三种放置技术中最容易使用和最具可配置性的。
import numpy as NP
import matplotlib.pyplot as PLT
import matplotlib.gridspec as gridspec
import matplotlib.cm as CM
V = 10 * NP.random.rand(10, 10) # some data to plot
fig = PLT.figure(1, (5., 5.)) # create the top-level container
gs = gridspec.GridSpec(4, 4) # create a GridSpec object
# for the arguments to subplot that are identical across all four subplots,
# to avoid keying them in four times, put them in a dict
# and let subplot unpack them
kx = dict(frameon = False, xticks = [], yticks = [])
ax1 = PLT.subplot(gs[0, 0], **kx)
ax3 = PLT.subplot(gs[2, 0], **kx)
ax2 = PLT.subplot(gs[1, 1], **kx)
ax4 = PLT.subplot(gs[3, 1], **kx)
for itm in [ax1, ax2, ax3, ax4] :
itm.imshow(V, cmap=CM.jet, interpolation='nearest')
PLT.show()
除了在一个“棋盘”配置中排列四个图(根据您的问题)之外,我没有尝试调整这个配置,但这很容易做到。例如,
# to change the space between the cells that hold the plots:
gs1.update(left=.1, right=,1, wspace=.1, hspace=.1)
# to create a grid comprised of varying cell sizes:
gs = gridspec.GridSpec(4, 4, width_ratios=[1, 2], height_ratios=[4, 1])

https://stackoverflow.com/questions/5054593
复制相似问题