0

I have some info and I want to observe it with some plots one near the other.

When I just want to show one plot I'm not having problems,

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

But now I want to show different plots and It's failing,

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

I don't understand what is happening. What I'm trying to do, is create different subplots, define that I want 1 row, 2 columns, and with pos define the position.

Can somebody help me? I'm really blocked with matplotlib.

Thank you very much

Lleims
  • 1,275
  • 12
  • 39

1 Answers1

1

plt.subplot() States that it is a wrapper of fib.add_subplot(). The documentation state that it returns an axes object. You are expecting a fig and ax to be returned, but only ax is returned by the object. Change

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

to

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

Full code:

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

enter image description here

Cohan
  • 4,384
  • 2
  • 22
  • 40