18

我正在尝试将普通的 matplotlib.pyplotplt.plot(x,y)与变量y作为变量的函数x与箱线图相结合。但是,我只想要在某些(可变)位置上的箱线图,x但这似乎在 matplotlib 中不起作用?

4

2 回答 2

27

你想要这样的东西吗?positionskwarg toboxplot允许您将箱线图放置在任意位置。

import matplotlib.pyplot as plt
import numpy as np

# Generate some data...
data = np.random.random((100, 5))
y = data.mean(axis=0)
x = np.random.random(y.size) * 10
x -= x.min()
x.sort()

# Plot a line between the means of each dataset
plt.plot(x, y, 'b-')

# Save the default tick positions, so we can reset them...
locs, labels = plt.xticks() 

plt.boxplot(data, positions=x, notch=True)

# Reset the xtick locations.
plt.xticks(locs)
plt.show()

在此处输入图像描述

于 2011-05-09T19:38:15.863 回答
0

这对我有用:

  1. 箱线图
  2. 获取箱线图 x 轴刻度位置
  3. 使用箱线图 x 轴刻度位置作为线图的 x 轴值
# Plot Box-plot
ax.boxplot(data, positions=x, notch=True)
# Get box-plot x-tick locations
locs=ax.get_xticks()

# Plot a line between the means of each dataset
# x-values = box-plot x-tick locations
# y-values = means
ax.plot(locs, y, 'b-')


于 2021-05-23T12:32:09.533 回答