如何在一个图形中制作10个以上的子图? [英] How to make more than 10 subplots in a figure?

查看:74
本文介绍了如何在一个图形中制作10个以上的子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个5x4的子图网格,通过查看示例,我觉得最好的方法是:

 将matplotlib.pyplot导入为pltplt.figure()plt.subplot(221) 

其中子图(22)中的前两个数字表示它是2x2网格,第三个数字表示您正在制作4个网格中的哪个.但是,当我尝试执行此操作时,我不得不执行以下操作:

  plt.subplot(5420) 

我得到了错误:

  ValueError:整数子图规范必须为三位数.不是4 

那么这是否意味着您不能制作超过10个子图,或者有办法解决,或者我误解了它的工作原理?

谢谢.

解决方案

您可能正在寻找

您应该构建嵌套循环来制作完整的网格吗?

 将matplotlib.pyplot导入为pltplt.figure(0)对于我在范围(5)中:对于范围(4)中的j:plt.subplot2grid((5,4),(i,j))plt.show() 

,您将获得以下信息:

图的工作方式与任何子图中相同(直接从创建的轴调用它):

 将matplotlib.pyplot导入为plt将numpy导入为npplt.figure(0)地块= []对于我在范围(5)中:对于范围(4)中的j:斧= plt.subplot2grid((5,4),(i,j))ax.scatter(范围(20),范围(20)+ np.random.randint(-5,5,20))plt.show() 

,导致以下结果:

请注意,您可以为图提供不同的大小(说明每个图的列数和行数):

 将matplotlib.pyplot导入为pltplt.figure(0)ax1 = plt.subplot2grid((3,3),(0,0),colspan = 3)ax2 = plt.subplot2grid((3,3),(1,0),colspan = 2)ax3 = plt.subplot2grid((3,3),(1,2),行跨度= 2)ax4 = plt.subplot2grid((3,3),(2,0))ax5 = plt.subplot2grid((3,3),(2,1))plt.show() 

,因此:

在我开头给出的链接中,您还将找到删除标签等示例.

I am trying to make a 5x4 grid of subplots, and from looking at examples it seems to me that the best way is:

import matplotlib.pyplot as plt
plt.figure()
plt.subplot(221)

where the first two numbers in the subplot (22) indicate that it is a 2x2 grid and the third number indicates which one of the 4 you are making. However, when I tried this I had to go up to:

plt.subplot(5420)

and I got the error:

ValueError: Integer subplot specification must be a three digit number.  Not 4

So does that mean you cannot make more that 10 subplots, or is there a way around it, or am I misunderstanding how it works?

Thank you in advance.

解决方案

You are probably looking for GridSpec. You can state the size of your grid (5,4) and the position for each plot (row = 0, column = 2, i.e. - 0,2). Check the following example:

import matplotlib.pyplot as plt

plt.figure(0)
ax1 = plt.subplot2grid((5,4), (0,0))
ax2 = plt.subplot2grid((5,4), (1,1))
ax3 = plt.subplot2grid((5,4), (2, 2))
ax4 = plt.subplot2grid((5,4), (3, 3))
ax5 = plt.subplot2grid((5,4), (4, 0))
plt.show()

, which results in this:

Should you build nested loops to make your full grid:

import matplotlib.pyplot as plt

plt.figure(0)
for i in range(5):
    for j in range(4):
        plt.subplot2grid((5,4), (i,j))
plt.show()

, you'll obtain this:

The plots works the same as in any subplot (call it directly from the axes you've created):

import matplotlib.pyplot as plt
import numpy as np

plt.figure(0)
plots = []
for i in range(5):
    for j in range(4):
        ax = plt.subplot2grid((5,4), (i,j))
        ax.scatter(range(20),range(20)+np.random.randint(-5,5,20))
plt.show()

, resulting in this:

Notice that you can provide different sizes to plots (stating number of columns and rows for each plot):

import matplotlib.pyplot as plt

plt.figure(0)
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))
plt.show()

, therefore:

In the link I gave in the beginning you will also find examples to remove labels among other stuff.

这篇关于如何在一个图形中制作10个以上的子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆