我正在尝试将一个普通的matplotlib.pyplot plt.plot(x,y)
与变量y
结合起来作为变量x
的函数和一个箱线图。但是,我只想要x
的某些(可变)位置上的箱线图,但这在matplotlib中似乎不起作用?
答案 0 :(得分:20)
positions
kwarg boxplot
允许您将箱形图放置在任意位置。
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()
答案 1 :(得分:0)
这对我有用:
# 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-')