我正在尝试将一个普通的matplotlib.pyplot的plt.plot(x,y)
和变量y
作为变量x
的函数与一个箱线图结合起来。然而,我只想在x
的某些(可变)位置上绘制箱线图,但这似乎在matplotlib中不起作用?
我正在尝试将一个普通的matplotlib.pyplot的plt.plot(x,y)
和变量y
作为变量x
的函数与一个箱线图结合起来。然而,我只想在x
的某些(可变)位置上绘制箱线图,但这似乎在matplotlib中不起作用?
你是否想要类似这样的效果?boxplot函数中的positions
参数可以让你将箱线图放置在任意位置。
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()
以下是我所使用的方法:
# 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-')
locs = ax.get_xticks()
和ax.set_xticks(locs)
。 - MrCyclophilax.set_xticklabels(locs)
才能正确获取标签。 - MrCyclophil