1

我有一个值列表,我想为其绘制分布。我正在使用箱线图,但最好添加一些从箱线图四分位数到轴的虚线。我还想要 x 刻度上显示的四分位数。这是一个粗略的想法,但最后是值而不是名称。

import numpy as np
import pandas as pd 
import matplotlib.pylab as plt


vel_arr = np.random.rand(1000,1)
fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)

# Create the boxplot
ax.boxplot(vel_arr,vert=False, manage_ticks=True)
ax.set_xlabel('value')
plt.yticks([1], ['category'])
plt.show()
4

1 回答 1

2

np.quantile计算所需的分位数。

ax.vlines绘制垂直线,例如从箱线图的中心到y=0. zorder=0确保这些线在箱线图后面。

ax.set_ylim(0.5, 1.5)重置 ylims。默认情况下,vlines使用一些额外的填充强制 ylims。

ax.set_xticks(quantiles)在每个分位数的位置设置 xticks。

import numpy as np
import matplotlib.pylab as plt

vel_arr = np.random.rand(50, 1)
fig = plt.figure(1, figsize=(9, 6))
ax = fig.add_subplot(111)

ax.boxplot(vel_arr, vert=False, manage_ticks=True)
ax.set_xlabel('value')
ax.set_yticks([1])
ax.set_yticklabels(['category'])

quantiles = np.quantile(vel_arr, np.array([0.00, 0.25, 0.50, 0.75, 1.00]))
ax.vlines(quantiles, [0] * quantiles.size, [1] * quantiles.size,
          color='b', ls=':', lw=0.5, zorder=0)
ax.set_ylim(0.5, 1.5)
ax.set_xticks(quantiles)
plt.show()

在此处输入图像描述

于 2020-05-16T16:55:50.163 回答