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)
([,
,
],
)
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')
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])
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))
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)