python画条形图实例

作者:水泽木兰@野蛮生长 时间:2023-12-04 12:32:33 

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

在做毕设的过程中有些数据用表格来展现,会很难看出数据之间的差别,凸显不出数据的特点,所以想制作一个条形图,这里特地记录下,已备以后用到。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
 
c101= (8,7,3,7,10,4,11,8,11,10,8,9)
c102= (21,21,24,25,16,17,11,24,24,25,36,33)
c103= (4,4,10,3,9,8,12,2,4,5,2,6)
c104= (1,5,1,3,2,3,3,3,6,5,1,0)
c105= (3,1,0,1,3,3,1,2,0,0,0,0)
c106= (1,2,0,0,1,1,0,0,1,1,0,0)
c107= (1,0,1,0,0,1,0,0,0,0,0,0)
c108= (0,1,0,0,0,2,1,1,0,1,0,0)
c109= (1,0,1,1,0,0,1,1,0,0,0,0)
 
ind = np.arange(0,24,2) # the x locations for the groups
width = 0.2  # the width of the bars
 
fig,ax = plt.subplots()
rects1 = ax.bar(ind + width, c101, width, color='SkyBlue',align='edge', label='101')
rects2 = ax.bar(ind + 2*width, c102, width,color='IndianRed',align='edge', label='102')
rects3 = ax.bar(ind + 3*width, c103, width, color='Cyan',align='edge', label='103')
rects4 = ax.bar(ind + 4*width, c104, width, color='Magenta',align='edge', label='104')
rects5 = ax.bar(ind + 5*width, c105, width, color='Purple',align='edge', label='105')
rects6 = ax.bar(ind + 6*width, c106, width, color='Green',align='edge', label='106')
rects7 = ax.bar(ind + 7*width, c107, width, color='Yellow',align='edge', label='107')
rects8 = ax.bar(ind + 8*width, c108, width, color='Blue',align='edge', label='108')
rects9 = ax.bar(ind + 9*width, c109, width, color='Orange',align='edge', label='109')
 
# Add some text for labels, title and custom x-axis tick labels, etc.

#ax.set_title('Scores by group and gender')
plt.xticks(ind,('1班', '2班', '3班', '4班', '5班','6班','7班','8班','9班','10班','11班','12班'))
ax.legend(loc='upper center')
 
plt.show()
fig.savefig('./test77.jpg')

python画条形图实例

竖起来的

%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
#指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
#解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False
data = [[8, 21, 4, 1, 3, 1, 1, 0, 1],
        [7,21,4,5,1,2,0,1,0], 
        [3,24,10,1,0,0,1,0,1],
        [7,25,3,3,1,0,0,0,1], 
        [10,16,9,2,3,1,0,0,0], 
        [4,17,8,3,3,1,1,2,0], 
        [11,11,12,3,1,0,0,1,1], 
        [8,24,2,3,2,0,0,1,1], 
        [11,24,4,6,0,1,0,0,0], 
        [10,25,5,5,0,1,0,1,0], 
        [8,36,2,1,0,0,0,0], 
        [9,33,6,0,0,0,0,0]]
df = pd.DataFrame(data,
                 index=['1班','2班','3班','4班','5班','6班','7班','8班','9班','10班','11班','12班'],
                 columns=pd.Index(['101','102','103','104','105','106','107','108','109']),
                 )

df.plot(kind='barh',figsize=(5,8)) 

plt.show()
#fig.savefig('./test2.jpg')

python画条形图实例

来源:https://blog.csdn.net/yl_mouse/article/details/89152252

标签:python,条形图
0
投稿

猜你喜欢

  • 浅谈Python使用Bottle来提供一个简单的web服务

    2021-08-30 13:03:19
  • Python兔子毒药问题实例分析

    2023-11-14 11:23:56
  • MySQL索引最左匹配原则实例详解

    2024-01-27 18:38:05
  • SQL Server高级内容之子查询和表链接概述及使用

    2024-01-24 22:34:27
  • 图文详解vscode配置运行php项目完整版

    2023-05-28 22:56:03
  • Python数学建模PuLP库线性规划进阶基于字典详解

    2022-03-11 18:04:04
  • vue实现选择商品规格功能

    2024-05-13 09:37:55
  • 关于scipy.optimize函数使用及说明

    2022-10-19 04:24:04
  • 详解如何利用amoeba(变形虫)实现mysql数据库读写分离

    2024-01-18 12:03:05
  • python sys模块使用方法介绍

    2021-02-03 09:19:16
  • python实现异常信息堆栈输出到日志文件

    2022-04-26 20:26:21
  • Python+OpenCV之图像轮廓详解

    2023-08-10 18:59:42
  • [ASP]利用 xmlhttp 分块上传文件

    2008-07-04 14:14:00
  • MySQL 常用引擎总结分享

    2024-01-27 04:05:28
  • python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)

    2022-06-12 23:09:48
  • 怎样用cmd命令行运行Python文件

    2023-07-15 00:25:11
  • Mysql8断电崩溃解决

    2024-01-25 19:12:02
  • 在Windows系统上搭建Nginx+Python+MySQL环境的教程

    2024-01-24 08:04:40
  • Python实现识别图片为文字的示例代码

    2022-01-02 14:57:48
  • asp任何连接带密码的access数据库

    2008-04-29 21:36:00
  • asp之家 网络编程 m.aspxhome.com