代码
import numpy as np
import matplotlib.pyplot as plt
# 准备数据
trades = [2190.21, 3225.59, 4398.6, 4584.51, 4714.29, 4833.77, 3830.86, 3680, 4550, 5650]
d = [(trades[i+1]-trades[i])/trades[i] for i in range(len(trades)-1) ]
x = np.arange(2009,2019)
y1 = np.array(trades)
y2 = np.array(d)
fig, ax1 = plt.subplots()
# 柱形图
l1 = ax1.bar(x, y1, color='c', label='a')
ax2 = ax1.twinx()
# 折线图
l2, = ax2.plot(x[1:], y2, 'r-', label='b')
plt.legend(handles=[l1,l2], loc='lower center',frameon=False,bbox_to_anchor=(0.5, -0.21),ncol=2)
plt.show()
效果
网友评论