[英]Combining plt.plot(x,y) with plt.boxplot()
I'm trying to combine a normal matplotlib.pyplot plt.plot(x,y)
with variable y
as a function of variable x
with a boxplot.我试图将一个普通的 matplotlib.pyplot plt.plot(x,y)
与变量y
作为变量x
与箱线图的函数结合起来。 However, I only want a boxplot on certain (variable) locations of x
but this does not seem to work in matplotlib?但是,我只想要x
某些(可变)位置的箱线图,但这在 matplotlib 中似乎不起作用?
Are you wanting something like this?你想要这样的东西吗? The positions
kwarg to boxplot
allows you to place the boxplots at arbitrary positions. boxplot
的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()
This is what has worked for me:这对我有用:
# 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-')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.