J'essaie de tracer un graphique à barres à l'aide de matplotlib mais cela m'affiche ce message d'erreur ValueError: shape mismatch: les objets ne peuvent pas être diffusés sur une seule forme

Voici mon code:

print(lol)
    
X = numpy.arange(3)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, lol[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, lol[2], color = 'r', width = 0.25)
plt.show()

Voici ma sortie avec message d'erreur : Ici, les données sont celles qui doivent être tracées dans le graphique à barres il est stocké dans lol sous la forme list de list.

[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]

Erreur :

Traceback (most recent call last):
      File "E:\Pyton Excel Extractions\dashboard.py", line 68, in <module>
        ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
      File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\__init__.py", line 1438, in inner
        return func(ax, *map(sanitize_sequence, args), **kwargs)
      File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
        x, height, width, y, linewidth = np.broadcast_arrays(
      File "<__array_function__ internals>", line 5, in broadcast_arrays
      File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\stride_tricks.py", line 258, in broadcast_arrays
        shape = _broadcast_shape(*args)
      File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\stride_tricks.py", line 189, in _broadcast_shape
        b = np.broadcast(*args[:32])
    ValueError: shape mismatch: objects cannot be broadcast to a single shape

Aide moi!

0
prateek_pro 8 oct. 2020 à 16:10

1 réponse

Meilleure réponse

X et lol[0], lol[1] et lol[2] ont des longueurs différentes (3 et 99 respectivement). Changer la longueur du X donne la sortie suivante. est-ce que cela aide?

import numpy
import matplotlib.pyplot as plt

lol=[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]

#changing the length of X such that it matches lol[0],lol[1] etc.
X = numpy.arange(len(lol[0]))
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, lol[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, lol[2], color = 'r', width = 0.25)

plt.show()

Sortie :

enter image description here

Sur la base de votre commentaire, quelque chose comme ça serait bien. Ici, j'utilise pandas :

import numpy
import pandas as pd
import matplotlib.pyplot as plt

lol=[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]


df = pd.DataFrame(lol).T
df.columns = ['1st Month','2nd Month', '3rd Month']
df['Product'] = numpy.arange(len(lol[0]))

#if you don't like subplots keep subplots=False
df.plot.bar(x='Product',subplots=True, figsize=(20,10)) 
plt.show()

Sortie : entrez la description de l'image ici

1
Grayrigel 8 oct. 2020 à 14:33