TypeError:/:'float'和'str'的不支持的操作数类型 - Python / Matplotlib

时间:2015-12-17 16:24:12

标签: python matplotlib

我有以下两个清单:

CREATE UNIQUE INDEX name ON table (column [, ...]);

我正在通过以下函数传递它们:

list1=['21', '28', '28', '18', '17', '17', '18', '16', '20', '21', '22', '22', '20', '17', '23', '21', '20', '21', '21', '22', '22', '22', '21', '22', '21', '20', '21', '20', '22', '21', '24', '24', '23', '23', '23', '22', '22', '22', '23', '24', '22', '23', '24', '20', '22', '23', '24', '23', '24', '22', '25', '26', '22', '21', '21', '21', '21', '20', '21', '23', '23', '23', '24', '21', '26', '26', '27', '26', '26', '29', '27', '26', '25', '27', '27', '26', '26', '24', '24', '23', '26', '24', '26', '29', '29', '28', '27', '27', '26', '26', '26', '28', '25', '26', '26', '24', '25', '25', '26', '25', '25', '25', '26', '31', '25', '24', '24', '24', '24', '24', '25', '24', '22', '26', '27', '26', '27', '28', '25', '28', '27', '28', '27', '29', '28', '28', '29', '28', '28', '25', '27', '27', '27', '27', '27', '29', '31', '30', '28', '28', '27', '28', '28', '27', '26', '28', '27', '25', '27', '25', '27', '26', '26', '27', '28', '32', '28', '26', '27', '26', '25', '25', '26', '25', '26', '25', '27', '26', '26', '25', '28', '28', '29', '29', '31', '34', '33', '31', '32', '32', '30', '32', '32', '31', '32', '33', '35', '36', '37', '35', '35', '34', '33', '32', '32', '34', '32', '32', '32', '31', '32', '31', '33', '31', '33', '33', '34', '37', '32', '32', '32', '32', '32', '30', '34', '35', '33', '35', '34', '32', '36', '38', '38', '38', '37', '37', '38', '39', '37', '38', '38', '39', '39', '39', '38', '39', '38', '41', '39', '43', '44', '44', '45', '44', '42', '42', '44', '43', '41', '43', '43', '43', '40', '41', '39', '41', '39', '40', '40', '45', '42', '41', '40', '40', '41', '40', '42', '42', '38', '42', '44', '45', '45', '45', '44', '46', '48', '47', '50', '51', '50', '51', '49', '50', '48', '49', '50', '51', '50', '52', '53', '56', '57', '55', '54', '53', '55', '53', '54', '51', '53', '52', '51', '49', '48', '48', '49', '50', '47', '48', '51', '53', '53', '50', '47', '47', '48', '47', '48', '47', '49', '48', '52', '60', '58', '55', '100', '61', '55', '55', '54', '54', '54', '55', '55', '54', '53', '54', '53', '55', '56', '54', '55', '56', '58', '56', '56', '56', '55', '54', '55', '56', '56', '56', '56', '53', '54', '54', '54', '54', '52', '52', '54', '57', '58', '55', '54', '52', '52', '52', '54', '55', '54', '53', '53', '56', '57', '56', '56', '58', '55', '61', '59', '60', '61', '60', '64', '61', '62', '61', '60', '62', '63', '63', '65', '67', '69', '71', '68', '69', '66', '68', '70', '69', '69', '70', '69', '67', '68', '66', '65', '66', '63', '63', '64', '66', '72', '68', '65', '62', '64', '61', '63', '60', '59', '62', '63', '64', '65', '63', '61', '64', '63', '61', '61', '63', '64', '64', '63', '62', '65', '64', '64', '64', '64', '68', '66', '68', '69', '72', '70', '68', '66', '68', '65', '71', '69', '62', '63', '64', '61', '63', '64', '64', '64', '62', '60', '62', '70', '66', '65', '62', '62', '62', '62', '60', '64', '64', '64', '66', '66', '63', '69', '68', '66', '67', '69', '68', '67', '67', '68', '66', '68', '67', '68', '70', '71', '69', '70', '70', '75', '75', '73', '69', '71', '69', '69', '69', '73', '71', '71', '69', '67', '67', '65', '68', '69', '64', '66', '65', '70', '66', '66', '64', '63', '65', '64', '65', '66', '65', '65', '66', '68', '65', '65', '69', '66', '68', '65', '70', '69', '71', '69', '71', '72', '69', '68', '67', '68', '71', '72', '74', '76', '72', '71', '72', '70', '69', '67', '70', '70', '66', '66', '68', '67', '66', '67', '65', '74', '68', '67', '69', '69', '68', '69', '67', '67', '66', '64', '63', '66', '66', '66', '66', '66', '64', '62', '66', '66', '65', '66', '68', '70', '69', '68', '67', '67', '67', '64', '67', '66', '68', '66', '69', '68', '69', '69', '65', '66', '69', '68', '69', '66', '65', '65', '66', '66', '65', '66']
list2=['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '2', '3', '2', '2', '3', '3', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '3', '3', '2', '3', '4', '4', '3', '5', '4', '5', '3', '4', '4', '5', '4', '3', '4', '3', '5', '4', '4', '3', '3', '5', '4', '4', '3', '4', '4', '3', '3', '3', '3', '3', '4', '5', '4', '6', '5', '5', '6', '5', '5', '6', '6', '6', '6', '6', '6', '5', '6', '8', '9', '9', '6', '8', '7', '6', '7', '8', '11', '10', '8', '12', '10', '9', '9', '10', '10', '8', '9', '10', '11', '12', '13', '11', '12', '11', '9', '10', '14', '9', '11', '8', '8', '9', '9', '8', '9', '10', '7', '8', '8', '8', '9', '10', '9', '9', '11', '12', '12', '11', '12', '12', '12', '12', '11', '9', '11', '10', '10', '9', '11', '10', '10', '9', '9', '9', '10', '10', '10', '9', '9', '7', '8', '7', '8', '8', '11', '11', '10', '13', '11', '11', '12', '10', '11', '9', '17', '11', '12', '13', '15', '19', '25', '23', '27', '28', '29', '36', '36', '38', '33', '36', '30', '35', '35', '35', '30', '36', '33', '34', '34', '34', '33', '32', '36', '33', '43', '37', '33', '37', '35', '35', '36', '40', '34', '34', '30', '29', '32', '33', '34', '31', '31', '34', '36', '29', '25', '23', '21', '22', '23', '23', '22', '24', '23', '23', '26', '31', '28', '31', '35', '35', '32', '33', '27', '30', '32', '32', '32', '31', '34', '34', '43', '48', '53', '62', '63', '70', '62', '65', '67', '63', '60', '57', '62', '51', '54', '56', '58', '56', '58', '60', '71', '61', '55', '57', '57', '59', '50', '50', '44', '40', '37', '37', '36', '42', '40', '39', '40', '35', '38', '38', '36', '45', '47', '100', '69', '51', '47', '46', '48', '45', '41', '44', '42', '43', '44', '37', '41', '42', '42', '38', '36', '38', '40', '34', '34', '33', '36', '38', '36', '35', '36', '30', '34', '34', '29', '28', '26', '22', '19', '19', '47', '36', '24', '27', '27', '28', '27', '27', '21', '18', '18', '16', '14', '14', '14', '13', '12', '13', '15', '12', '12', '13', '11', '11', '10', '13', '11', '10', '11', '11', '10', '10', '10', '8', '9', '9', '8', '7', '7', '7', '7', '6', '7', '7', '8', '6', '7', '6', '6', '6', '5', '5', '6', '5', '5', '5', '6', '6', '6', '5', '6', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '6', '5', '6', '7', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '5', '5', '5', '6', '5', '5', '5', '5', '5', '5', '5', '6', '5', '5', '4', '4', '5', '4', '4', '4', '4', '4', '4', '4', '5', '4', '5', '5', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '4', '5', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '3', '3', '4', '4', '4', '3', '3', '3', '3', '3', '3', '4', '3', '3', '3', '4', '3', '3', '3', '3', '4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '3', '3', '3', '3', '3', '3', '3', '3', '2']

我收到以下错误消息:

def line_plot(list1,list2,datelist,labellist1="list1",labellist2="list2"):
"""
Plots a chart based on two different lists
:param list1: list 
:param list2: list
:param datelist: a list of dates
:return: a chart with both lists
"""
import matplotlib.pyplot as plt

norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]

plt.plot(datelist,norm_list1,label=labellist1)
plt.plot(datelist,norm_list2,label=labellist2)
plt.xlabel("Date")
plt.legend()
plt.show()

关于我做错什么的任何想法?

2 个答案:

答案 0 :(得分:3)

您尝试将整数除以字符串。

norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]

max(list1)将在列表中提供最高的字符串,因为您的列表由字符串组成。为什么列表中有字符串而不是整数?将列表转换为int或float或使用下面的代码

解决错误并保留你的字符串(这可能不是你想要的):

norm_list1=[float(i)/float(max(list1)) for i in list1]
norm_list2=[float(i)/float(max(list2)) for i in list2]

只需将列表转换为int或float并解决错误(可能是您想要的):

newlist1 = [float(i) for i in list1]
newlist2 = [float(i) for i in list2]

然后使用新列表。

答案 1 :(得分:0)

字符串整数列表中的max也返回字符串,因此您尝试划分float和string。

>>> max(['333', '22222'])
'333'

为什么呢? answer

>>> 2.0 / '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'float' and 'str'