栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python:Matplotlib-多个数据集的概率图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python:Matplotlib-多个数据集的概率图

我不清楚您想要什么,所以我想在这里…

您希望“概率/百分比”值是累积直方图吗?

因此,对于一个地块,您会有类似的东西吗?(如上所示,用标记绘制它,而不是更传统的阶梯图…)

import scipy.statsimport numpy as npimport matplotlib.pyplot as plt# 100 values from a normal distribution with a std of 3 and a mean of 0.5data = 3.0 * np.random.randn(100) + 0.5counts, start, dx, _ = scipy.stats.cumfreq(data, numbins=20)x = np.arange(counts.size) * dx + startplt.plot(x, counts, 'ro')plt.xlabel('Value')plt.ylabel('Cumulative Frequency')plt.show()

如果这大致是单个图所需要的,则有多种方法可以在一个图形上绘制多个图。最简单的就是使用子图。

在这里,我们将生成一些数据集,并将它们绘制在具有不同符号的不同子图中。

import itertoolsimport scipy.statsimport numpy as npimport matplotlib.pyplot as plt# Generate some data... (Using a list to hold it so that the datasets don't # have to be the same length...)numdatasets = 4stds = np.random.randint(1, 10, size=numdatasets)means = np.random.randint(-5, 5, size=numdatasets)values = [std * np.random.randn(100) + mean for std, mean in zip(stds, means)]# Set up several subplotsfig, axes = plt.subplots(nrows=1, ncols=numdatasets, figsize=(12,6))# Set up some colors and markers to cycle through...colors = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])markers = itertools.cycle(['o', '^', 's', r'$Phi$', 'h'])# Now let's actually plot our data...for ax, data, color, marker in zip(axes, values, colors, markers):    counts, start, dx, _ = scipy.stats.cumfreq(data, numbins=20)    x = np.arange(counts.size) * dx + start    ax.plot(x, counts, color=color, marker=marker,  markersize=10, linestyle='none')# Next we'll set the various labels...axes[0].set_ylabel('Cumulative Frequency')labels = ['This', 'That', 'The Other', 'And Another']for ax, label in zip(axes, labels):    ax.set_xlabel(label)plt.show()

如果我们希望它看起来像一个连续的图,则可以将子图挤压在一起并关闭一些边界。只需在调用之前添加以下内容

plt.show()

# Because we want this to look like a continuous plot, we need to hide the# boundaries (a.k.a. "spines") and yticks on most of the subplotsfor ax in axes[1:]:    ax.spines['left'].set_color('none')    ax.spines['right'].set_color('none')    ax.yaxis.set_ticks([])axes[0].spines['right'].set_color('none')# To reduce clutter, let's leave off the first and last x-ticks.for ax in axes:    xticks = ax.get_xticks()    ax.set_xticks(xticks[1:-1])# Now, we'll "scrunch" all of the subplots together, so that they look like onefig.subplots_adjust(wspace=0)

希望这可以有所帮助!

编辑:如果您想要百分位值,而不是累积直方图(我真的不应该使用100作为样本量!),这很容易做到。

只需执行以下操作即可(使用

numpy.percentile
而不是手动进行标准化):

# Replacing the for loop from before...plot_percentiles = range(0, 110, 10)for ax, data, color, marker in zip(axes, values, colors, markers):    x = np.percentile(data, plot_percentiles)    ax.plot(x, plot_percentiles, color=color, marker=marker,  markersize=10, linestyle='none')



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号