matplotlib条形图


条形图

一、简单的条形图

1.用jupyter实现

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
#使得随机数据可预测,即设置相同的seed,每次生成的随机数相同
np.random.seed(0)
#一个参数时,参数值为终点
x=np.arange(5)
#从标准正态分布中返回随机样本,五个条形的高度
y=np.random.randint(-5,5,5)
print(y)
#ncols为列的数量,画子图,fig相当于figure在其上面创建图,axes为轴
fig,axes=plt.subplots(ncols=2)
#竖值条形图,v是竖着的
v_bars=axes[0].bar(x,y,color='red')
#水平条形图,h是横着的
h_bars=axes[1].barh(x,y,color='red')
#给表中加上横线
axes[0].axhline(0,color='grey',linewidth=2)
axes[1].axvline(0,color='grey',linewidth=2)
plt.show()

在这里插入图片描述

2.pycharm

import numpy as np
import matplotlib.pyplot as plt
#使得随机数据可预测,即设置相同的seed,每次生成的随机数相同
np.random.seed(0)
#一个参数时,参数值为终点
x=np.arange(5)
#从标准正态分布中返回随机样本
y=np.random.randn(5)
fig,axes=plt.subplots(ncols=2)
v_bars=axes[0].bar(x,y,color='red')
h_bars=axes[1].barh(x,y,color='red')
plt.show()

在这里插入图片描述

二、对条形图进行颜色区分

1.jupyter

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
fig,ax=plt.subplots()
#设置一个竖值条形图,颜色为浅蓝
v_bars=ax.bar(x,y,color='lightblue')
#对条形图和y值进行遍历,如果在y=0下面则设置成绿色
for bar,height in zip(v_bars,y):
    if height<0:
        bar.set(color='green',linewidth=3)
plt.show()

在这里插入图片描述

2.pycharm

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
#一个参数时,参数值为终点
x=np.arange(5)
#从标准正态分布中返回随机样本
y=np.random.randint(-5,5,5)
fig,ax=plt.subplots()
#设置一个竖值条形图,颜色为浅蓝
v_bars=ax.bar(x,y,color='lightblue')
#对条形图和y值进行遍历,如果在y=0下面则设置成绿色
for bar,height in zip(v_bars,y):
    if height<0:
        bar.set(color='green',linewidth=3)
plt.show()

在这里插入图片描述

三、对折线图进行填充

1.jupyter

样例一

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
#cumsum对其进行求和
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()

在这里插入图片描述
样例二

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
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')
ax.plot(x,y_mean,color='black')
plt.show()

在这里插入图片描述

2.pycharm

样例一

import numpy as np
import matplotlib.pyplot as plt
#cumsum对其进行求和
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()

在这里插入图片描述
样例二

import numpy as np
import matplotlib.pyplot as plt
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')
ax.plot(x,y_mean,color='black')
plt.show()

在这里插入图片描述

四、条形图细节

1.jupyter

样例一

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
#要画的三个指标
mean_values=[1,2,3]
#误差范围
variance=[0.2,0.4,0.5]
#三个柱名字
bar_label=['bar1','bar2','bar3']
fig,ax=plt.subplots()
#在x上的位置
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()

在这里插入图片描述
样例二

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
#柱子的长度
x1=np.array([1,2,3])
x2=np.array([2,2,3])
bar_labels=['bar1','bar2','bar3']
#图的宽,高
fig=plt.figure(figsize=(8,6))
#三个值对应的位置
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='b',alpha=0.5)
plt.xlim(-max(x2)-1,max(x1)+1)
plt.ylim(-1,len(x1)+1)
plt.show()

在这里插入图片描述
样例三

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
green_data=[1,2,3]
blue_data=[3,2,1]
red_data=[2,3,3]
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()

在这里插入图片描述

2.pycharm

样例一

import matplotlib.pyplot as plt
#要画的三个指标
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))
plt.ylim([0,(max_y[0]+max_y[1])*1.2])
plt.ylabel('variable y')
plt.xticks(x_pos,bar_label)
plt.show()

在这里插入图片描述
样例二

import numpy as np
import matplotlib.pyplot as plt
#柱子的长度
x1=np.array([1,2,3])
x2=np.array([2,2,3])
bar_labels=['bar1','bar2','bar3']
#图的宽,高
fig=plt.figure(figsize=(8,6))
#三个值对应的位置
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='b',alpha=0.5)
plt.xlim(-max(x2)-1,max(x1)+1)
plt.ylim(-1,len(x1)+1)
plt.show()

在这里插入图片描述
样例三

import matplotlib.pyplot as plt
green_data=[1,2,3]
blue_data=[3,2,1]
red_data=[2,3,3]
labels=['group 1','group 2','groups 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()

在这里插入图片描述

五、条形图外观

1.pycharm

import matplotlib.pyplot as plt
mean_values=range(10,18)
x_pos=range(len(mean_values))
import matplotlib.colors as col
import matplotlib.cm as 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()

在这里插入图片描述

2.jupyter

import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
patterns=('-','+','x','\\','*','o','O','.')
fig=plt.gca()
mean_value=range(1,len(patterns)+1)
fig,ax=plt.subplots()
x_pos=list(range(len(mean_value)))
bars=plt.bar(x_pos,mean_value,color='white')
for bar,pattern in zip(bars,patterns):
    bar.set_hatch(pattern)
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anan.3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值