python绘制概率图_Python:Matplotlib-多个数据的概率图

这篇博客介绍了如何使用Python的matplotlib和scipy库绘制累积频率图,并展示了在同一图表上绘制多个数据集的方法。内容包括从正态分布生成数据、计算累积频率、设置子图以及使用百分位数进行绘图。
摘要由CSDN通过智能技术生成

我不太清楚你想要什么,所以我猜,这里。。。

你希望“概率/百分位数”值是累积直方图吗?

所以对于一个情节,你会有这样的东西?(使用上面显示的标记绘制,而不是更传统的阶梯图…)import scipy.stats

import numpy as np

import matplotlib.pyplot as plt

# 100 values from a normal distribution with a std of 3 and a mean of 0.5

data = 3.0 * np.random.randn(100) + 0.5

counts, start, dx, _ = scipy.stats.cumfreq(data, numbins=20)

x = np.arange(counts.size) * dx + start

plt.plot(x, counts, 'ro')

plt.xlabel('Value')

plt.ylabel('Cumulative Frequency')

plt.show()

如果这基本上就是你想要的一个图形,那么有多种方法可以在一个图形上绘制多个图形。最简单的方法就是使用子块。

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

import scipy.stats

import numpy as np

import 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 = 4

stds = 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 subplots

fig, 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 subplots

for 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 one

fig.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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值