0

所以我正在尝试使用 scipy.stats 绘制均匀分布,但我遇到了这个错误,与形状转换有关,有人可以告诉我为什么会这样/我的代码应该看起来如何,谢谢。

完整代码和回溯如下:

# UNIFORM DISTRIBUTION:

from scipy.stats import uniform
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)

n = 10  # total data number

x = list(range(0, n + 1))

# create a list of values that follow uniform distribution
r = uniform.rvs(size=1000)
ax.vlines(x, 0, r, colors = "blue")
plt.show()

完整的追溯:

ValueError Traceback (most recent call last) in 12 # 创建一个遵循均匀分布的值列表 13 r = uniform.rvs(size=1000) ---> 14 ax.vlines(x, 0, r, colors = "blue ") 15 plt.show() 16

\matplotlib_ init _.py in inner(ax, data, *args, **kwargs) 1436 def inner(ax, *args, data=None, **kwargs): 1437 if data is None: -> 1438 return func( ax, *map(sanitize_sequence, args), **kwargs) 1439 1440 bound = new_sig.bind(ax, *args, **kwargs)

\matplotlib\axes_axes.py in vlines(self, x, ymin, ymax, colors, linestyles, label, **kwargs)
1257 masked_verts[:, 0, 1] = ymin 1258
masked_verts[:, 1, 0] = x - > 1259 masked_verts[:, 1, 1] = ymax 1260 1261 行 = mcoll.LineCollection(masked_verts, colors=colors,

\numpy\ma\core.py in setitem (self, indx, value) 3378 if _mask is nomask: 3379 # 设置数据,然后掩码 -> 3380 _data[indx] = dval 3381 if mval is not nomask: 3382 _mask = self._mask = make_mask_none(self.shape, _dtype)

ValueError:无法将输入数组从形状(1000)广播到形状(11)

4

1 回答 1

0

您正在尝试x使用 sizen=10rsize进行绘图size=1000。它们的大小必须相同。

import numpy as np
import scipy.stats as sps

import matplotlib.pyplot as plt

n = 1000
x = range(n)
# define the distribution
# by default [0,1]
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.uniform.html
dist = sps.uniform()
# generate n random variates
r = dist.rvs(n)

plt.plot(x, r)

在此处输入图像描述

于 2021-03-27T16:20:24.837 回答