Python学习笔记(4)--Matplotlib同时画柱状图与折线图

Matplotlib同时画柱状图与折线图

实现功能

  • 柱状折线图共存
  • 中文label
  • 两个y轴,两套尺度
  • 图中有标注

效果展示

这里写图片描述

代码


#python 画柱状图折线图
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick  
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
a=[1228.3,3.38,63.8,0.07,0.16,6.74,1896.18]  #数据
b=[0.12,-12.44,1.82,16.67,6.67,-6.52,4.04]
l=[i for i in range(7)]

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签

fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)  #设置百分比形式的坐标轴
lx=[u'粮食',u'棉花',u'油料',u'麻类',u'糖料',u'烤烟',u'蔬菜']

fig = plt.figure()  
ax1 = fig.add_subplot(111)  
ax1.plot(l, b,'or-',label=u'增长率');
ax1.yaxis.set_major_formatter(yticks)
for i,(_x,_y) in enumerate(zip(l,b)):  
    plt.text(_x,_y,b[i],color='black',fontsize=10,)  #将数值显示在图形上
ax1.legend(loc=1)
ax1.set_ylim([-20, 30]);
ax1.set_ylabel('增长率');
plt.legend(prop={'family':'SimHei','size':8})  #设置中文
ax2 = ax1.twinx() # this is the important function  
plt.bar(l,a,alpha=0.3,color='blue',label=u'产量')  
ax2.legend(loc=2)
ax2.set_ylim([0, 2500])  #设置y轴取值范围
plt.legend(prop={'family':'SimHei','size':8},loc="upper left") 
plt.xticks(l,lx)
plt.show()
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值