QBoard » Advanced Visualizations » Viz - Python » Plotting error in Python

Plotting error in Python

  • I am trying to plot a simple function in python ( x + sqrt(x^2 + 2x) ). Here is my code:

    import pylab as pl
    import numpy as np
    import math
    X = np.linspace(-999999,999999)
    Y = (X+math.sqrt(X**2+2*X))
    pl.plot(X,Y)
    pl.show()​

    Here is the error that I am facing: TypeError: only length -1 arrays can be converted to Python scalars This post was edited by Shivakumar Kota at September 24, 2020 12:28 PM IST
      June 11, 2019 5:05 PM IST
    0
  • The problem with your approach is that the math.sqrt function expects a single number as an input. Since you have a numpy-array with more than one element you should use the built-in numpy function sqrt as suggested by Rory Yorke i.e.

    Y = X+np.sqrt(X**2+2*X​

    Since the numpy function is much more optimized it gives you a better performance, for example:

    X = np.arange(10000)
    timeit Y = map(lambda x: (x+math.sqrt(x**2+2*x)), X)
    10 loops, best of 3: 82.1 ms per loop
    timeit Y = X+np.sqrt(X**2+2*X) ​

    10000 loops, best of 3: 108 µs per loop This post was edited by Pranav B at September 24, 2020 12:30 PM IST
      June 14, 2019 12:45 PM IST
    0
  • The errorbar() function in pyplot module of matplotlib library is used to plot y versus x as lines and/or markers with attached errorbars.

    Syntax: matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt=”, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, \*, data=None, \*\*kwargs)

    Parameters: This method accept the following parameters that are described below:

    • x, y: These parameter are the horizontal and vertical coordinates of the data points.
    • fmt: This parameter is an optional parameter and it contains the string value.
    • xerr, yerr: These parameter contains an array.And the error array should have positive values.
    • ecolor: This parameter is an optional parameter. And it is the color of the errorbar lines with default value NONE.
    • elinewidth: This parameter is also an optional parameter. And it is the linewidth of the errorbar lines with default value NONE.
    • capsize: This parameter is also an optional parameter. And it is the length of the error bar caps in points with default value NONE.
    • barsabove: This parameter is also an optional parameter. It contains boolean value True for plotting errorsbars above the plot symbols.Its default value is False.
    • lolims, uplims, xlolims, xuplims: These parameter are also an optional parameter. They contain boolean values which is used to indicate that a value gives only upper/lower limits.
    • errorevery: This parameter is also an optional parameter. They contain integer values which is used to draws error bars on a subset of the data.
      September 14, 2021 1:36 PM IST
    0
  • use

    Y = [(x+math.sqrt(x**2+2*x)) for x in X]

    or

    Y = map(lambda x: (x+math.sqrt(x**2+2*x)), X)

    to generate a list of y-values.

    You can also vectorize your function and apply it. See List comprehension, map, and numpy.vectorize performance for additional remarks.
    shareimprove this answer
      June 11, 2019 5:08 PM IST
    0