我正在尝试将普通的 matplotlib.pyplotplt.plot(x,y)
与变量y
作为变量的函数x
与箱线图相结合。但是,我只想要在某些(可变)位置上的箱线图,x
但这似乎在 matplotlib 中不起作用?
问问题
17517 次
2 回答
27
你想要这样的东西吗?positions
kwarg 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
这对我有用:
- 箱线图
- 获取箱线图 x 轴刻度位置
- 使用箱线图 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 回答