python绘制柱形图的方法

作者:Wayne0926 时间:2022-03-07 07:36:06 

本文实例为大家分享了python绘制柱形图的具体代码,供大家参考,具体内容如下

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

结果为:

python绘制柱形图的方法

#横向柱形图
result_asd=result_columns.sort_values(
        by='月总消费',
        ascending=False)
plt.barh(index,
        result_asd.月总消费,
        color=maincolor)
plt.yticks(index,result_asd.手机品牌)
plt.show()

结果为:

python绘制柱形图的方法

#计算出交叉表的数据
result=data_columns.pivot_table(
        values='月消费(元)',
        index='手机品牌',
        columns='通信品牌',
        aggfunc=numpy.sum)

结果为:

python绘制柱形图的方法

#定义三个颜色
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()

结果为:

python绘制柱形图的方法

#重新排序进行绘制
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()

结果为:

python绘制柱形图的方法

#绘制堆叠柱形图
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()

结果为:

python绘制柱形图的方法

#绘制双向柱形图
plt.barh(
        index,
        result['神州行'],
        color=midcolor)
plt.barh(
        index,
        -result['动感地带'],
        color=maxcolor)
plt.yticks(index,
           result.index)
plt.legend(['动感地带','神州行'])
plt.show()

结果为:

python绘制柱形图的方法

来源:https://blog.csdn.net/Wayne0926/article/details/96993043

标签:python,柱形图
0
投稿

猜你喜欢

  • python实现图片,视频人脸识别(opencv版)

    2023-03-14 12:41:07
  • 使用Python开发SQLite代理服务器的方法

    2021-01-27 13:10:31
  • Python 详解通过Scrapy框架实现爬取百度新冠疫情数据流程

    2023-04-03 12:13:33
  • Python变量和数据类型和数据类型的转换

    2023-09-28 07:44:34
  • Python实现快速排序算法及去重的快速排序的简单示例

    2021-06-02 19:58:09
  • 给SQL Server传送数组参数的变通办法

    2008-11-25 11:39:00
  • Python实现LRU算法的2种方法

    2021-10-19 11:30:32
  • python实现的希尔排序算法实例

    2023-03-27 05:01:38
  • Python利用PyExecJS库执行JS函数的案例分析

    2022-10-26 08:53:19
  • IE下img多余5像素空白

    2009-06-08 13:17:00
  • Python浮点型(float)运算结果不正确的解决方案

    2023-10-04 16:57:44
  • python处理RSTP视频流过程解析

    2023-11-22 00:54:55
  • Python实现复杂对象转JSON的方法示例

    2021-11-14 09:55:47
  • Django权限控制的使用

    2023-12-27 01:50:57
  • Python批量启动多线程代码实例

    2021-09-01 04:41:16
  • Python分析彩票记录并预测中奖号码过程详解

    2023-07-20 04:49:18
  • PHP连接MSSQL方法汇总

    2023-11-17 19:34:36
  • pytorch中dataloader 的sampler 参数详解

    2023-09-16 21:00:13
  • 如何前后翻阅聊友们的发言?

    2010-01-18 20:49:00
  • php面象对象数据库操作类实例

    2023-10-13 22:33:05
  • asp之家 网络编程 m.aspxhome.com