TypeError: Cannot interpret '12.779999999999998' as a data type


I am trying to plot my data on a chart with matplotlib but I keep getting an error that states that 12.7799 cannot be interpreted as a data type. It works when I take out the figure of the predicted gas prices but not when I include it in. I have tried to convert it to an int but the error still keeps showing up.

#Comparing Gas Prices in Ireland From 2021 & 2022

import time
import matplotlib.pyplot as plt
import numpy as np
#The first number is the gas price in July of 2021 and the second number is the gas price in July of 2022
gp = [1.75, 2.13] 
diff = round(gp[1] - gp[0], 2)
diff1 = '€' + str(diff)
percInc = round(((gp[1] - gp[0]) / gp[0]) * 100, 2) 
percInc1 = str(percInc) + '%'

print('Today we will be comparing the Gas Prices in Ireland from 2021 & 2022')
print('Gas Prices in July 2021 were €1.75 per litre and in July 2022 were €2.13 per litre.')
print('The difference in price between 2021 and 2022 is', diff1)
print('The percentage increase in price was',percInc1)

print('Now it is your turn to predict the gas prices in 2023')
userInf = int(input('Enter the inflation rate for the next year (whole number between -10 and 10: '))
gp1 = gp[1] * userInf
gp23 = gp1 + gp[1]
print('The predicted price for 2023 is', gp23)

x = np.array(['2021', '2022', '2023'])
y = np.array([gp[0], gp[1]], gp23) #where problem occurs
plt.title('Comparing Gas Prices in Ireland')
plt.bar(x,y, align='edge', width=-0.4)

This is the error I am receiving

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    y = np.array([gp[0], gp[1]], gp23)
TypeError: Cannot interpret '12.779999999999998' as a data type

Can anyone please help me identify the mistake?

Asked By: R1111111111



Try this:
y = np.array([x , y, z]) instead of y = np.array([x ,y], z)

I checked it on my end and it works 😉enter image description here

y = np.array([gp[0], gp[1], gp23]) 
Answered By: Kavya Kommuri
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.