Python科学计算之Matplotlib条形图

条形图的多种画法

#导入画图所需要的包
import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
%matplotlib inline
  • 画一个简单的条形图
#使用一个随机种子
np.random.seed(0)
x = np.arange(5)
y = np.random.randint(-5,5,5)
print (y)
#定义划分为子图
fig,axes = plt.subplots(ncols = 2)
#竖条形
v_bars = axes[0].bar(x,y,color='red')
#横条形
h_bars = axes[1].barh(x,y,color='blue')
#横着加一条线,因为图是竖条形
axes[0].axhline(0,color='green',linewidth=2)
#竖着加一条线,因为图是横条形
axes[1].axvline(0,color='green',linewidth=2)
plt.xkcd()

在这里插入图片描述

  • 区分颜色
fig,ax = plt.subplots()
v_bars = ax.bar(x,y,color='lightblue')
#大于0的为北卡蓝,小于0的为红色
for bar,height in zip(v_bars,y):
    if height < 0:
        bar.set(edgecolor = 'darkred',color = 'red',linewidth = 3)
plt.show()

在这里插入图片描述

  • 填充条形图
#x设为100个数的累加和
x = np.random.randn(100).cumsum()
y = np.linspace(0,10,100)

fig,ax = plt.subplots()
#填充颜色
ax.fill_between(x,y,color='lightblue')
plt.show()

在这里插入图片描述

x = np.linspace(0,10,200)
y1 = 2*x +1
y2 = 3*x +1.2
y_mean = 0.5*x*np.cos(2*x) + 2.5*x +1.1
fig,ax = plt.subplots()
#填充
ax.fill_between(x,y1,y2,color='red')
#画一条y_mean的线
ax.plot(x,y_mean,color='yellow')
plt.show()

在这里插入图片描述

条形图的细节操作

#指定三个指标
mean_values = [1,2,3]
#误差范围
variance = [0.2,0.4,0.5]
#标签
bar_label = ['bar1','bar2','bar3']
#设定位置
x_pos = list(range(len(bar_label)))
#设置误差棒yerr
plt.bar(x_pos,mean_values,yerr=variance,alpha=0.3)
#设置最大高度
max_y = max(zip(mean_values,variance))
#限制y轴
plt.ylim([0,(max_y[0]+max_y[1])*1.2])
plt.ylabel('variable y')
#x轴标签位置
plt.xticks(x_pos,bar_label)
plt.show()

在这里插入图片描述

  • 设置两个指标
x1 = np.array([1,2,3])
x2 = np.array([3,2,1])
#指定标签
bar_labels = ['bar1','bar2','bar3']
#设置窗口
fig = plt.figure(figsize = (8,6))
#设置y轴位置
y_pos = np.arange(len(x1))
y_pos = [x for x in y_pos]
#横着画条形图
plt.barh(y_pos,x1,color='g',alpha = 0.5)
plt.barh(y_pos,-x2,color='r',alpha = 0.5)
#限制x,y轴
plt.xlim(-max(x2)-1,max(x1)+1)
plt.ylim(-1,len(x1)+1)
plt.show()

在这里插入图片描述

green_data = [1, 2, 3]
blue_data = [3, 1, 2]
red_data = [2, 3, 1]
labels = ['group 1', 'group 2', 'group 3']

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

plt.bar(pos,green_data,width,alpha=0.5,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,alpha=0.5,color='b',label=labels[1])
plt.bar([p+width*2 for p in pos],red_data,width,alpha=0.5,color='r',label=labels[2])

plt.show()

在这里插入图片描述

data = range(200, 225, 5)
#设定标签
bar_labels = ['a', 'b', 'c', 'd', 'e']
#设定窗口大小
fig = plt.figure(figsize=(10,8))
#设定位置
y_pos = np.arange(len(data))
#指定y轴标签位置
plt.yticks(y_pos, bar_labels, fontsize=16)

bars = plt.barh(y_pos,data,alpha = 0.3,color='g')
#画一条竖线(linestyle指定为虚线)
plt.vlines(min(data),-1,len(data)+0.5,linestyle = 'dashed')
#添加数值标签,用data自身值除以最小的data。
for b,d in zip(bars,data):    plt.text(b.get_width()+b.get_width()*0.05,b.get_y()+b.get_height()/2,'{0:.2%}'.format(d/min(data)))

在这里插入图片描述

条形图外观设置

自定义颜色库Cmap

mean_values = range(10,18)
x_pos = range(len(mean_values))
#导入colormap(cm)库
import matplotlib.colors as col
import matplotlib.cm as cm
#设置cm颜色,颜色完全取决于cm库
cmap1 = cm.ScalarMappable(col.Normalize(min(mean_values),max(mean_values),cm.hot))
cmap2 = cm.ScalarMappable(col.Normalize(0,20,cm.hot))
plt.subplot(121)
plt.bar(x_pos,mean_values,color = cmap1.to_rgba(mean_values))
plt.subplot(122)
plt.bar(x_pos,mean_values,color = cmap2.to_rgba(mean_values))
plt.show()

自定义字符填充

#设置字符填充可以设置+++来表示字符密集程度
patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.')
#gca表示调用\获得对象
fig = plt.gca()
mean_value = range(1,len(patterns)+1)
x_pos = list(range(len(mean_value)))
bars = plt.bar(x_pos,mean_value,color='lightblue')
for bar,pattern in zip(bars,patterns):
    bar.set_hatch(pattern)
plt.show()

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值