python绘制小提琴图_matplotlib 小提琴图(violin plot)

小提琴图 (Violin Plot) 用于显示数据分布及其概率密度。

这种图表结合了箱形图和密度图的特征,主要用来显示数据的分布形状。中间白点为中位数,中间的黑色粗条表示四分位数范围。上下贯穿小提琴图的黑线代表最小非异常值min到最大非异常值max的区间,线上下端分别代表上限和下限,超出此范围为异常数据。(或者,从黑色粗条延伸的细黑线代表 95% 置信区间)

Matplotlib库中,使用violinplot()函数来绘制小提琴图。

例子1:import matplotlib.pyplot as plt

import numpy as np

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))

# Fixing random state for reproducibility

np.random.seed(19680801)

# generate some random test data

all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]

# plot violin plot

axes[0].violinplot(all_data,showmeans=False,showmedians=True)

axes[0].set_title('Violin plot')

# plot box plot

axes[1].boxplot(all_data)

axes[1].set_title('Box plot')

# adding horizontal grid lines

for ax in axes:

ax.yaxis.grid(True)

ax.set_xticks([y + 1 for y in range(len(all_data))])

ax.set_xlabel('Four separate samples')

ax.set_ylabel('Observed values')

# add x-tick labels

plt.setp(axes, xticks=[y + 1 for y in range(len(all_data))],

xticklabels=['x1', 'x2', 'x3', 'x4'])

plt.show()

例子2:"""

This example demonstrates how to fully customize violin plots.

"""

import matplotlib.pyplot as plt

import numpy as np

def adjacent_values(vals, q1, q3):

upper_adjacent_value = q3 + (q3 - q1) * 1.5

upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])

lower_adjacent_value = q1 - (q3 - q1) * 1.5

lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)

return lower_adjacent_value, upper_adjacent_value

def set_axis_style(ax, labels):

ax.get_xaxis().set_tick_params(direction='out')

ax.xaxis.set_ticks_position('bottom')

ax.set_xticks(np.arange(1, len(labels) + 1))

ax.set_xticklabels(labels)

ax.set_xlim(0.25, len(labels) + 0.75)

ax.set_xlabel('Sample name')

# create test data

np.random.seed(19680801)

data = [sorted(np.random.normal(0, std, 100)) for std in range(1, 5)]

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(9, 4), sharey=True)

ax1.set_title('Default violin plot')

ax1.set_ylabel('Observed values')

ax1.violinplot(data)

ax2.set_title('Customized violin plot')

parts = ax2.violinplot(

data, showmeans=False, showmedians=False,

showextrema=False)

for pc in parts['bodies']:

pc.set_facecolor('#D43F3A')

pc.set_edgecolor('black')

pc.set_alpha(1)

quartile1, medians, quartile3 = np.percentile(data, [25, 50, 75], axis=1)

whiskers = np.array([

adjacent_values(sorted_array, q1, q3)

for sorted_array, q1, q3 in zip(data, quartile1, quartile3)])

whiskersMin, whiskersMax = whiskers[:, 0], whiskers[:, 1]

inds = np.arange(1, len(medians) + 1)

ax2.scatter(inds, medians, marker='o', color='white', s=30, zorder=3)

ax2.vlines(inds, quartile1, quartile3, color='k', linestyle='-', lw=5)

ax2.vlines(inds, whiskersMin, whiskersMax, color='k', linestyle='-', lw=1)

# set style for the axes

labels = ['A', 'B', 'C', 'D']

for ax in [ax1, ax2]:

set_axis_style(ax, labels)

plt.subplots_adjust(bottom=0.15, wspace=0.05)

plt.show()

例子3:import random

import numpy as np

import matplotlib.pyplot as plt

# Fixing random state for reproducibility

np.random.seed(19680801)

# fake data

fs = 10 # fontsize

pos = [1, 2, 4, 5, 7, 8]

data = [np.random.normal(0, std, size=100) for std in pos]

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6))

axes[0, 0].violinplot(data, pos, points=20, widths=0.3,

showmeans=True, showextrema=True, showmedians=True)

axes[0, 0].set_xticks(pos)

axes[0, 0].set_xticklabels(["A","B","C","D","E","F"])

axes[0, 0].set_xlabel('group name')

axes[0, 0].set_title('Custom violinplot 1', fontsize=fs)

axes[0, 1].violinplot(data, pos, points=40, widths=0.5,

showmeans=True, showextrema=True, showmedians=True,

bw_method='silverman')

axes[0, 1].set_title('Custom violinplot 2', fontsize=fs)

axes[0, 2].violinplot(data, pos, points=60, widths=0.7, showmeans=True,

showextrema=True, showmedians=True, bw_method=0.5)

axes[0, 2].set_title('Custom violinplot 3', fontsize=fs)

axes[1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7,

showmeans=True, showextrema=True, showmedians=True)

axes[1, 0].set_title('Custom violinplot 4', fontsize=fs)

axes[1, 1].violinplot(data, pos, points=100, vert=False, widths=0.9,

showmeans=True, showextrema=True, showmedians=True,

bw_method='silverman')

axes[1, 1].set_title('Custom violinplot 5', fontsize=fs)

axes[1, 2].violinplot(data, pos, points=200, vert=False, widths=1.1,

showmeans=True, showextrema=True, showmedians=True,

bw_method=0.5)

axes[1, 2].set_title('Custom violinplot 6', fontsize=fs)

#for ax in axes.flatten():

#ax.set_yticklabels([])

fig.suptitle("Violin Plotting Examples")

fig.subplots_adjust(hspace=0.4)

plt.show()

你可能感兴趣的:(python绘制小提琴图)