Python——柱状图(条形图、堆叠图)

目录

1 基本函数

2 竖条形图

3 横条形图

4 并列条形图

5 添加标签

6 堆叠柱形图


1 基本函数

bar(x, height, [width], **kwargs)   #竖条形图
barh(x, height, [width], **kwargs)  #横条形图

x:数据标签(横坐标);

height:个数或一个数组,条形的高度;

[width]:可选参数,一个数或一个数组,条形的宽度,默认为 0.8

2 竖条形图

import matplotlib.pyplot as plt
# 解决plt中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = ('A', 'B', 'C', 'D', 'E')
y = [1, 2, 3, 4, 5]

plt.bar(x, y)
plt.title('结果')
plt.show()

3 横条形图

import matplotlib.pyplot as plt
# 解决plt中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = ('A', 'B', 'C', 'D', 'E')
y = [1, 2, 3, 4, 5]

plt.barh(x, y)
plt.title('结果')
plt.show()

4 并列条形图

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 数据
x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.2  # 条形宽度
index_y1 = np.arange(len(x))  # y1条形图的横坐标
index_y2 = index_y1 + bar_width  # y2条形图的横坐标

# 使用两次 bar 函数画出两组条形图
plt.bar(index_y1, height=y1, width=bar_width, color='#499c9f', label='y1')
plt.bar(index_y2, height=y2, width=bar_width, color='#c76813', label='y2')

plt.legend()  #图例
plt.xticks(index_y1 + bar_width/2, x)  # 标签+位置
plt.ylabel('数量')  # 纵坐标轴标题
plt.title('结果')  # 图形标题

plt.show()

5 添加标签

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 数据
x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.3  # 条形宽度
index_y1 = np.arange(len(x))  # y1条形图的横坐标
index_y2 = index_y1 + bar_width  # y2条形图的横坐标

# 使用两次 bar 函数画出两组条形图
plt.bar(index_y1, height=y1, width=bar_width, color='#499c9f', label='y1')
plt.bar(index_y2, height=y2, width=bar_width, color='#c76813', label='y2')

index = np.arange(len(y1))
for a,b in zip(index,y1):   #柱子上的数字显示
     plt.text(a*1.02,b*1.02,'%.2f'%b,ha='center',va='bottom',fontsize=7);
for a,b in zip(index+width*3,y2):
     plt.text(a*1.02,b*1.02,'%.2f'%b,ha='center',va='bottom',fontsize=7);

plt.legend()  # 显示图例
plt.xticks(index_y1 + bar_width/2, x)  # 让横坐标轴刻度显示 waters 里的饮用水, index_male + bar_width/2 为横坐标轴刻度的位置
plt.ylabel('数量')  # 纵坐标轴标题
plt.title('结果')  # 图形标题

plt.show()

6 堆叠柱形图

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.3  # 条形宽度

plt.bar(x, y1, bar_width, color = '#A65F58', label = 'y1')
plt.bar(x, y2, bar_width, bottom = y1,           # 堆叠在第一个上方
        color = '#99886B', label = 'y2')

plt.legend()

  • 6
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值