本文转载自斗大的熊猫
简单 Boxplots
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(, std, ) for std in range(, )]
fig = plt.figure(figsize=(,))
plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent
plt.xticks([y+ for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
黑白盒状图
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(, std, ) for std in range(, )]
fig = plt.figure(figsize=(,))
bplot = plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent
plt.xticks([y+ for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
for components in bplot.keys():
for line in bplot[components]:
line.set_color('black') # black lines
t = plt.title('Black and white box plot')
plt.show()
自定义颜色填充盒状图
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(, std, ) for std in range(, )]
fig = plt.figure(figsize=(,))
bplot = plt.boxplot(all_data,
notch=False, # notch shape
vert=True, # vertical box aligmnent
patch_artist=True) # fill with color
colors = ['pink', 'lightblue', 'lightgreen']
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
plt.xticks([y+ for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
小提琴
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(nrows=,ncols=, figsize=(,))
all_data = [np.random.normal(, std, ) for std in range(, )]
#fig = plt.figure(figsize=(8,6))
axes[].violinplot(all_data,
showmeans=False,
showmedians=True
)
axes[].set_title('violin plot')
axes[].boxplot(all_data,
)
axes[].set_title('box plot')
# adding horizontal grid lines
for ax in axes:
ax.yaxis.grid(True)
ax.set_xticks([y+ for y in range(len(all_data))], )
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
plt.setp(axes, xticks=[y+ for y in range(len(all_data))],
xticklabels=['x1', 'x2', 'x3', 'x4'],
)
plt.show()