0

so i am trying to plot a uniform distribution using scipy.stats, but am running into this error, to do with shape transformation, could someone please tell me why this is happening / how my code should look, thank you.

Full Code and traceback below:

# 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()

FULL TRACEBACK:

ValueError Traceback (most recent call last) in 12 # create a list of values that follow uniform distribution 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 lines = mcoll.LineCollection(masked_verts, colors=colors,

\numpy\ma\core.py in setitem(self, indx, value) 3378 if _mask is nomask: 3379 # Set the data, then the mask -> 3380 _data[indx] = dval 3381 if mval is not nomask: 3382 _mask = self._mask = make_mask_none(self.shape, _dtype)

ValueError: could not broadcast input array from shape (1000) into shape (11)

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143

1 Answers1

0

You are trying to plot x with size n=10 and r with size size=1000. They have to be the same size.

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)

enter image description here

Max Pierini
  • 2,027
  • 11
  • 17