Python Can';t绘制两个图-Matplotlib

Python Can';t绘制两个图-Matplotlib,python,matplotlib,Python,Matplotlib,我有一些信息,我想用一个挨着另一个的一些图来观察它 当我只想展示一个情节时,我没有问题 import matplotlib import matplotlib.pyplot as plt import numpy as np labels = ['2', '3', '4', '5'] ayax_cara = [20, 34, 30, 27] ayax_cruz = [25, 32, 20, 25] ayax_rojo = [20, 34, 30, 27] ayax_negro = [25,

我有一些信息,我想用一个挨着另一个的一些图来观察它

当我只想展示一个情节时,我没有问题

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


labels = ['2', '3', '4', '5']
ayax_cara = [20, 34, 30, 27]
ayax_cruz = [25, 32, 20, 25]
ayax_rojo = [20, 34, 30, 27]
ayax_negro = [25, 32, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, ayax_cara, width, label='Cara')
rects2 = ax.bar(x - width*1.5, ayax_cruz, width, label='Cruz')
rects3 = ax.bar(x + width/2, ayax_rojo, width, label='Rojo')
rects4 = ax.bar(x + width*1.5, ayax_negro, width, label='Negro')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xlabel('NCD')
ax.set_title('AYAX')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

plt.show()
但现在我想展示不同的情节,但失败了

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


labels = ['2', '3', '4', '5']
ayax = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
prok = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
albz = [ayax, prok]

plt.figure(figsize=(24,16))
x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars
pos = 1

for al in albz:
    fig, ax = plt.subplot(1,2,pos)
    rects1 = ax.bar(x - width/2, al[0], width, label='Cara')
    rects2 = ax.bar(x - width*1.5, al[1], width, label='Cruz')
    rects3 = ax.bar(x + width/2, al[2], width, label='Rojo')
    rects4 = ax.bar(x + width*1.5, al[3], width, label='Negro')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_xlabel('NCD')
    ax.set_title('AYAX')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend()
    pos += 1

plt.show()
我不明白发生了什么事。我想做的是创建不同的子图,定义我想要的1行,2列,然后用
pos
定义位置

有人能帮我吗?我真的被matplotlib阻止了

非常感谢

声明它是fib.add_subplot()的包装。文档声明它返回一个
对象。您希望返回
fig
ax
,但对象只返回
ax
。改变

fig, ax = plt.subplot(1,2,pos)

完整代码:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


labels = ['2', '3', '4', '5']
ayax = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
prok = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
albz = [ayax, prok]

plt.figure(figsize=(24,16))
x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars
pos = 1

for al in albz:
    ax = plt.subplot(1,2,pos)  # <-- Get rid of the fig
    rects1 = ax.bar(x - width/2, al[0], width, label='Cara')
    rects2 = ax.bar(x - width*1.5, al[1], width, label='Cruz')
    rects3 = ax.bar(x + width/2, al[2], width, label='Rojo')
    rects4 = ax.bar(x + width*1.5, al[3], width, label='Negro')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_xlabel('NCD')
    ax.set_title('AYAX')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend()
    pos += 1

plt.show()
导入matplotlib
将matplotlib.pyplot作为plt导入
将numpy作为np导入
标签=['2','3','4','5']
ayax=[[20,34,30,27],[25,32,20,25],[20,34,30,27],[25,32,20,25]]
prok=[[20,34,30,27],[25,32,20,25],[20,34,30,27],[25,32,20,25]]
albz=[ayax,prok]
plt.图(figsize=(24,16))
x=np.arange(len(标签))#标签位置
宽度=0.1#钢筋的宽度
位置=1
对于albz中的al:
ax=零件子批次(1,2,位置)#
import matplotlib
import matplotlib.pyplot as plt
import numpy as np


labels = ['2', '3', '4', '5']
ayax = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
prok = [[20, 34, 30, 27],[25, 32, 20, 25],[20, 34, 30, 27],[25, 32, 20, 25]]
albz = [ayax, prok]

plt.figure(figsize=(24,16))
x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars
pos = 1

for al in albz:
    ax = plt.subplot(1,2,pos)  # <-- Get rid of the fig
    rects1 = ax.bar(x - width/2, al[0], width, label='Cara')
    rects2 = ax.bar(x - width*1.5, al[1], width, label='Cruz')
    rects3 = ax.bar(x + width/2, al[2], width, label='Rojo')
    rects4 = ax.bar(x + width*1.5, al[3], width, label='Negro')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_xlabel('NCD')
    ax.set_title('AYAX')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend()
    pos += 1

plt.show()