Matplotlib: subplot

只愿长相守 提交于 2020-01-23 17:04:12

问题


I have several time series signals (8x8) that I would like to plot using subplot. My data are stored in a matrix called H(x, y, N) where N is the number of points in each signal. I would like to display the 64 signals using subplots.

fig  = figure(figsize=(12,8))
time = np.arange(0, Nt, 1)

for x in range(8):
    for y in range(8):
        subplot(8,y+1,x+1)
        plot(time,H[x,y,:])

What I get is 8 signals in the first row, 4 in the second one, then 2, 2, 1, 1, 1 and 1.


回答1:


That's not how subplot indexing works. From the docs to subplot:

subplot(nrows, ncols, plot_number)

Where nrows and ncols are used to notionally split the figure into nrows * ncols sub-axes, and plot_number is used to identify the particular subplot that this function is to create within the notional grid. plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.

So, you want to have nrows=8, ncols=8 and then a plot_number in the range 1-64, so something like:

nrows,ncols = 8,8
for y in range(8):
    for x in range(8):
        plot_number = 8*y + x + 1
        subplot(nrows,ncols,plot_number)
        plot(time,H[x,y,:])

        # Remove tick labels if not on the bottom/left of the grid
        if y<7: gca().set_xticklabels([])
        if x>0: gca().set_yticklabels([])

To remove tick labels, use gca() to get the current axes, and the set the xticklabels and yticklabels to an empty list: []



来源:https://stackoverflow.com/questions/35510155/matplotlib-subplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!