Home > OS >  Matplotlib without numpy or panda
Matplotlib without numpy or panda

Time:11-25

i'm trying to creat a bar grafic using the matplotlib, but i can't use the numpy or panda. so i'd like some help with the bars separation

here is my code:

import matplotlib.pyplot as plt

eixo_X_ano = [78,82,86,90,94]
eixo_Y_AMM = [1.8,1.75,1.9,1.77,1.74]
eixo_Y_AMF = [1.71,1.69,1.8,1.73,1.82]


plt.title("Altura Media dos atletas que ganharam medalhas")
plt.xlabel("Anos")
plt.ylabel("Altura media")


largura = 2
plt.ylim(1,2.50)
plt.bar(eixo_X_ano - largura/2, eixo_Y_AMM, largura, label = "Masculino", color = "teal")
plt.bar(eixo_X_ano   largura/2, eixo_Y_AMF, largura, label = "Feminino", color = "r")
plt.legend()
plt.xticks(eixo_X_ano)
plt.show()

CodePudding user response:

If you're not using numpy, you can't do array arithmetic (that is, eixo_X_ano - largura/2). You have to do it yourself:

import matplotlib.pyplot as plt

eixo_X_ano = [78,82,86,90,94]
eixo_Y_AMM = [1.8,1.75,1.9,1.77,1.74]
eixo_Y_AMF = [1.71,1.69,1.8,1.73,1.82]

plt.title("Altura Media dos atletas que ganharam medalhas")
plt.xlabel("Anos")
plt.ylabel("Altura media")

largura = 2

x1 = [i - largura/2 for i in eixo_X_ano]
x2 = [i   largura/2 for i in eixo_X_ano]

plt.ylim(1,2.50)
plt.bar(x1, eixo_Y_AMM, largura, label = "Masculino", color = "teal")
plt.bar(x2, eixo_Y_AMF, largura, label = "Feminino", color = "r")
plt.legend()
plt.xticks(eixo_X_ano)
plt.show()

As Michael said, your restriction against numpy is silly. Matplotlib imports numpy, so there is no cost in importing it yourself.

CodePudding user response:

Remove - largura/2 and largura/2 and your bars will have a nice separation between them.

  • Related