如何在条形图和折线图的同一图上绘制多个列表?

时间:2020-06-21 20:58:46

标签: python

我有4个列表-yr_list,income_list,expense_list和profit_list。

我想在y轴上显示yr_list,在y轴上显示入帐为条形图,在y轴上显示出支出表的条形图,在次要y轴上显示为收益线的条形图。我该怎么办?

enter image description here

[2013, 2014, 2015, 2016, 2017, 2018]
[3174507.7278688527, 4666571.258098361, 4757771.147540984, 3347104.209836066, 3475079.6604590164, 3282371.708852459]
[2957117.5807213113, 4133987.196196721, 4617619.47947541, 3218228.637639344, 3402567.9462295077, 3226548.6240000003]
[217390.14714754096, 532584.0619016397, 140151.66806557373, 128875.57219672124, 72511.7142295087, 55823.084852459004]

1 个答案:

答案 0 :(得分:0)

yr_list = [2013, 2014, 2015, 2016, 2017, 2018]
income_list= [3174507.7278688527, 4666571.258098361, 4757771.147540984, 3347104.209836066, 3475079.6604590164, 3282371.708852459]
expense_list = [2957117.5807213113, 4133987.196196721, 4617619.47947541, 3218228.637639344, 3402567.9462295077, 3226548.6240000003]
profit_list = [217390.14714754096, 532584.0619016397, 140151.66806557373, 128875.57219672124, 72511.7142295087, 55823.084852459004]

import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure()
# To get the axis of the figure
ax = fig.add_axes([0,0,1,1])
# Add bar plot
ax.bar(yr_list, income_list, color = 'b', width = 0.25, label='Income')
# Add the second bar plot with a small shift
ax.bar([yr + 0.25 for yr in yr_list], expense_list, color = 'g', width = 0.25, label='Expense')
# Adds line plot
ax.plot(yr_list , profit_list , color = 'y', label='Profit')

plt.legend()

enter image description here