Last answered:

17 Aug 2022

Posted on:

16 Aug 2022


Question 5 "Only length -1 arrays can be converted to Python scalars while plot showing

Hello, I'm getting an error for question 5 and I don't understand how to prevent it.  My code is as follows:

"import string
from random import shuffle
x = [[i] for i in range(26)]
letters = list(string.ascii_lowercase)
numbers = x

solution = dict(zip(letters,numbers))
import matplotlib.pyplot as plt
letters,numbers = zip(*solution.items())


1 answers ( 0 marked as helpful)
Posted on:

17 Aug 2022



Thank you for your question and your solution to the exercise!

The documentation of matplotlib's bar() method instructs that both the first and the second argument the method takes in have to be either float or array-like objects. Typing


in your code, you will see that those variables are, in fact, tuples. We therefore need to convert them to array-like objects in order to use the bar() method. The following piece of code could do that:

import numpy as np

letters = np.asarray(letters)
numbers = np.asarray(numbers).reshape(1, 26)[0],numbers)

Here, I redefine the letters and numbers variables such that they are both ndarray objects containing 26 elements.

Hope this helps!

Kind regards,
365 Hristina

Submit an answer