开发者

Python Subplot function parameters

I am having a hard time with putting in the parameters for the python subplot function.

What I want is to plot 4 graphs on a same image file with the following criteria

left
space
     right
space
left
space
     right

I have tried开发者_如何学C different ways of the 3 numbers but the output doesnt show up correctly.


Do you mean something like this?

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()

Python Subplot function parameters


Well, the not-so-easily-found documentation regarding the sublot function template is as follows:

subplot (number_of_graphs_horizontal, number of graphs_vertical, index)

Let us investigate the code from Joe Kington above:

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()

You told matplotlib that you want a grid with 4 rows and 2 columns of graphs. ax1, ax2 and so on are the graphs that you add at the index positions which you can read as the third parameter. You count from left to right in a row-wise manner.

I hope that helped :)


Matplotlib provides several ways deal with the deliberate placement of plots on a single page; i think the best is gridspec, which i believe first appeared in the 1.0 release. The other two, by the way, are (i) directly indexing subplot and (ii) the new ImageGrid toolkit).

GridSpec works like grid-based packers in GUI toolkits used to placed widgets in a parent frame, so for that reason at least, it seems the easiest to use and the most configurable of the three placement techniques.

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()


Beyond just arranging the four plots in a 'checkerboard' configuration (per your Question), I have not tried to tune this configuration, but that's easy to do. E.g.,

# 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])

Python Subplot function parameters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜