How to deal with "ValueError: x and y must have same first dimension" when defining a piecewise function? : I have written a small code in which I am producing a Gaussian PDF, namely p(y) and then I am trying to find the area under the curve numerically through the method of rectangular summation cumulatively between any two points y1 and y2 for which p(y) is known at any given number. My main problem is the way I have defined my function f(x) in the code so that it measures this area and plot it cumulatively for any grid point along p(y) axis. I am having trouble with the function and it is not doing what I am expecting. I would really appreciate if I can get any help with my code.

    import matplotlib
    from scipy.stats import norm
    import matplotlib.pyplot as pyplot
    import random
    import numpy as np
    from sympy import *
    from sympy.abc import x, y
    from sympy.utilities.lambdify import lambdify, implemented_function 
    from sympy import Function
    
    
    
    N  = 10                           # Number of sigmas away from central value
    M, K = 2**10, 2**10               # Number of grid points along y and p(y)
                         
    mean, sigma = 10.0, 1.0                         #Mean value and standard deviation of a Gaussian probability distribuiton (PDF)
    ymin, ymax = -N*sigma+mean, N*sigma+mean        #Minimum and maximum and spacing between grid points along y-axis
    ylims = [ymin, ymax]
    y = np.linspace(ylims[0],ylims[1],M)            #The values of y-axis on grid points
    pdf_y = norm.pdf(y,loc=mean,scale=sigma)        # Definiton of the normalized probability distribuiton function (PDF)
    
    max_p , min_p = max(pdf_y), min(pdf_y)          #The maximum, minimum value of p(y) 
    p = np.linspace(min_p, max_p, K)                #The values of p(y)-axis on grid points                                                                                               
    
    def area(p_j):                                  #Calculating the area under the PDF for which probability is more than pj-value for a particular jth index in pj_array above  
        for p_j in p.flat: 
            area = 0.0
            for yi in y.flat:             
                delta_y = (ymax - ymin) / (M-1)
                if (pdf_y[yi] > p_j): area += (delta_y * pdf_y.sum())
                else: area += 0.0
            return area 
    
    pyplot.plot(p, area(p))
    pyplot.axis([0, 1, 0, 1])
    pyplot.show()

Here is the error message as it appears:

>   File "example3.py", line 34, in <module>
    pyplot.plot(p, area(p))   File "/usr/lib/python3.4/site-packages/matplotlib/pyplot.py", line 2987, in plot
    ret = ax.plot(*args, **kwargs)   File "/usr/lib/python3.4/site-packages/matplotlib/axes.py", line 4139, in plot
    for line in self._get_lines(*args, **kwargs):   File "/usr/lib/python3.4/site-packages/matplotlib/axes.py", line 319, in
_grab_next_args
    for seg in self._plot_args(remaining, kwargs):   File "/usr/lib/python3.4/site-packages/matplotlib/axes.py", line 297, in
_plot_args
    x, y = self._xy_from_xy(x, y)   File "/usr/lib/python3.4/site-packages/matplotlib/axes.py", line 239, in
_xy_from_xy
    raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension

