简体   繁体   中英

How to plot multiple lists on the same plot as bar and line plots?

I have 4 lists - yr_list, income_list, expense_list and profit_list.

I would like to show yr_list on x-axis, income_list as bar chart on y-axis, expense_list as bar chart on y-axis and profit_list as line chart on secondary y-axis. How do I do that?

在此处输入图像描述

[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]
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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM