Python 堆叠柱状图绘制方法
作者:刘六六 时间:2022-05-15 01:39:00
本文介绍了Python 堆叠柱状图绘制方法,分享给大家,具体如下:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>文件: 堆叠直方图.py
>>作者: liu yang
>>邮箱: liuyang0001@outlook.com
>>博客: www.cnblogs.com/liu66blog
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib
# 定义要使用的字体,防止出现中文乱码
font=matplotlib.font_manager.FontProperties(fname=r"C:\Windows\Fonts\Deng.ttf")
# 双层直方图,上下结构 适用于一个柱状图全部高于另一组
def barsplot():
# 先生成一个画布
fig=plt.figure()
# 生成数据
x1=[x for x in range(1,9)]
y1=[n*2 for n in range(1,9)]
x2=[x for x in range(1,9)]
y2=[x**2 for x in x2]
# 开始画条形图2,先画数值大的,数值小的直接在原图覆盖
l2=plt.bar(x2,y2,color='b',width=0.4)
# 开始画条形图1
l1=plt.bar(x1,y1,color='g',width=0.4)
# 设置x标签
plt.xlabel(u'x轴',fontproperties=font)
# 设置y轴标签
plt.ylabel('y轴',fontproperties=font)
# 设置标题
plt.title(u'堆叠柱状图',fontproperties=font)
# 设置注解狂
plt.legend(handles = [l1, l2,], labels = ['去年', '今年'], loc = 'best',prop=font)
# 把确切数字显示出来
for x1,x2, y1, y2 in zip(x1,x2, y1, y2):
plt.text(x1 , y1, '%.0f' % y1, ha='center', va='bottom')
plt.text(x2 , y2, '%.0f' % y2, ha='center', va='bottom')
# 显示
plt.show()
# 如果最为主模块运行
if __name__ == '__main__':
# 实例化
ba=barsplot()
来源:https://www.cnblogs.com/liu66blog/p/8486109.html
标签:Python,堆叠,柱状图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
numpy中的随机打乱数据方法np.random.shuffle解读
2023-05-19 08:26:53
实例讲解Python爬取网页数据
2023-01-10 03:55:05
Sublime Text4 配置 Python3 环境、代码提示、编译报错的解决方案
2021-09-24 12:00:03
![](https://img.aspxhome.com/file/2023/4/72074_0s.png)
Pycharm中import torch报错的快速解决方法
2022-03-11 05:23:26
![](https://img.aspxhome.com/file/2023/0/101630_0s.jpg)
flask框架路由常用定义方式总结
2021-04-18 22:12:29
使用Python遍历文件夹实现查找指定文件夹
2021-01-19 09:23:06
![](https://img.aspxhome.com/file/2023/4/86244_0s.png)
python+logging+yaml实现日志分割
2023-12-18 01:19:08
![](https://img.aspxhome.com/file/2023/4/69514_0s.jpg)
Django中ORM找出内容不为空的数据实例
2022-03-24 04:47:56
Python序列化基础知识(json/pickle)
2021-04-24 01:09:34
jquery的$(document).ready()和onload的加载顺序
2023-08-23 18:57:40
python爬虫爬取图片的简单代码
2021-08-04 10:19:27
python属于哪种语言
2022-05-30 18:20:07
对python指数、幂数拟合curve_fit详解
2022-12-03 16:28:52
![](https://img.aspxhome.com/file/2023/9/95269_0s.jpg)
Javascript与PHP验证用户输入URL地址是否正确
2023-10-23 02:36:07
ASP中DLL的调试环境配置全攻略
2007-09-27 13:20:00
介绍Python的Urllib库的一些高级用法
2023-03-29 08:28:06
![](https://img.aspxhome.com/file/2023/5/93825_0s.png)
浅谈PyQt5中异步刷新UI和Python多线程总结
2023-08-22 01:37:18
MySQL的root帐户密码重置方法
2007-08-24 15:53:00
python plotly画柱状图代码实例
2023-11-29 13:41:31
![](https://img.aspxhome.com/file/2023/2/92392_0s.png)
Python读取yaml文件的详细教程
2021-03-16 20:43:27
![](https://img.aspxhome.com/file/2023/9/65959_0s.png)