matplotlib学习笔记5.误差bar以及复杂条形图

import numpy as np
import matplotlib.pyplot as plt
# 不用打imshow也能显示
%matplotlib inline 

误差bar以及复杂条形图

mean_values = [1,2,3]
variance = [0.2,0.4,0.5]
bar_label = ['bar1', 'bar2', 'bar3']

x_pos = list(range(len(bar_label)))
plt.bar(x_pos,mean_values,yerr=variance)
max_y = max(zip(mean_values, variance))
# y轴刻度的大小
plt.ylim([0,(max_y[0]+max_y[1])*1.2])
# x轴的label
plt.xticks(x_pos, bar_label)
([<matplotlib.axis.XTick at 0x242ff375b38>,
  <matplotlib.axis.XTick at 0x242ff375400>,
  <matplotlib.axis.XTick at 0x242ff3752e8>],
 <a list of 3 Text xticklabel objects>)

请添加图片描述

  • 背靠背画法
x1 = np.array([1,2,3])
x2 = np.array([2,2,3])
bar_labels = ['bar1','bar2','bar3']
# 尺寸
fig = plt.figure(figsize=(8,4))
y_pos = np.arange(len(x1))
y_pos = [x for x in y_pos]
plt.barh(y_pos,x1,color='g')
plt.barh(y_pos,-x1,color='r')
<BarContainer object of 3 artists>

请添加图片描述

  • 多个柱子
green_data = [1,2,3]
blue_data = [3,2,2]
red_data = [1,3,3]
labels = ['g1','g2','g3']

pos = list(range(len(green_data)))
width = 0.2
fig, ax = plt.subplots(figsize=(8,6))

# pos其实就是x坐标,width是柱子宽度,所以可以理解了吧
plt.bar(pos,green_data,width,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,color='b',label=labels[1])
plt.bar([p+width*2 for p in pos],red_data,width,color='r',label=labels[2])

<BarContainer object of 3 artists>

请添加图片描述

  • 给柱子加上标
green_data = [1,2,3]
blue_data = [3,2,2]
red_data = [1,3,3]
labels = ['g1','g2','g3']

pos = list(range(len(green_data)))
width = 0.2
fig, ax = plt.subplots(figsize=(8,6))

# pos其实就是x坐标,width是柱子宽度,所以可以理解了吧
plt.bar(pos,green_data,width,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,color='b',label=labels[1])
bars = plt.bar([p+width*2 for p in pos],red_data,width,color='r',label=labels[2])

plt.ylim([0,3.5])
# 加上标
for b,d in zip(bars, red_data):
    plt.text(b.get_x()+b.get_width()*0.05, b.get_y()+b.get_height()*1.04,'{0:.2%}'.format(d/min(red_data)))

请添加图片描述

  • 颜色
mean_values = range(10,18)
x_pos = range(len(mean_values))

import matplotlib.colors as col
import matplotlib.cm as cm

camp1 = cm.ScalarMappable(col.Normalize(min(mean_values), max(mean_values),cm.hot))
camp2 = cm.ScalarMappable(col.Normalize(0,20,cm.hot))
                          
plt.subplot(121)
plt.bar(x_pos,mean_values,color=camp1.to_rgba(mean_values))                          
<BarContainer object of 8 artists>

请添加图片描述

  • 柱状图的形状
patterns = ('-','+','x','\\','*','o','0','.')

fig = plt.gca()

mean_values = range(10,18)
x_pos = range(len(mean_values))

bars = plt.bar(x_pos, mean_values)

for bar, pattern in zip(bars, patterns):
    bar.set_hatch(pattern)

请添加图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值