mirror of
https://github.com/Estom/notes.git
synced 2026-04-02 10:30:28 +08:00
matplotlib & pandas
This commit is contained in:
213
Python/matplotlab/gallery/statistics/barchart_demo.md
Normal file
213
Python/matplotlab/gallery/statistics/barchart_demo.md
Normal file
@@ -0,0 +1,213 @@
|
||||
# 条形图演示
|
||||
|
||||
使用Matplotlib的许多形状和大小的条形图。
|
||||
|
||||
条形图对于可视化计数或带有误差栏的汇总统计信息非常有用。这些示例显示了使用Matplotlib执行此操作的几种方法。
|
||||
|
||||
```python
|
||||
# Credit: Josh Hemann
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.ticker import MaxNLocator
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
n_groups = 5
|
||||
|
||||
means_men = (20, 35, 30, 35, 27)
|
||||
std_men = (2, 3, 4, 1, 2)
|
||||
|
||||
means_women = (25, 32, 34, 20, 25)
|
||||
std_women = (3, 5, 2, 3, 3)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
index = np.arange(n_groups)
|
||||
bar_width = 0.35
|
||||
|
||||
opacity = 0.4
|
||||
error_config = {'ecolor': '0.3'}
|
||||
|
||||
rects1 = ax.bar(index, means_men, bar_width,
|
||||
alpha=opacity, color='b',
|
||||
yerr=std_men, error_kw=error_config,
|
||||
label='Men')
|
||||
|
||||
rects2 = ax.bar(index + bar_width, means_women, bar_width,
|
||||
alpha=opacity, color='r',
|
||||
yerr=std_women, error_kw=error_config,
|
||||
label='Women')
|
||||
|
||||
ax.set_xlabel('Group')
|
||||
ax.set_ylabel('Scores')
|
||||
ax.set_title('Scores by group and gender')
|
||||
ax.set_xticks(index + bar_width / 2)
|
||||
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
|
||||
ax.legend()
|
||||
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
这个例子来自一个应用程序,在这个应用程序中,小学体育教师希望能够向父母展示他们的孩子在一些健身测试中的表现,而且重要的是,相对于其他孩子的表现。 为了演示目的提取绘图代码,我们只为小Johnny Doe编制一些数据......
|
||||
|
||||
```python
|
||||
Student = namedtuple('Student', ['name', 'grade', 'gender'])
|
||||
Score = namedtuple('Score', ['score', 'percentile'])
|
||||
|
||||
# GLOBAL CONSTANTS
|
||||
testNames = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility',
|
||||
'Push Ups']
|
||||
testMeta = dict(zip(testNames, ['laps', 'sec', 'min:sec', 'sec', '']))
|
||||
|
||||
|
||||
def attach_ordinal(num):
|
||||
"""helper function to add ordinal string to integers
|
||||
|
||||
1 -> 1st
|
||||
56 -> 56th
|
||||
"""
|
||||
suffixes = {str(i): v
|
||||
for i, v in enumerate(['th', 'st', 'nd', 'rd', 'th',
|
||||
'th', 'th', 'th', 'th', 'th'])}
|
||||
|
||||
v = str(num)
|
||||
# special case early teens
|
||||
if v in {'11', '12', '13'}:
|
||||
return v + 'th'
|
||||
return v + suffixes[v[-1]]
|
||||
|
||||
|
||||
def format_score(scr, test):
|
||||
"""
|
||||
Build up the score labels for the right Y-axis by first
|
||||
appending a carriage return to each string and then tacking on
|
||||
the appropriate meta information (i.e., 'laps' vs 'seconds'). We
|
||||
want the labels centered on the ticks, so if there is no meta
|
||||
info (like for pushups) then don't add the carriage return to
|
||||
the string
|
||||
"""
|
||||
md = testMeta[test]
|
||||
if md:
|
||||
return '{0}\n{1}'.format(scr, md)
|
||||
else:
|
||||
return scr
|
||||
|
||||
|
||||
def format_ycursor(y):
|
||||
y = int(y)
|
||||
if y < 0 or y >= len(testNames):
|
||||
return ''
|
||||
else:
|
||||
return testNames[y]
|
||||
|
||||
|
||||
def plot_student_results(student, scores, cohort_size):
|
||||
# create the figure
|
||||
fig, ax1 = plt.subplots(figsize=(9, 7))
|
||||
fig.subplots_adjust(left=0.115, right=0.88)
|
||||
fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')
|
||||
|
||||
pos = np.arange(len(testNames))
|
||||
|
||||
rects = ax1.barh(pos, [scores[k].percentile for k in testNames],
|
||||
align='center',
|
||||
height=0.5, color='m',
|
||||
tick_label=testNames)
|
||||
|
||||
ax1.set_title(student.name)
|
||||
|
||||
ax1.set_xlim([0, 100])
|
||||
ax1.xaxis.set_major_locator(MaxNLocator(11))
|
||||
ax1.xaxis.grid(True, linestyle='--', which='major',
|
||||
color='grey', alpha=.25)
|
||||
|
||||
# Plot a solid vertical gridline to highlight the median position
|
||||
ax1.axvline(50, color='grey', alpha=0.25)
|
||||
# set X-axis tick marks at the deciles
|
||||
cohort_label = ax1.text(.5, -.07, 'Cohort Size: {0}'.format(cohort_size),
|
||||
horizontalalignment='center', size='small',
|
||||
transform=ax1.transAxes)
|
||||
|
||||
# Set the right-hand Y-axis ticks and labels
|
||||
ax2 = ax1.twinx()
|
||||
|
||||
scoreLabels = [format_score(scores[k].score, k) for k in testNames]
|
||||
|
||||
# set the tick locations
|
||||
ax2.set_yticks(pos)
|
||||
# make sure that the limits are set equally on both yaxis so the
|
||||
# ticks line up
|
||||
ax2.set_ylim(ax1.get_ylim())
|
||||
|
||||
# set the tick labels
|
||||
ax2.set_yticklabels(scoreLabels)
|
||||
|
||||
ax2.set_ylabel('Test Scores')
|
||||
|
||||
ax2.set_xlabel(('Percentile Ranking Across '
|
||||
'{grade} Grade {gender}s').format(
|
||||
grade=attach_ordinal(student.grade),
|
||||
gender=student.gender.title()))
|
||||
|
||||
rect_labels = []
|
||||
# Lastly, write in the ranking inside each bar to aid in interpretation
|
||||
for rect in rects:
|
||||
# Rectangle widths are already integer-valued but are floating
|
||||
# type, so it helps to remove the trailing decimal point and 0 by
|
||||
# converting width to int type
|
||||
width = int(rect.get_width())
|
||||
|
||||
rankStr = attach_ordinal(width)
|
||||
# The bars aren't wide enough to print the ranking inside
|
||||
if width < 5:
|
||||
# Shift the text to the right side of the right edge
|
||||
xloc = width + 1
|
||||
# Black against white background
|
||||
clr = 'black'
|
||||
align = 'left'
|
||||
else:
|
||||
# Shift the text to the left side of the right edge
|
||||
xloc = 0.98*width
|
||||
# White on magenta
|
||||
clr = 'white'
|
||||
align = 'right'
|
||||
|
||||
# Center the text vertically in the bar
|
||||
yloc = rect.get_y() + rect.get_height()/2.0
|
||||
label = ax1.text(xloc, yloc, rankStr, horizontalalignment=align,
|
||||
verticalalignment='center', color=clr, weight='bold',
|
||||
clip_on=True)
|
||||
rect_labels.append(label)
|
||||
|
||||
# make the interactive mouse over give the bar title
|
||||
ax2.fmt_ydata = format_ycursor
|
||||
# return all of the artists created
|
||||
return {'fig': fig,
|
||||
'ax': ax1,
|
||||
'ax_right': ax2,
|
||||
'bars': rects,
|
||||
'perc_labels': rect_labels,
|
||||
'cohort_label': cohort_label}
|
||||
|
||||
student = Student('Johnny Doe', 2, 'boy')
|
||||
scores = dict(zip(testNames,
|
||||
(Score(v, p) for v, p in
|
||||
zip(['7', '48', '12:52', '17', '14'],
|
||||
np.round(np.random.uniform(0, 1,
|
||||
len(testNames))*100, 0)))))
|
||||
cohort_size = 62 # The number of other 2nd grade boys
|
||||
|
||||
arts = plot_student_results(student, scores, cohort_size)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: barchart_demo.py](https://matplotlib.org/_downloads/barchart_demo.py)
|
||||
- [下载Jupyter notebook: barchart_demo.ipynb](https://matplotlib.org/_downloads/barchart_demo.ipynb)
|
||||
97
Python/matplotlab/gallery/statistics/boxplot.md
Normal file
97
Python/matplotlab/gallery/statistics/boxplot.md
Normal file
@@ -0,0 +1,97 @@
|
||||
# 箱形图中的艺术
|
||||
|
||||
此示例演示如何使用各种kwargs完全自定义箱形图。第一个图演示了如何删除和添加单个组件(请注意,平均值是默认情况下未显示的唯一值)。第二个图展示了如何定制艺术家的风格。它还演示了如何将胡须的极限设置为特定的百分位数(右下轴)
|
||||
|
||||
关于箱形图及其历史的一般参考可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# fake data
|
||||
np.random.seed(19680801)
|
||||
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
|
||||
labels = list('ABCD')
|
||||
fs = 10 # fontsize
|
||||
```
|
||||
|
||||
演示如何切换不同元素的显示:
|
||||
|
||||
```python
|
||||
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)
|
||||
axes[0, 0].boxplot(data, labels=labels)
|
||||
axes[0, 0].set_title('Default', fontsize=fs)
|
||||
|
||||
axes[0, 1].boxplot(data, labels=labels, showmeans=True)
|
||||
axes[0, 1].set_title('showmeans=True', fontsize=fs)
|
||||
|
||||
axes[0, 2].boxplot(data, labels=labels, showmeans=True, meanline=True)
|
||||
axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)
|
||||
|
||||
axes[1, 0].boxplot(data, labels=labels, showbox=False, showcaps=False)
|
||||
tufte_title = 'Tufte Style \n(showbox=False,\nshowcaps=False)'
|
||||
axes[1, 0].set_title(tufte_title, fontsize=fs)
|
||||
|
||||
axes[1, 1].boxplot(data, labels=labels, notch=True, bootstrap=10000)
|
||||
axes[1, 1].set_title('notch=True,\nbootstrap=10000', fontsize=fs)
|
||||
|
||||
axes[1, 2].boxplot(data, labels=labels, showfliers=False)
|
||||
axes[1, 2].set_title('showfliers=False', fontsize=fs)
|
||||
|
||||
for ax in axes.flatten():
|
||||
ax.set_yscale('log')
|
||||
ax.set_yticklabels([])
|
||||
|
||||
fig.subplots_adjust(hspace=0.4)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
演示如何自定义显示不同的元素:
|
||||
|
||||
```python
|
||||
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
|
||||
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
|
||||
linestyle='none')
|
||||
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
|
||||
meanpointprops = dict(marker='D', markeredgecolor='black',
|
||||
markerfacecolor='firebrick')
|
||||
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
|
||||
|
||||
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)
|
||||
axes[0, 0].boxplot(data, boxprops=boxprops)
|
||||
axes[0, 0].set_title('Custom boxprops', fontsize=fs)
|
||||
|
||||
axes[0, 1].boxplot(data, flierprops=flierprops, medianprops=medianprops)
|
||||
axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs)
|
||||
|
||||
axes[0, 2].boxplot(data, whis='range')
|
||||
axes[0, 2].set_title('whis="range"', fontsize=fs)
|
||||
|
||||
axes[1, 0].boxplot(data, meanprops=meanpointprops, meanline=False,
|
||||
showmeans=True)
|
||||
axes[1, 0].set_title('Custom mean\nas point', fontsize=fs)
|
||||
|
||||
axes[1, 1].boxplot(data, meanprops=meanlineprops, meanline=True,
|
||||
showmeans=True)
|
||||
axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)
|
||||
|
||||
axes[1, 2].boxplot(data, whis=[15, 85])
|
||||
axes[1, 2].set_title('whis=[15, 85]\n#percentiles', fontsize=fs)
|
||||
|
||||
for ax in axes.flatten():
|
||||
ax.set_yscale('log')
|
||||
ax.set_yticklabels([])
|
||||
|
||||
fig.suptitle("I never said they'd be pretty")
|
||||
fig.subplots_adjust(hspace=0.4)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: boxplot.py](https://matplotlib.org/_downloads/boxplot.py)
|
||||
- [下载Jupyter notebook: boxplot.ipynb](https://matplotlib.org/_downloads/boxplot.ipynb)
|
||||
53
Python/matplotlab/gallery/statistics/boxplot_color.md
Normal file
53
Python/matplotlab/gallery/statistics/boxplot_color.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# 带有自定义填充颜色的箱形图
|
||||
|
||||
此图说明了如何创建两种类型的箱形图(矩形和缺口),以及如何通过访问框图的艺术家属性来使用自定义颜色填充它们。 此外,labels参数用于为每个样本提供x-tick标签。
|
||||
|
||||
关于箱形图及其历史的一般参考可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
# Random test data
|
||||
np.random.seed(19680801)
|
||||
all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)]
|
||||
labels = ['x1', 'x2', 'x3']
|
||||
|
||||
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
|
||||
|
||||
# rectangular box plot
|
||||
bplot1 = axes[0].boxplot(all_data,
|
||||
vert=True, # vertical box alignment
|
||||
patch_artist=True, # fill with color
|
||||
labels=labels) # will be used to label x-ticks
|
||||
axes[0].set_title('Rectangular box plot')
|
||||
|
||||
# notch shape box plot
|
||||
bplot2 = axes[1].boxplot(all_data,
|
||||
notch=True, # notch shape
|
||||
vert=True, # vertical box alignment
|
||||
patch_artist=True, # fill with color
|
||||
labels=labels) # will be used to label x-ticks
|
||||
axes[1].set_title('Notched box plot')
|
||||
|
||||
# fill with colors
|
||||
colors = ['pink', 'lightblue', 'lightgreen']
|
||||
for bplot in (bplot1, bplot2):
|
||||
for patch, color in zip(bplot['boxes'], colors):
|
||||
patch.set_facecolor(color)
|
||||
|
||||
# adding horizontal grid lines
|
||||
for ax in axes:
|
||||
ax.yaxis.grid(True)
|
||||
ax.set_xlabel('Three separate samples')
|
||||
ax.set_ylabel('Observed values')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: boxplot_color.py](https://matplotlib.org/_downloads/boxplot_color.py)
|
||||
- [下载Jupyter notebook: boxplot_color.ipynb](https://matplotlib.org/_downloads/boxplot_color.ipynb)
|
||||
238
Python/matplotlab/gallery/statistics/boxplot_demo.md
Normal file
238
Python/matplotlab/gallery/statistics/boxplot_demo.md
Normal file
@@ -0,0 +1,238 @@
|
||||
# 箱形图
|
||||
|
||||
用matplotlib可视化箱形图。
|
||||
|
||||
以下示例展示了如何使用Matplotlib可视化箱图。有许多选项可以控制它们的外观以及用于汇总数据的统计信息。
|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from matplotlib.patches import Polygon
|
||||
|
||||
|
||||
# Fixing random state for reproducibility
|
||||
np.random.seed(19680801)
|
||||
|
||||
# fake up some data
|
||||
spread = np.random.rand(50) * 100
|
||||
center = np.ones(25) * 50
|
||||
flier_high = np.random.rand(10) * 100 + 100
|
||||
flier_low = np.random.rand(10) * -100
|
||||
data = np.concatenate((spread, center, flier_high, flier_low))
|
||||
|
||||
fig, axs = plt.subplots(2, 3)
|
||||
|
||||
# basic plot
|
||||
axs[0, 0].boxplot(data)
|
||||
axs[0, 0].set_title('basic plot')
|
||||
|
||||
# notched plot
|
||||
axs[0, 1].boxplot(data, 1)
|
||||
axs[0, 1].set_title('notched plot')
|
||||
|
||||
# change outlier point symbols
|
||||
axs[0, 2].boxplot(data, 0, 'gD')
|
||||
axs[0, 2].set_title('change outlier\npoint symbols')
|
||||
|
||||
# don't show outlier points
|
||||
axs[1, 0].boxplot(data, 0, '')
|
||||
axs[1, 0].set_title("don't show\noutlier points")
|
||||
|
||||
# horizontal boxes
|
||||
axs[1, 1].boxplot(data, 0, 'rs', 0)
|
||||
axs[1, 1].set_title('horizontal boxes')
|
||||
|
||||
# change whisker length
|
||||
axs[1, 2].boxplot(data, 0, 'rs', 0, 0.75)
|
||||
axs[1, 2].set_title('change whisker length')
|
||||
|
||||
fig.subplots_adjust(left=0.08, right=0.98, bottom=0.05, top=0.9,
|
||||
hspace=0.4, wspace=0.3)
|
||||
|
||||
# fake up some more data
|
||||
spread = np.random.rand(50) * 100
|
||||
center = np.ones(25) * 40
|
||||
flier_high = np.random.rand(10) * 100 + 100
|
||||
flier_low = np.random.rand(10) * -100
|
||||
d2 = np.concatenate((spread, center, flier_high, flier_low))
|
||||
data.shape = (-1, 1)
|
||||
d2.shape = (-1, 1)
|
||||
# Making a 2-D array only works if all the columns are the
|
||||
# same length. If they are not, then use a list instead.
|
||||
# This is actually more efficient because boxplot converts
|
||||
# a 2-D array into a list of vectors internally anyway.
|
||||
data = [data, d2, d2[::2, 0]]
|
||||
|
||||
# Multiple box plots on one Axes
|
||||
fig, ax = plt.subplots()
|
||||
ax.boxplot(data)
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
下面我们将从五个不同的概率分布生成数据,每个概率分布具有不同的特征。 我们想要了解数据的IID引导程序重采样如何保留原始样本的分布属性,并且箱形图是进行此评估的一种可视化工具。
|
||||
|
||||
```python
|
||||
numDists = 5
|
||||
randomDists = ['Normal(1,1)', ' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)',
|
||||
'Triangular(2,9,11)']
|
||||
N = 500
|
||||
|
||||
norm = np.random.normal(1, 1, N)
|
||||
logn = np.random.lognormal(1, 1, N)
|
||||
expo = np.random.exponential(1, N)
|
||||
gumb = np.random.gumbel(6, 4, N)
|
||||
tria = np.random.triangular(2, 9, 11, N)
|
||||
|
||||
# Generate some random indices that we'll use to resample the original data
|
||||
# arrays. For code brevity, just use the same random indices for each array
|
||||
bootstrapIndices = np.random.random_integers(0, N - 1, N)
|
||||
normBoot = norm[bootstrapIndices]
|
||||
expoBoot = expo[bootstrapIndices]
|
||||
gumbBoot = gumb[bootstrapIndices]
|
||||
lognBoot = logn[bootstrapIndices]
|
||||
triaBoot = tria[bootstrapIndices]
|
||||
|
||||
data = [norm, normBoot, logn, lognBoot, expo, expoBoot, gumb, gumbBoot,
|
||||
tria, triaBoot]
|
||||
|
||||
fig, ax1 = plt.subplots(figsize=(10, 6))
|
||||
fig.canvas.set_window_title('A Boxplot Example')
|
||||
fig.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25)
|
||||
|
||||
bp = ax1.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
|
||||
plt.setp(bp['boxes'], color='black')
|
||||
plt.setp(bp['whiskers'], color='black')
|
||||
plt.setp(bp['fliers'], color='red', marker='+')
|
||||
|
||||
# Add a horizontal grid to the plot, but make it very light in color
|
||||
# so we can use it for reading data values but not be distracting
|
||||
ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey',
|
||||
alpha=0.5)
|
||||
|
||||
# Hide these grid behind plot objects
|
||||
ax1.set_axisbelow(True)
|
||||
ax1.set_title('Comparison of IID Bootstrap Resampling Across Five Distributions')
|
||||
ax1.set_xlabel('Distribution')
|
||||
ax1.set_ylabel('Value')
|
||||
|
||||
# Now fill the boxes with desired colors
|
||||
boxColors = ['darkkhaki', 'royalblue']
|
||||
numBoxes = numDists*2
|
||||
medians = list(range(numBoxes))
|
||||
for i in range(numBoxes):
|
||||
box = bp['boxes'][i]
|
||||
boxX = []
|
||||
boxY = []
|
||||
for j in range(5):
|
||||
boxX.append(box.get_xdata()[j])
|
||||
boxY.append(box.get_ydata()[j])
|
||||
boxCoords = np.column_stack([boxX, boxY])
|
||||
# Alternate between Dark Khaki and Royal Blue
|
||||
k = i % 2
|
||||
boxPolygon = Polygon(boxCoords, facecolor=boxColors[k])
|
||||
ax1.add_patch(boxPolygon)
|
||||
# Now draw the median lines back over what we just filled in
|
||||
med = bp['medians'][i]
|
||||
medianX = []
|
||||
medianY = []
|
||||
for j in range(2):
|
||||
medianX.append(med.get_xdata()[j])
|
||||
medianY.append(med.get_ydata()[j])
|
||||
ax1.plot(medianX, medianY, 'k')
|
||||
medians[i] = medianY[0]
|
||||
# Finally, overplot the sample averages, with horizontal alignment
|
||||
# in the center of each box
|
||||
ax1.plot([np.average(med.get_xdata())], [np.average(data[i])],
|
||||
color='w', marker='*', markeredgecolor='k')
|
||||
|
||||
# Set the axes ranges and axes labels
|
||||
ax1.set_xlim(0.5, numBoxes + 0.5)
|
||||
top = 40
|
||||
bottom = -5
|
||||
ax1.set_ylim(bottom, top)
|
||||
ax1.set_xticklabels(np.repeat(randomDists, 2),
|
||||
rotation=45, fontsize=8)
|
||||
|
||||
# Due to the Y-axis scale being different across samples, it can be
|
||||
# hard to compare differences in medians across the samples. Add upper
|
||||
# X-axis tick labels with the sample medians to aid in comparison
|
||||
# (just use two decimal places of precision)
|
||||
pos = np.arange(numBoxes) + 1
|
||||
upperLabels = [str(np.round(s, 2)) for s in medians]
|
||||
weights = ['bold', 'semibold']
|
||||
for tick, label in zip(range(numBoxes), ax1.get_xticklabels()):
|
||||
k = tick % 2
|
||||
ax1.text(pos[tick], top - (top*0.05), upperLabels[tick],
|
||||
horizontalalignment='center', size='x-small', weight=weights[k],
|
||||
color=boxColors[k])
|
||||
|
||||
# Finally, add a basic legend
|
||||
fig.text(0.80, 0.08, str(N) + ' Random Numbers',
|
||||
backgroundcolor=boxColors[0], color='black', weight='roman',
|
||||
size='x-small')
|
||||
fig.text(0.80, 0.045, 'IID Bootstrap Resample',
|
||||
backgroundcolor=boxColors[1],
|
||||
color='white', weight='roman', size='x-small')
|
||||
fig.text(0.80, 0.015, '*', color='white', backgroundcolor='silver',
|
||||
weight='roman', size='medium')
|
||||
fig.text(0.815, 0.013, ' Average Value', color='black', weight='roman',
|
||||
size='x-small')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
在这里,我们编写一个自定义函数来引导置信区间。然后我们可以使用boxplot和此函数来显示这些间隔。
|
||||
|
||||
```python
|
||||
def fakeBootStrapper(n):
|
||||
'''
|
||||
This is just a placeholder for the user's method of
|
||||
bootstrapping the median and its confidence intervals.
|
||||
|
||||
Returns an arbitrary median and confidence intervals
|
||||
packed into a tuple
|
||||
'''
|
||||
if n == 1:
|
||||
med = 0.1
|
||||
CI = (-0.25, 0.25)
|
||||
else:
|
||||
med = 0.2
|
||||
CI = (-0.35, 0.50)
|
||||
|
||||
return med, CI
|
||||
|
||||
inc = 0.1
|
||||
e1 = np.random.normal(0, 1, size=(500,))
|
||||
e2 = np.random.normal(0, 1, size=(500,))
|
||||
e3 = np.random.normal(0, 1 + inc, size=(500,))
|
||||
e4 = np.random.normal(0, 1 + 2*inc, size=(500,))
|
||||
|
||||
treatments = [e1, e2, e3, e4]
|
||||
med1, CI1 = fakeBootStrapper(1)
|
||||
med2, CI2 = fakeBootStrapper(2)
|
||||
medians = [None, None, med1, med2]
|
||||
conf_intervals = [None, None, CI1, CI2]
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
pos = np.array(range(len(treatments))) + 1
|
||||
bp = ax.boxplot(treatments, sym='k+', positions=pos,
|
||||
notch=1, bootstrap=5000,
|
||||
usermedians=medians,
|
||||
conf_intervals=conf_intervals)
|
||||
|
||||
ax.set_xlabel('treatment')
|
||||
ax.set_ylabel('response')
|
||||
plt.setp(bp['whiskers'], color='k', linestyle='-')
|
||||
plt.setp(bp['fliers'], markersize=3.0)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: boxplot_demo.py](https://matplotlib.org/_downloads/boxplot_demo.py)
|
||||
- [下载Jupyter notebook: boxplot_demo.ipynb](https://matplotlib.org/_downloads/boxplot_demo.ipynb)
|
||||
54
Python/matplotlab/gallery/statistics/boxplot_vs_violin.md
Normal file
54
Python/matplotlab/gallery/statistics/boxplot_vs_violin.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# 箱形图与小提琴图对比
|
||||
|
||||
请注意,尽管小提琴图与Tukey(1977)的箱形图密切相关,但它们还添加了有用的信息,例如样本数据的分布(密度轨迹)。
|
||||
|
||||
默认情况下,箱形图显示1.5 *四分位数范围之外的数据点作为晶须上方或下方的异常值,而小提琴图则显示数据的整个范围。
|
||||
|
||||
关于箱形图及其历史的一般参考可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf
|
||||
|
||||
小提琴图需要 matplotlib >= 1.4。
|
||||
|
||||
有关小提琴绘制的更多信息,scikit-learn文档有一个很棒的部分:http://scikit-learn.org/stable/modules/density.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
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()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: boxplot_vs_violin.py](https://matplotlib.org/_downloads/boxplot_vs_violin.py)
|
||||
- [下载Jupyter notebook: boxplot_vs_violin.ipynb](https://matplotlib.org/_downloads/boxplot_vs_violin.ipynb)
|
||||
112
Python/matplotlab/gallery/statistics/bxp.md
Normal file
112
Python/matplotlab/gallery/statistics/bxp.md
Normal file
@@ -0,0 +1,112 @@
|
||||
# 箱形图抽屉功能
|
||||
|
||||
此示例演示如何将预先计算的箱形图统计信息传递到框图抽屉。第一个图演示了如何删除和添加单个组件(请注意,平均值是默认情况下未显示的唯一值)。第二个图展示了如何定制艺术风格。
|
||||
|
||||
关于箱形图及其历史的一个很好的一般参考可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.cbook as cbook
|
||||
|
||||
# fake data
|
||||
np.random.seed(19680801)
|
||||
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
|
||||
labels = list('ABCD')
|
||||
|
||||
# compute the boxplot stats
|
||||
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
|
||||
```
|
||||
|
||||
在我们计算了统计数据之后,我们可以通过并改变任何事情。 为了证明这一点,我将每组的中位数设置为所有数据的中位数,并将均值加倍
|
||||
|
||||
```python
|
||||
for n in range(len(stats)):
|
||||
stats[n]['med'] = np.median(data)
|
||||
stats[n]['mean'] *= 2
|
||||
|
||||
print(list(stats[0]))
|
||||
|
||||
fs = 10 # fontsize
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
```python
|
||||
['label', 'mean', 'iqr', 'cilo', 'cihi', 'whishi', 'whislo', 'fliers', 'q1', 'med', 'q3']
|
||||
```
|
||||
|
||||
演示如何切换不同元素的显示:
|
||||
|
||||
```python
|
||||
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)
|
||||
axes[0, 0].bxp(stats)
|
||||
axes[0, 0].set_title('Default', fontsize=fs)
|
||||
|
||||
axes[0, 1].bxp(stats, showmeans=True)
|
||||
axes[0, 1].set_title('showmeans=True', fontsize=fs)
|
||||
|
||||
axes[0, 2].bxp(stats, showmeans=True, meanline=True)
|
||||
axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)
|
||||
|
||||
axes[1, 0].bxp(stats, showbox=False, showcaps=False)
|
||||
tufte_title = 'Tufte Style\n(showbox=False,\nshowcaps=False)'
|
||||
axes[1, 0].set_title(tufte_title, fontsize=fs)
|
||||
|
||||
axes[1, 1].bxp(stats, shownotches=True)
|
||||
axes[1, 1].set_title('notch=True', fontsize=fs)
|
||||
|
||||
axes[1, 2].bxp(stats, showfliers=False)
|
||||
axes[1, 2].set_title('showfliers=False', fontsize=fs)
|
||||
|
||||
for ax in axes.flatten():
|
||||
ax.set_yscale('log')
|
||||
ax.set_yticklabels([])
|
||||
|
||||
fig.subplots_adjust(hspace=0.4)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
演示如何自定义显示不同的元素:
|
||||
|
||||
```python
|
||||
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
|
||||
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
|
||||
linestyle='none')
|
||||
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
|
||||
meanpointprops = dict(marker='D', markeredgecolor='black',
|
||||
markerfacecolor='firebrick')
|
||||
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
|
||||
|
||||
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6), sharey=True)
|
||||
axes[0, 0].bxp(stats, boxprops=boxprops)
|
||||
axes[0, 0].set_title('Custom boxprops', fontsize=fs)
|
||||
|
||||
axes[0, 1].bxp(stats, flierprops=flierprops, medianprops=medianprops)
|
||||
axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs)
|
||||
|
||||
axes[1, 0].bxp(stats, meanprops=meanpointprops, meanline=False,
|
||||
showmeans=True)
|
||||
axes[1, 0].set_title('Custom mean\nas point', fontsize=fs)
|
||||
|
||||
axes[1, 1].bxp(stats, meanprops=meanlineprops, meanline=True,
|
||||
showmeans=True)
|
||||
axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)
|
||||
|
||||
for ax in axes.flatten():
|
||||
ax.set_yscale('log')
|
||||
ax.set_yticklabels([])
|
||||
|
||||
fig.suptitle("I never said they'd be pretty")
|
||||
fig.subplots_adjust(hspace=0.4)
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: bxp.py](https://matplotlib.org/_downloads/bxp.py)
|
||||
- [下载Jupyter notebook: bxp.ipynb](https://matplotlib.org/_downloads/bxp.ipynb)
|
||||
75
Python/matplotlab/gallery/statistics/customized_violin.md
Normal file
75
Python/matplotlab/gallery/statistics/customized_violin.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# 自定义小提琴图
|
||||
|
||||
此示例演示如何完全自定义小提琴图。 第一个图通过仅提供数据来显示默认样式。第二个图首先限制了matplotlib用额外的kwargs绘制的内容。然后在顶部绘制箱形图的简化表示。 最后,修改了小提琴图的风格。
|
||||
|
||||
有关小提琴图的更多信息,scikit-learn文档有一个很棒的部分:http://scikit-learn.org/stable/modules/density.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
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()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: customized_violin.py](https://matplotlib.org/_downloads/customized_violin.py)
|
||||
- [下载Jupyter notebook: customized_violin.ipynb](https://matplotlib.org/_downloads/customized_violin.ipynb)
|
||||
23
Python/matplotlab/gallery/statistics/errorbar.md
Normal file
23
Python/matplotlab/gallery/statistics/errorbar.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# 误差条形图功能
|
||||
|
||||
这展示了误差条形图功能的最基本用法。在这种情况下,为x方向和y方向的误差提供常数值。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# example data
|
||||
x = np.arange(0.1, 4, 0.5)
|
||||
y = np.exp(-x)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
ax.errorbar(x, y, xerr=0.2, yerr=0.4)
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: errorbar.py](https://matplotlib.org/_downloads/errorbar.py)
|
||||
- [下载Jupyter notebook: errorbar.ipynb](https://matplotlib.org/_downloads/errorbar.ipynb)
|
||||
47
Python/matplotlab/gallery/statistics/errorbar_features.md
Normal file
47
Python/matplotlab/gallery/statistics/errorbar_features.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# 误差条形图的不同方法
|
||||
|
||||
可以将错误指定为常数值(如errorbar_demo.py中所示)。但是,此示例通过指定错误值数组来演示它们的不同之处。
|
||||
|
||||
如果原始x和y数据的长度为N,则有两个选项:
|
||||
|
||||
1. 数组形状为(N,):
|
||||
每个点的误差都不同,但误差值是对称的(即,上下两个值相等)。
|
||||
1. 数组形状为(2, N):
|
||||
每个点的误差不同,并且下限和上限(按该顺序)不同(非对称情况)。
|
||||
|
||||
此外,此示例演示如何使用带有误差线的对数刻度。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# example data
|
||||
x = np.arange(0.1, 4, 0.5)
|
||||
y = np.exp(-x)
|
||||
|
||||
# example error bar values that vary with x-position
|
||||
error = 0.1 + 0.2 * x
|
||||
|
||||
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
|
||||
ax0.errorbar(x, y, yerr=error, fmt='-o')
|
||||
ax0.set_title('variable, symmetric error')
|
||||
|
||||
# error bar values w/ different -/+ errors that
|
||||
# also vary with the x-position
|
||||
lower_error = 0.4 * error
|
||||
upper_error = error
|
||||
asymmetric_error = [lower_error, upper_error]
|
||||
|
||||
ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o')
|
||||
ax1.set_title('variable, asymmetric error')
|
||||
ax1.set_yscale('log')
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: errorbar_features.py](https://matplotlib.org/_downloads/errorbar_features.py)
|
||||
- [下载Jupyter notebook: errorbar_features.ipynb](https://matplotlib.org/_downloads/errorbar_features.ipynb)
|
||||
|
||||
73
Python/matplotlab/gallery/statistics/errorbar_limits.md
Normal file
73
Python/matplotlab/gallery/statistics/errorbar_limits.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# 误差条形图中的上限和下限
|
||||
|
||||
在matplotlib中,误差条可以有“限制”。对误差线应用限制实质上使误差单向。因此,可以分别通过``uplims``,``lolims``,``xuplims``和``xlolims``参数在y方向和x方向上应用上限和下限。 这些参数可以是标量或布尔数组。
|
||||
|
||||
例如,如果``xlolims``为``True``,则``x-error``条形将仅从数据扩展到递增值。如果``uplims``是一个填充了``False``的数组,除了第4和第7个值之外,所有y误差条都是双向的,除了第4和第7个条形,它们将从数据延伸到减小的y值。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# example data
|
||||
x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
|
||||
y = np.exp(-x)
|
||||
xerr = 0.1
|
||||
yerr = 0.2
|
||||
|
||||
# lower & upper limits of the error
|
||||
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
|
||||
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
|
||||
ls = 'dotted'
|
||||
|
||||
fig, ax = plt.subplots(figsize=(7, 4))
|
||||
|
||||
# standard error bars
|
||||
ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
|
||||
|
||||
# including upper limits
|
||||
ax.errorbar(x, y + 0.5, xerr=xerr, yerr=yerr, uplims=uplims,
|
||||
linestyle=ls)
|
||||
|
||||
# including lower limits
|
||||
ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
|
||||
linestyle=ls)
|
||||
|
||||
# including upper and lower limits
|
||||
ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
|
||||
lolims=lolims, uplims=uplims,
|
||||
marker='o', markersize=8,
|
||||
linestyle=ls)
|
||||
|
||||
# Plot a series with lower and upper limits in both x & y
|
||||
# constant x-error with varying y-error
|
||||
xerr = 0.2
|
||||
yerr = np.zeros_like(x) + 0.2
|
||||
yerr[[3, 6]] = 0.3
|
||||
|
||||
# mock up some limits by modifying previous data
|
||||
xlolims = lolims
|
||||
xuplims = uplims
|
||||
lolims = np.zeros(x.shape)
|
||||
uplims = np.zeros(x.shape)
|
||||
lolims[[6]] = True # only limited at this index
|
||||
uplims[[3]] = True # only limited at this index
|
||||
|
||||
# do the plotting
|
||||
ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
|
||||
xlolims=xlolims, xuplims=xuplims,
|
||||
uplims=uplims, lolims=lolims,
|
||||
marker='o', markersize=8,
|
||||
linestyle='none')
|
||||
|
||||
# tidy up the figure
|
||||
ax.set_xlim((0, 5.5))
|
||||
ax.set_title('Errorbar upper and lower limits')
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: errorbar_limits.py](https://matplotlib.org/_downloads/errorbar_limits.py)
|
||||
- [下载Jupyter notebook: errorbar_limits.ipynb](https://matplotlib.org/_downloads/errorbar_limits.ipynb)
|
||||
68
Python/matplotlab/gallery/statistics/errorbars_and_boxes.md
Normal file
68
Python/matplotlab/gallery/statistics/errorbars_and_boxes.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# 使用PatchCollection在误差图中创建箱型图
|
||||
|
||||
在这个例子中,我们通过在x方向和y方向上添加由条形极限定义的矩形块来拼写一个非常标准的误差条形图。为此,我们必须编写自己的自定义函数 ``make_error_boxes``。仔细检查此函数将揭示matplotlib编写函数的首选模式:
|
||||
|
||||
1. an Axes object is passed directly to the function
|
||||
1. the function operates on the Axes methods directly, not through the ``pyplot`` interface
|
||||
1. plotting kwargs that could be abbreviated are spelled out for better code readability in the future (for example we use ``facecolor`` instead of fc)
|
||||
1. the artists returned by the Axes plotting methods are then returned by the function so that, if desired, their styles can be modified later outside of the function (they are not modified in this example).
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.collections import PatchCollection
|
||||
from matplotlib.patches import Rectangle
|
||||
|
||||
# Number of data points
|
||||
n = 5
|
||||
|
||||
# Dummy data
|
||||
np.random.seed(19680801)
|
||||
x = np.arange(0, n, 1)
|
||||
y = np.random.rand(n) * 5.
|
||||
|
||||
# Dummy errors (above and below)
|
||||
xerr = np.random.rand(2, n) + 0.1
|
||||
yerr = np.random.rand(2, n) + 0.2
|
||||
|
||||
|
||||
def make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r',
|
||||
edgecolor='None', alpha=0.5):
|
||||
|
||||
# Create list for all the error patches
|
||||
errorboxes = []
|
||||
|
||||
# Loop over data points; create box from errors at each point
|
||||
for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
|
||||
rect = Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum())
|
||||
errorboxes.append(rect)
|
||||
|
||||
# Create patch collection with specified colour/alpha
|
||||
pc = PatchCollection(errorboxes, facecolor=facecolor, alpha=alpha,
|
||||
edgecolor=edgecolor)
|
||||
|
||||
# Add collection to axes
|
||||
ax.add_collection(pc)
|
||||
|
||||
# Plot errorbars
|
||||
artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror,
|
||||
fmt='None', ecolor='k')
|
||||
|
||||
return artists
|
||||
|
||||
|
||||
# Create figure and axes
|
||||
fig, ax = plt.subplots(1)
|
||||
|
||||
# Call function to create error boxes
|
||||
_ = make_error_boxes(ax, x, y, xerr, yerr)
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: errorbars_and_boxes.py](https://matplotlib.org/_downloads/errorbars_and_boxes.py)
|
||||
- [下载Jupyter notebook: errorbars_and_boxes.ipynb](https://matplotlib.org/_downloads/errorbars_and_boxes.ipynb)
|
||||
46
Python/matplotlab/gallery/statistics/hexbin_demo.md
Normal file
46
Python/matplotlab/gallery/statistics/hexbin_demo.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Hexbin 演示
|
||||
|
||||
使用Matplotlib绘制hexbins。
|
||||
|
||||
Hexbin是一种轴方法或pyplot函数,它基本上是具有六边形单元的二维直方图的pcolor。 它可以比散点图更具信息性。 在下面的第一个图中,尝试用'scatter'代替'hexbin'。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Fixing random state for reproducibility
|
||||
np.random.seed(19680801)
|
||||
|
||||
n = 100000
|
||||
x = np.random.standard_normal(n)
|
||||
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
|
||||
xmin = x.min()
|
||||
xmax = x.max()
|
||||
ymin = y.min()
|
||||
ymax = y.max()
|
||||
|
||||
fig, axs = plt.subplots(ncols=2, sharey=True, figsize=(7, 4))
|
||||
fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93)
|
||||
ax = axs[0]
|
||||
hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
|
||||
ax.axis([xmin, xmax, ymin, ymax])
|
||||
ax.set_title("Hexagon binning")
|
||||
cb = fig.colorbar(hb, ax=ax)
|
||||
cb.set_label('counts')
|
||||
|
||||
ax = axs[1]
|
||||
hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
|
||||
ax.axis([xmin, xmax, ymin, ymax])
|
||||
ax.set_title("With a log color scale")
|
||||
cb = fig.colorbar(hb, ax=ax)
|
||||
cb.set_label('log10(N)')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: hexbin_demo.py](https://matplotlib.org/_downloads/hexbin_demo.py)
|
||||
- [下载Jupyter notebook: hexbin_demo.ipynb](https://matplotlib.org/_downloads/hexbin_demo.ipynb)
|
||||
102
Python/matplotlab/gallery/statistics/hist.md
Normal file
102
Python/matplotlab/gallery/statistics/hist.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# 直方图
|
||||
|
||||
演示如何使用matplotlib绘制直方图。
|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from matplotlib import colors
|
||||
from matplotlib.ticker import PercentFormatter
|
||||
|
||||
# Fixing random state for reproducibility
|
||||
np.random.seed(19680801)
|
||||
```
|
||||
|
||||
## 生成数据并绘制简单的直方图
|
||||
|
||||
要生成一维直方图,我们只需要一个数字矢量。对于二维直方图,我们需要第二个矢量。我们将在下面生成两者,并显示每个向量的直方图。
|
||||
|
||||
```python
|
||||
N_points = 100000
|
||||
n_bins = 20
|
||||
|
||||
# Generate a normal distribution, center at x=0 and y=5
|
||||
x = np.random.randn(N_points)
|
||||
y = .4 * x + np.random.randn(100000) + 5
|
||||
|
||||
fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True)
|
||||
|
||||
# We can set the number of bins with the `bins` kwarg
|
||||
axs[0].hist(x, bins=n_bins)
|
||||
axs[1].hist(y, bins=n_bins)
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 更新直方图颜色
|
||||
|
||||
直方图方法(除其他外)返回一个修补程序对象。这使我们可以访问所绘制对象的特性。使用这个,我们可以根据自己的喜好编辑直方图。让我们根据每个条的y值更改其颜色。
|
||||
|
||||
```python
|
||||
fig, axs = plt.subplots(1, 2, tight_layout=True)
|
||||
|
||||
# N is the count in each bin, bins is the lower-limit of the bin
|
||||
N, bins, patches = axs[0].hist(x, bins=n_bins)
|
||||
|
||||
# We'll color code by height, but you could use any scalar
|
||||
fracs = N / N.max()
|
||||
|
||||
# we need to normalize the data to 0..1 for the full range of the colormap
|
||||
norm = colors.Normalize(fracs.min(), fracs.max())
|
||||
|
||||
# Now, we'll loop through our objects and set the color of each accordingly
|
||||
for thisfrac, thispatch in zip(fracs, patches):
|
||||
color = plt.cm.viridis(norm(thisfrac))
|
||||
thispatch.set_facecolor(color)
|
||||
|
||||
# We can also normalize our inputs by the total number of counts
|
||||
axs[1].hist(x, bins=n_bins, density=True)
|
||||
|
||||
# Now we format the y-axis to display percentage
|
||||
axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=1))
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 绘制二维直方图
|
||||
|
||||
要绘制二维直方图,只需两个长度相同的向量,对应于直方图的每个轴。
|
||||
|
||||
```python
|
||||
fig, ax = plt.subplots(tight_layout=True)
|
||||
hist = ax.hist2d(x, y)
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 自定义直方图
|
||||
|
||||
自定义2D直方图类似于1D情况,您可以控制可视组件,如存储箱大小或颜色规格化。
|
||||
|
||||
```python
|
||||
fig, axs = plt.subplots(3, 1, figsize=(5, 15), sharex=True, sharey=True,
|
||||
tight_layout=True)
|
||||
|
||||
# We can increase the number of bins on each axis
|
||||
axs[0].hist2d(x, y, bins=40)
|
||||
|
||||
# As well as define normalization of the colors
|
||||
axs[1].hist2d(x, y, bins=40, norm=colors.LogNorm())
|
||||
|
||||
# We can also define custom numbers of bins for each axis
|
||||
axs[2].hist2d(x, y, bins=(80, 10), norm=colors.LogNorm())
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: hist.py](https://matplotlib.org/_downloads/hist.py)
|
||||
- [下载Jupyter notebook: hist.ipynb](https://matplotlib.org/_downloads/hist.ipynb)
|
||||
55
Python/matplotlab/gallery/statistics/histogram_cumulative.md
Normal file
55
Python/matplotlab/gallery/statistics/histogram_cumulative.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# 使用直方图绘制累积分布
|
||||
|
||||
这展示了如何绘制一个累积的、归一化的直方图作为一个步骤函数,以便可视化一个样本的经验累积分布函数(CDF)。文中还给出了理论上的CDF值。
|
||||
|
||||
演示了 ``hist`` 函数的其他几个选项。也就是说,我们使用 ``normed`` 参数来标准化直方图以及 ``累积`` 参数的几个不同选项。normed参数采用布尔值。 如果为 ``True`` ,则会对箱高进行缩放,使得柱状图的总面积为1。``累积``的kwarg稍微有些细微差别。与normed一样,您可以将其传递为True或False,但您也可以将其传递-1以反转分布。
|
||||
|
||||
由于我们显示了归一化和累积直方图,因此这些曲线实际上是样本的累积分布函数(CDF)。 在工程学中,经验CDF有时被称为“非超越”曲线。 换句话说,您可以查看给定-x值的y值,以使样本的概率和观察值不超过该x值。 例如,x轴上的值225对应于y轴上的约0.85,因此样本中的观察值不超过225的可能性为85%。相反,设置累积为-1,如同已完成 在此示例的最后一个系列中,创建“超出”曲线。
|
||||
|
||||
选择不同的箱数和大小会显着影响直方图的形状。Astropy文档有关于如何选择这些参数的重要部分:http://docs.astropy.org/en/stable/visualization/histogram.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
np.random.seed(19680801)
|
||||
|
||||
mu = 200
|
||||
sigma = 25
|
||||
n_bins = 50
|
||||
x = np.random.normal(mu, sigma, size=100)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(8, 4))
|
||||
|
||||
# plot the cumulative histogram
|
||||
n, bins, patches = ax.hist(x, n_bins, density=True, histtype='step',
|
||||
cumulative=True, label='Empirical')
|
||||
|
||||
# Add a line showing the expected distribution.
|
||||
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
|
||||
np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
|
||||
y = y.cumsum()
|
||||
y /= y[-1]
|
||||
|
||||
ax.plot(bins, y, 'k--', linewidth=1.5, label='Theoretical')
|
||||
|
||||
# Overlay a reversed cumulative histogram.
|
||||
ax.hist(x, bins=bins, density=True, histtype='step', cumulative=-1,
|
||||
label='Reversed emp.')
|
||||
|
||||
# tidy up the figure
|
||||
ax.grid(True)
|
||||
ax.legend(loc='right')
|
||||
ax.set_title('Cumulative step histograms')
|
||||
ax.set_xlabel('Annual rainfall (mm)')
|
||||
ax.set_ylabel('Likelihood of occurrence')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: histogram_cumulative.py](https://matplotlib.org/_downloads/histogram_cumulative.py)
|
||||
- [下载Jupyter notebook: histogram_cumulative.ipynb](https://matplotlib.org/_downloads/histogram_cumulative.ipynb)
|
||||
60
Python/matplotlab/gallery/statistics/histogram_features.md
Normal file
60
Python/matplotlab/gallery/statistics/histogram_features.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# 直方图(hist)函数的几个特性演示
|
||||
|
||||
除基本直方图外,此演示还显示了一些可选功能:
|
||||
|
||||
- 设置数据箱的数量。
|
||||
- ``标准化``标志,用于标准化箱高度,使直方图的积分为1.得到的直方图是概率密度函数的近似值。
|
||||
- 设置条形的面部颜色。
|
||||
- 设置不透明度(alpha值)。
|
||||
|
||||
选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分。
|
||||
|
||||
```python
|
||||
import matplotlib
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
np.random.seed(19680801)
|
||||
|
||||
# example data
|
||||
mu = 100 # mean of distribution
|
||||
sigma = 15 # standard deviation of distribution
|
||||
x = mu + sigma * np.random.randn(437)
|
||||
|
||||
num_bins = 50
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
# the histogram of the data
|
||||
n, bins, patches = ax.hist(x, num_bins, density=1)
|
||||
|
||||
# add a 'best fit' line
|
||||
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
|
||||
np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
|
||||
ax.plot(bins, y, '--')
|
||||
ax.set_xlabel('Smarts')
|
||||
ax.set_ylabel('Probability density')
|
||||
ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
|
||||
|
||||
# Tweak spacing to prevent clipping of ylabel
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 参考
|
||||
|
||||
此示例显示了以下函数和方法的使用:
|
||||
|
||||
```python
|
||||
matplotlib.axes.Axes.hist
|
||||
matplotlib.axes.Axes.set_title
|
||||
matplotlib.axes.Axes.set_xlabel
|
||||
matplotlib.axes.Axes.set_ylabel
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: histogram_features.py](https://matplotlib.org/_downloads/histogram_features.py)
|
||||
- [下载Jupyter notebook: histogram_features.ipynb](https://matplotlib.org/_downloads/histogram_features.ipynb)
|
||||
37
Python/matplotlab/gallery/statistics/histogram_histtypes.md
Normal file
37
Python/matplotlab/gallery/statistics/histogram_histtypes.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# 演示直方图函数的不同histtype设置
|
||||
|
||||
- 具有颜色填充的步进曲线的直方图。
|
||||
- 具有自定义和不相等的箱宽度的直方图。
|
||||
|
||||
选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分: http://docs.astropy.org/en/stable/visualization/histogram.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
np.random.seed(19680801)
|
||||
|
||||
mu = 200
|
||||
sigma = 25
|
||||
x = np.random.normal(mu, sigma, size=100)
|
||||
|
||||
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
|
||||
|
||||
ax0.hist(x, 20, density=True, histtype='stepfilled', facecolor='g', alpha=0.75)
|
||||
ax0.set_title('stepfilled')
|
||||
|
||||
# Create a histogram by providing the bin edges (unequally spaced).
|
||||
bins = [100, 150, 180, 195, 205, 220, 250, 300]
|
||||
ax1.hist(x, bins, density=True, histtype='bar', rwidth=0.8)
|
||||
ax1.set_title('unequal bins')
|
||||
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: histogram_histtypes.py](https://matplotlib.org/_downloads/histogram_histtypes.py)
|
||||
- [下载Jupyter notebook: histogram_histtypes.ipynb](https://matplotlib.org/_downloads/histogram_histtypes.ipynb)
|
||||
50
Python/matplotlab/gallery/statistics/histogram_multihist.md
Normal file
50
Python/matplotlab/gallery/statistics/histogram_multihist.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# 使用多个数据集演示直方图(hist)函数
|
||||
|
||||
绘制具有多个样本集的直方图并演示:
|
||||
|
||||
- 使用带有多个样本集的图例
|
||||
- 堆积图
|
||||
- 没有填充的步进曲线
|
||||
- 不同样本量的数据集
|
||||
|
||||
选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分: http://docs.astropy.org/en/stable/visualization/histogram.html
|
||||
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
np.random.seed(19680801)
|
||||
|
||||
n_bins = 10
|
||||
x = np.random.randn(1000, 3)
|
||||
|
||||
fig, axes = plt.subplots(nrows=2, ncols=2)
|
||||
ax0, ax1, ax2, ax3 = axes.flatten()
|
||||
|
||||
colors = ['red', 'tan', 'lime']
|
||||
ax0.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)
|
||||
ax0.legend(prop={'size': 10})
|
||||
ax0.set_title('bars with legend')
|
||||
|
||||
ax1.hist(x, n_bins, density=True, histtype='bar', stacked=True)
|
||||
ax1.set_title('stacked bar')
|
||||
|
||||
ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)
|
||||
ax2.set_title('stack step (unfilled)')
|
||||
|
||||
# Make a multiple-histogram of data-sets with different length.
|
||||
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
|
||||
ax3.hist(x_multi, n_bins, histtype='bar')
|
||||
ax3.set_title('different sample sizes')
|
||||
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: histogram_multihist.py](https://matplotlib.org/_downloads/histogram_multihist.py)
|
||||
- [下载Jupyter notebook: histogram_multihist.ipynb](https://matplotlib.org/_downloads/histogram_multihist.ipynb)
|
||||
@@ -0,0 +1,57 @@
|
||||
# 并排生成多个直方图
|
||||
|
||||
此示例沿范畴x轴绘制不同样本的水平直方图。此外,直方图被绘制成与它们的x位置对称,从而使它们与小提琴图非常相似。
|
||||
|
||||
为了使这个高度专门化的绘制,我们不能使用标准的 ``hist`` 方法。相反,我们使用 ``barh`` 直接绘制水平线。通过 ``np.histogram`` 函数计算棒材的垂直位置和长度。使用相同的范围(最小和最大值)和存储箱数量计算所有采样的直方图,以便每个采样的存储箱位于相同的垂直位置。
|
||||
|
||||
选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分: http://docs.astropy.org/en/stable/visualization/histogram.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
np.random.seed(19680801)
|
||||
number_of_bins = 20
|
||||
|
||||
# An example of three data sets to compare
|
||||
number_of_data_points = 387
|
||||
labels = ["A", "B", "C"]
|
||||
data_sets = [np.random.normal(0, 1, number_of_data_points),
|
||||
np.random.normal(6, 1, number_of_data_points),
|
||||
np.random.normal(-3, 1, number_of_data_points)]
|
||||
|
||||
# Computed quantities to aid plotting
|
||||
hist_range = (np.min(data_sets), np.max(data_sets))
|
||||
binned_data_sets = [
|
||||
np.histogram(d, range=hist_range, bins=number_of_bins)[0]
|
||||
for d in data_sets
|
||||
]
|
||||
binned_maximums = np.max(binned_data_sets, axis=1)
|
||||
x_locations = np.arange(0, sum(binned_maximums), np.max(binned_maximums))
|
||||
|
||||
# The bin_edges are the same for all of the histograms
|
||||
bin_edges = np.linspace(hist_range[0], hist_range[1], number_of_bins + 1)
|
||||
centers = 0.5 * (bin_edges + np.roll(bin_edges, 1))[:-1]
|
||||
heights = np.diff(bin_edges)
|
||||
|
||||
# Cycle through and plot each histogram
|
||||
fig, ax = plt.subplots()
|
||||
for x_loc, binned_data in zip(x_locations, binned_data_sets):
|
||||
lefts = x_loc - 0.5 * binned_data
|
||||
ax.barh(centers, binned_data, height=heights, left=lefts)
|
||||
|
||||
ax.set_xticks(x_locations)
|
||||
ax.set_xticklabels(labels)
|
||||
|
||||
ax.set_ylabel("Data values")
|
||||
ax.set_xlabel("Data sets")
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: multiple_histograms_side_by_side.py](https://matplotlib.org/_downloads/multiple_histograms_side_by_side.py)
|
||||
- [下载Jupyter notebook: multiple_histograms_side_by_side.ipynb](https://matplotlib.org/_downloads/multiple_histograms_side_by_side.ipynb)
|
||||
63
Python/matplotlab/gallery/statistics/violinplot.md
Normal file
63
Python/matplotlab/gallery/statistics/violinplot.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# 小提琴图基础
|
||||
|
||||
小提琴图类似于直方图和箱形图,因为它们显示了样本概率分布的抽象表示。小提琴图使用核密度估计(KDE)来计算样本的经验分布,而不是显示属于分类或顺序统计的数据点的计数。该计算由几个参数控制。此示例演示如何修改评估KDE的点数 ``(points)`` 以及如何修改KDE ``(bw_method)`` 的带宽。
|
||||
|
||||
有关小提琴图和KDE的更多信息,请参阅scikit-learn文档
|
||||
有一个很棒的部分:http://scikit-learn.org/stable/modules/density.html
|
||||
|
||||

|
||||
|
||||
```python
|
||||
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_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源码: violinplot.py](https://matplotlib.org/_downloads/violinplot.py)
|
||||
- [下载Jupyter notebook: violinplot.ipynb](https://matplotlib.org/_downloads/violinplot.ipynb)
|
||||
Reference in New Issue
Block a user