Matplotlib 组盒图组织程序设计

Matplotlib 组盒图组织程序设计,matplotlib,data-visualization,boxplot,Matplotlib,Data Visualization,Boxplot,我想对我的数据进行分组,并绘制所有组的箱线图。关于这一点有很多问题和答案,我的问题是我想用continuos变量分组,所以我想对我的数据进行历史编程 这就是我所做的。我的数据: import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt x = np.random.chisquare(5, size=100000) y = np.random.normal(size=100000) / (0.05 *

我想对我的数据进行分组,并绘制所有组的箱线图。关于这一点有很多问题和答案,我的问题是我想用continuos变量分组,所以我想对我的数据进行历史编程

这就是我所做的。我的数据:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

x = np.random.chisquare(5, size=100000)
y = np.random.normal(size=100000) / (0.05 * x + 0.1) + 2 * x

f, ax = plt.subplots()
ax.plot(x, y, '.', alpha=0.05)
plt.show()

我想研究
y
(位置、宽度,…)作为
x
函数的行为。我对
x
的分布不感兴趣,因此我将对其进行规范化

f, ax = plt.subplots()
xbins = np.linspace(0, 25, 50)
ybins = np.linspace(-20, 50, 50)
H, xedges, yedges = np.histogram2d(y, x, bins=(ybins, xbins))
norm = np.sum(H, axis = 0)
H /= norm
ax.pcolor(xbins, ybins, np.nan_to_num(H), vmax=.4)
plt.show()

我可以绘制柱状图,但我想要盒状图

binning = np.concatenate(([0], np.sort(np.random.random(20) * 25), [25]))
idx = np.digitize(x, binning)
data_to_plot = [y[idx == i] for i in xrange(len(binning))]
f, ax = plt.subplots()

midpoints = 0.5 * (binning[1:] + binning[:-1])
widths = 0.9 * (binning[1:] - binning[:-1])

from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator   = MultipleLocator(2)

ax.boxplot(data_to_plot, positions = midpoints, widths=widths)
ax.set_xlim(0, 25)
ax.xaxis.set_major_locator(majorLocator)
ax.set_xlabel('x')
ax.set_ylabel('median(y)')
plt.show()

有没有一种自动的方法,比如
ax.magic(x,y,binning)
?有更好的方法吗?(例如,查看将平均值和平均值误差绘制为误差条的图)

此外,我想缩小内存占用(我的实际数据远远超过100000),我担心
data\u to\u plot
,它是副本吗