# Librariesimport numpy as np
import matplotlib.pyplot as plt
# Create dataset
height =[3,12,5,18,45]
bars =('A','B','C','D','E')
x_pos = np.arange(len(bars))# Create bars
plt.bar(x_pos, height)# Create names on the x-axis
plt.xticks(x_pos, bars)# Show graphic
plt.show()
二. 三色柱形图
# librariesimport numpy as np
import matplotlib.pyplot as plt
# set width of bars
barWidth =0.25# set heights of bars
bars1 =[12,30,1,8,22]
bars2 =[28,6,16,5,10]
bars3 =[29,3,24,25,17]# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 =[x + barWidth for x in r1]
r3 =[x + barWidth for x in r2]# Make the plot
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r inrange(len(bars1))],['A','B','C','D','E'])# Create legend & Show graphic
plt.legend()
plt.show()
三. 叠加柱形图
# librariesimport numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
# y-axis in bold
rc('font', weight='bold')# Values of each group
bars1 =[12,28,1,8,22]
bars2 =[28,7,16,4,10]
bars3 =[25,3,23,25,17]# Heights of bars1 + bars2
bars = np.add(bars1, bars2).tolist()# The position of the bars on the x-axis
r =[0,1,2,3,4]# Names of group and bar width
names =['A','B','C','D','E']
barWidth =1# Create brown bars
plt.bar(r, bars1, color='#7f6d5f', edgecolor='white', width=barWidth)# Create green bars (middle), on top of the first ones
plt.bar(r, bars2, bottom=bars1, color='#557f2d', edgecolor='white', width=barWidth)# Create green bars (top)
plt.bar(r, bars3, bottom=bars, color='#2d7f5e', edgecolor='white', width=barWidth)# Custom X axis
plt.xticks(r, names, fontweight='bold')
plt.xlabel("group")# Show graphic
plt.show()
在这里插入图片描述
四. 叠加百分比柱形图
# librariesimport numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
# Data
r =[0,1,2,3,4]
raw_data ={
'greenBars':[20,1.5,7,10,5],'orangeBars':[5,15,5,10,15],'blueBars':[2,15,18,5,10]}
df = pd.DataFrame(raw_data)# From raw value to percentage
totals =[i+j+k for i,j,k inzip(df['greenBars'], df['orangeBars'], df['blueBars'])]
greenBars =[i / j *100for i,j inzip(df['greenBars'], totals)]
orangeBars =[i / j *100for i,j inzip(df['orangeBars'], totals)]
blueBars =[i / j *100for i,j inzip(df['blueBars'], totals)]# plot
barWidth =0.85
names =('A','B','C','D','E')# Create green Bars
plt.bar(r, greenBars, color='#b5ffb9', edgecolor='white', width=barWidth)# Create orange Bars
plt.bar(r, orangeBars, bottom=greenBars, color='#f9bc86', edgecolor='white', width=barWidth)# Create blue Bars
plt.bar(r, blueBars, bottom=[i+j for i,j inzip(greenBars, orangeBars)], color='#a3acff', edgecolor='white', width=barWidth)# Custom x axis
plt.xticks(r, names)
plt.xlabel("group")# Show graphic
plt.show()
五. 叠加百分比柱形图
# libraries & datasetimport seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above)
sns.set(style="darkgrid")
df = sns.load_dataset("iris")
sns.histplot(data=df, y="sepal_length")
plt.show()
六. 紫色柱形图
# librariesimport numpy as np
import matplotlib.pyplot as plt
# create dataset
height =[3,12,5,18,45]
bars =('A','B','C','D','E')
x_pos = np.arange(len(bars))# Create bars and choose color
plt.bar(x_pos, height, color =(0.5,0.1,0.5,0.6))# Add title and axis names
plt.title('My title')
plt.xlabel('categories')
plt.ylabel('values')# Create names on the x axis
plt.xticks(x_pos, bars)# Show graph
plt.show()
七.带条纹的条形图
# Librariesimport numpy as np
import matplotlib.pyplot as plt
# Create dataset
height =[2,5,4,6]
bars =('A','B','C','D')
x_pos = np.arange(len(bars))# Create bars
figure = plt.bar(x_pos, height)# Define some hatches
hatches =['-','/','||','///']# Loop over bars and assign hatchesfor bar, hatch inzip(figure, hatches):
bar.set_hatch(hatch)# Create names on the x-axis
plt.xticks(x_pos, bars)# Show graphic
plt.show()
八、带标签的不同色条形图
# librariesimport numpy as np
import matplotlib.pyplot as plt
# width of the bars
barWidth =0.3# Choose the height of the blue bars
bars1 =[10,9,2]# Choose the height of the cyan bars
bars2 =[10.8,9.5,4.5]# Choose the height of the error bars (bars1)
yer1 =[0.5,0.4,0.5]# Choose the height of the error bars (bars2)
yer2 =[1,0.7,1]# The x position of bars
r1 = np.arange(len(bars1))
r2 =[x + barWidth for x in r1]# Create blue bars
plt.bar(r1, bars1, width = barWidth, color ='blue', edgecolor ='black', yerr=yer1, capsize=7, label='poacee')# Create cyan bars
plt.bar(r2, bars2, width = barWidth, color ='cyan', edgecolor ='black', yerr=yer2, capsize=7, label='sorgho')# general layout
plt.xticks([r + barWidth for r inrange(len(bars1))],['cond_A','cond_B','cond_C'])
plt.ylabel('height')
plt.legend()# Show graphic
plt.show()
九、详细的条形图
# libraryimport matplotlib.pyplot as plt
# Create bars
barWidth =0.9
bars1 =[3,3,1]
bars2 =[4,2,3]
bars3 =[4,6,7,10,4,4]
bars4 = bars1 + bars2 + bars3
# The X position of bars
r1 =