TypeError: unsupported operand type(s) for -: 'list' and 'float' in python bar chart

TypeError: unsupported operand type(s) for -: 'list' and 'float' in python bar chart

我正在尝试绘制分组条形图,我有这些数据。

y1= [2232424, 2324353, 0, 8433232, 21421521, 2164216, 2761731,  752164215]
y2=[0, 32, 253, 6271, 263, 5535142, 1513153, 92512152]

我想绘制这样的图表,标签以 45 度角打印

收到此错误

plt.bar(x-0.2, y1, width) TypeError: unsupported operand type(s) for -: 'list' and 'float'

我的代码是这样的

import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams.update({'font.size': 16})
from matplotlib.pyplot import figure
figure(figsize=(8, 6), dpi=80)
x = [0,1,2,3,4,5,6,7]
L = ['AAAAAA', 'BBBB', 'CCCCCC','DDDDDD', 'EEEEE', 'FFFFFFFFF', 'FGGGGG','HHHHHHHHHH']  
y1= [2232424, 2324353, 0, 8433232, 21421521, 2164216, 2761731,  752164215]
y2=[0, 32, 253, 6271, 263, 5535142, 1513153, 92512152]

width = 0.40
plt.bar(y1, width)
plt.bar(y2, width)

plt.legend(['one', 'two'], loc='upper right')
plt.xticks(x, L, rotation=30, horizontalalignment='right')
plt.show()

List对象不允许广播操作

如果你想广播,只需像下面这样扭曲 numpy 数组对象。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams.update({'font.size': 16})
from matplotlib.pyplot import figure
figure(figsize=(8, 6), dpi=80)

x = np.array([0,1,2,3,4,5,6,7])
L = ['AAAAAA', 'BBBB', 'CCCCCC','DDDDDD', 'EEEEE', 'FFFFFFFFF', 'FGGGGG','HHHHHHHHHH']  
y1= [2232424, 2324353, 0, 8433232, 21421521, 2164216, 2761731,  752164215]
y2=[0, 32, 253, 6271, 263, 5535142, 1513153, 92512152]

width = 0.40
plt.bar(x - width/2, y1, width)
plt.bar(x + width/2, y2, width)

plt.legend(['one', 'two'], loc='upper right')
plt.xticks(x, L, rotation=30, horizontalalignment='right')
plt.show()