美文网首页
Matplotlib: 柱形图和折线图在同一图中

Matplotlib: 柱形图和折线图在同一图中

作者: wzNote | 来源:发表于2020-03-07 21:09 被阅读0次
代码
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()
效果

相关文章

网友评论

      本文标题:Matplotlib: 柱形图和折线图在同一图中

      本文链接:https://www.haomeiwen.com/subject/fyxkdhtx.html