简体   繁体   English

结合 plt.plot(x,y) 和 plt.boxplot()

[英]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. boxplotpositions 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:这对我有用:

  1. plot box-plot绘制箱线图
  2. get boxt-plot x-axis tick locations获取 boxt-plot x 轴刻度位置
  3. use box-plot x-axis tick locations as x-axis values for the line plot使用箱线图 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-')


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM