从大型数据集逐步构建箱线图
Buildling boxplots incrementally from large datasets
假设我有 4 个文件作为 .npz 文件保存在我的计算机上:W、X、Y 和 Z。
假设我的电脑在 RAM 消耗方面无法承受同时加载其中一个以上。
我怎样才能 运行 这个命令? :
matplotlib.pyplot.boxplot([W],[X],[Y],[Z])
换句话说,我如何加载 W、绘制 W、删除 W,然后加载 Y、绘制 Y、删除 Y,...并将其中的 4 个放在同一个图形上? (而不是子图)
谢谢!
一种选择是将数据的随机样本传递给绘图函数。
或者,因为箱线图仅包含聚合数据,所以您应该考虑单独计算这些聚合值,然后将它们应用于箱线图可视化。
使用 documentation 中的完整选项列表,您可以通过传递聚合数据来构建箱线图:
boxplot(self, x, notch=False, sym='b+', vert=True, whis=1.5,
positions=None, widths=None, patch_artist=False,
bootstrap=None, usermedians=None, conf_intervals=None,
meanline=False, showmeans=False, showcaps=True,
showbox=True, showfliers=True, boxprops=None, labels=None,
flierprops=None, medianprops=None, meanprops=None,
capprops=None, whiskerprops=None, manage_xticks=True):
例如,请参阅 usermedians:
usermedians : array-like or None (default)
An array or sequence whose first dimension (or length) is compatible with x. This overrides the medians computed by matplotlib for each element of usermedians that is not None. When an element of usermedians == None, the median will be computed by matplotlib as normal.
matplotlib.axes.boxplot
function actually calls two functions under the hood. One to compute the necessary statistics (cbook.boxplot_stats
) and one to actually draw the plot (matplotlib.axes.bxp
)。您可以通过为每个数据集调用第一个(通过一次加载一个)然后将结果提供给绘图函数来利用此结构。
在下面的这个例子中,我们有 3 个数据集并迭代它们以收集 cbook.boxplot_stats
的输出(只需要很少的内存)。在调用 ax.bxp
之后创建图表。 (在您的应用程序中,您将迭代加载文件,使用 boxplot_stats
并删除数据)
import matplotlib.cbook as cbook
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(10,10)
y = np.random.rand(10,10)
z = np.random.rand(10,10)
fig, ax = plt.subplots(1,1)
bxpstats = list()
for dataset, label in zip([x, y, z], ['X', 'Y', 'Z']):
bxpstats.extend(cbook.boxplot_stats(np.ravel(dataset), labels=[label]))
ax.bxp(bxpstats)
plt.show()
结果:
假设我有 4 个文件作为 .npz 文件保存在我的计算机上:W、X、Y 和 Z。 假设我的电脑在 RAM 消耗方面无法承受同时加载其中一个以上。
我怎样才能 运行 这个命令? :
matplotlib.pyplot.boxplot([W],[X],[Y],[Z])
换句话说,我如何加载 W、绘制 W、删除 W,然后加载 Y、绘制 Y、删除 Y,...并将其中的 4 个放在同一个图形上? (而不是子图)
谢谢!
一种选择是将数据的随机样本传递给绘图函数。
或者,因为箱线图仅包含聚合数据,所以您应该考虑单独计算这些聚合值,然后将它们应用于箱线图可视化。
使用 documentation 中的完整选项列表,您可以通过传递聚合数据来构建箱线图:
boxplot(self, x, notch=False, sym='b+', vert=True, whis=1.5,
positions=None, widths=None, patch_artist=False,
bootstrap=None, usermedians=None, conf_intervals=None,
meanline=False, showmeans=False, showcaps=True,
showbox=True, showfliers=True, boxprops=None, labels=None,
flierprops=None, medianprops=None, meanprops=None,
capprops=None, whiskerprops=None, manage_xticks=True):
例如,请参阅 usermedians:
usermedians : array-like or None (default)
An array or sequence whose first dimension (or length) is compatible with x. This overrides the medians computed by matplotlib for each element of usermedians that is not None. When an element of usermedians == None, the median will be computed by matplotlib as normal.
matplotlib.axes.boxplot
function actually calls two functions under the hood. One to compute the necessary statistics (cbook.boxplot_stats
) and one to actually draw the plot (matplotlib.axes.bxp
)。您可以通过为每个数据集调用第一个(通过一次加载一个)然后将结果提供给绘图函数来利用此结构。
在下面的这个例子中,我们有 3 个数据集并迭代它们以收集 cbook.boxplot_stats
的输出(只需要很少的内存)。在调用 ax.bxp
之后创建图表。 (在您的应用程序中,您将迭代加载文件,使用 boxplot_stats
并删除数据)
import matplotlib.cbook as cbook
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(10,10)
y = np.random.rand(10,10)
z = np.random.rand(10,10)
fig, ax = plt.subplots(1,1)
bxpstats = list()
for dataset, label in zip([x, y, z], ['X', 'Y', 'Z']):
bxpstats.extend(cbook.boxplot_stats(np.ravel(dataset), labels=[label]))
ax.bxp(bxpstats)
plt.show()
结果: