Numpy 如何在子批次中组合两个数据帧?

Numpy 如何在子批次中组合两个数据帧?,numpy,matplotlib,Numpy,Matplotlib,我像这样设置了一个数据帧 import numpy as np import matplotlib.pyplot as plt midterm = np.random.randint(0, 100, size = (1,5)) finals = np.random.randint(0, 100, size = (1,5)) print(midterm) print(finals) 接下来我做了一些代码 fig = plt.figure() ax1 = fig.add_subplot(3, 1,

我像这样设置了一个数据帧

import numpy as np
import matplotlib.pyplot as plt
midterm = np.random.randint(0, 100, size = (1,5))
finals = np.random.randint(0, 100, size = (1,5))
print(midterm)
print(finals)
接下来我做了一些代码

fig = plt.figure()
ax1 = fig.add_subplot(3, 1, 1)
ax2 = fig.add_subplot(3, 1, 2)
ax3 = fig.add_subplot(3, 1, 3)
labels = ['a', 'b', 'c', 'd', 'e']
width = 0.35

ax1.bar(labels, midterm, width, label='midterm')
ax.legend()
plt.subplots_adjust(hspace=0.1)
plt.show()


ax2.bar(labels, finals, width, label='finals')
ax2.legend()
plt.subplots_adjust(hspace=0.1)
plt.show()
我的目标是将ax1与ax2结合起来,并在ax3中打印,所以我尝试这样做

ax3.bar(labels, midterm*0.4, width, label='midterm')
ax3.bar(labels, finals*0.6, width, bottom=midterm,
        label='finals')
ax3.set_ylabel('Scores')
ax3.set_title('Scores by each term')
ax3.legend()

plt.show()
但是结果没有出来。我该怎么做?

将您的数据创建为pandasonic数据帧:

midterm = np.random.randint(0, 100, size = 5)
finals = np.random.randint(0, 100, size = 5)
labels = ['a', 'b', 'c', 'd', 'e']
df = pd.DataFrame({'midterm': midterm, 'finals': finals}, index=labels)
然后,要打印所有3个子批次,请运行:

fig = plt.figure(figsize=(6, 8))
ax1 = fig.add_subplot(3, 1, 1)
ax2 = fig.add_subplot(3, 1, 2)
ax3 = fig.add_subplot(3, 1, 3)
width = 0.5
df.midterm.plot.bar(width=width, ax=ax1, rot=0, legend=True)
df.finals.plot.bar(width=width, ax=ax2, rot=0, legend=True)
df.plot.bar(width=width, stacked=True, ax=ax3, rot=0)
plt.show()
注意增加了figsize,否则所有子地块的高度都非常小

结果是:


非常感谢您!顺便说一下,我想结合期中考试乘以40%和期末考试乘以60%。你能解决这个问题吗?也许你应该在生成打印输出之前执行所有的Miltiplication。