python绘制柱形图的方法
作者:Wayne0926 发布时间:2022-03-07 07:36:06
标签:python,柱形图
本文实例为大家分享了python绘制柱形图的具体代码,供大家参考,具体内容如下
#柱形图
import pandas
import numpy
import matplotlib
from matplotlib import pyplot as plt
#导入数据
data_columns=pandas.read_csv('D://Python projects//reference data//6.4//data.csv')
#定义中文格式
font={'family':'MicroSoft Yahei',
'weight':'bold',
'size':12}
matplotlib.rc('font',**font)
#使用手机品牌作为分组列,月消费作为统计列
result_columns=data_columns.groupby(
by=['手机品牌'],
as_index=False)['月消费(元)'
].agg({'月总消费':numpy.sum
})
#生成一个间隔为1的序列
index=numpy.arange(result_columns.月总消费.size)
#绘制纵向柱形图
plt.bar(index,result_columns['月总消费'])
#%matplotlib qt
plt.show()
#配置颜色
maincolor=(42/256,87/256,141/256,1)
plt.bar(index,
result_columns['月总消费'])
plt.show()
#配置X轴标签
plt.bar(index,
result_columns['月总消费'])
plt.xticks(index,result_columns.手机品牌)
plt.show()
#对数据进行降序排序后展示
result_asd=result_columns.sort_values(
by='月总消费',
ascending=False)
plt.bar(index,
result_asd.月总消费,
color=maincolor)
plt.xticks(index,result_asd.手机品牌)
plt.show()
结果为:
#横向柱形图
result_asd=result_columns.sort_values(
by='月总消费',
ascending=False)
plt.barh(index,
result_asd.月总消费,
color=maincolor)
plt.yticks(index,result_asd.手机品牌)
plt.show()
结果为:
#计算出交叉表的数据
result=data_columns.pivot_table(
values='月消费(元)',
index='手机品牌',
columns='通信品牌',
aggfunc=numpy.sum)
结果为:
#定义三个颜色
index=numpy.arange(len(result))
mincolor=(42/256,87/256,141/256,1/3)
midcolor=(42/256,87/256,141/256,2/3)
maxcolor=(42/256,87/256,141/256,1)
#建立簇状柱形图
plt.bar(
index,
result['全球通'],
color=mincolor,
width=1/4)
plt.bar(
index+1/4,
result['动感地带'],
color=midcolor,
width=1/4)
plt.bar(
index+1/2,
result['神州行'],
color=maxcolor,
width=1/4)
plt.xticks(index+1/3,result.index)
#添加图例
plt.legend(['全球通','动感地带','神州行'])
plt.show()
结果为:
#重新排序进行绘制
result=result.sort_values(
by='神州行',
ascending=False)
plt.bar(
index,
result['全球通'],
color=mincolor,
width=1/4)
plt.bar(
index+1/4,
result['动感地带'],
color=midcolor,
width=1/4)
plt.bar(
index+1/2,
result['神州行'],
color=maxcolor,
width=1/4)
plt.xticks(index+1/3,result.index)
plt.legend(['全球通','动感地带','神州行'])
plt.show()
结果为:
#绘制堆叠柱形图
result=result.sort_values(
by='神州行',
ascending=False)
plt.bar(
index,
result['全球通'],
color=maxcolor)
plt.bar(
index,
result['动感地带'],
bottom=result['全球通'],
color=midcolor)
plt.bar(
index,
result['神州行'],
bottom=result['全球通']+result['动感地带'],
color=mincolor)
plt.xticks(index,result.index)
plt.legend(['全球通','动感地带','神州行'])
plt.show()
结果为:
#绘制双向柱形图
plt.barh(
index,
result['神州行'],
color=midcolor)
plt.barh(
index,
-result['动感地带'],
color=maxcolor)
plt.yticks(index,
result.index)
plt.legend(['动感地带','神州行'])
plt.show()
结果为:
来源:https://blog.csdn.net/Wayne0926/article/details/96993043
0
投稿
猜你喜欢
- 本文介绍了python同时替换多个字符串方法示例,分享给大家,具体如下:import rewords = ''' &
- 今天用numpy 的linalg.det()求矩阵的逆的过程中出现了一个错误:TypeError: No loop matching the
- 本文实例为大家分享了python微信跳一跳的具体代码,供大家参考,具体内容如下部分代码分享:wechat_jump.pyfrom __fut
- 我就废话不多说了,直接上代码吧!import torchimport torch.nn.functional as Fimport nump
- 通常我们在制作上图的时候,会分别给四个div加上不同的css属性,来实现中间间隔。但我们更希望的是不需要对html标签做标识,直接能通过cs
- MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(
- BLOG阅读:http://www.planabc.net/article.asp?id=118在使用CSS实现表现的时候,会经常接触到di
- 常用的网站性能测试指标有:并发数、响应时间、吞吐量、性能计数器等。1、并发数并发数是指系统同时能处理的请求数量,这个也是反应了系统的负载能力
- 本文实例讲述了Python装饰器。分享给大家供大家参考。具体分析如下:这是在Python学习小组上介绍的内容,现学现卖、多练习是好的学习方式
- Python获取电脑截图有多种方式,具体如下:PIL中的ImageGrab模块windows APIPyQtpyautoguiPIL中的Im
- 简介最近在整理我们项目代码的时候,发现有很多活动的代码在结构和提供的功能上都非常相似。为了方便今后的开发,我花了一点时间编写了一个生成代码框
- 好几年前我在做一些自动化的脚本时,脑子里也闪过这样的想法:能不能直接把浏览器的cookies取出来用呢?直到昨天看到代码《python模拟发
- 有过相关经验的朋友都知道,Jscript的效率毕竟有限,在数组中查找数据时如果用常规的算法来做执行起来会很慢。例如在一个含500个字符串的d
- 字符串是Python中最常用的数据类型1、创建字符串1.1 使用 ’ ’ 或 " &quo
- 本文实例讲述了PHP基于phpqrcode生成带LOGO图像的二维码。分享给大家供大家参考。具体如下:这里PHP使用phpqrcode生成带
- 本文实例讲述了python文件读写操作与linux shell变量命令交互执行的方法。分享给大家供大家参考。具体如下:python对文件的读
- 什么是CSS Sprites?“Sprite”(精灵)这个词在计算机图形学中有它独特的定义,由于游戏、视频等画质越来越高,必须有一种技术可以
- 最近公司有一个日文项目,由于之前使用的是自己开发的中文CMS,并没有将语言包分离,在网站建设调试过程中出现了令人头疼的乱码问题。乱码出现的原
- 1、简单应用代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @File : jieba
- https://discuss.pytorch.org/t/how-to-modify-the-final-fc-layer-based-o