Hướng dẫn dùng python loglog python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: Matplotlib

    Matplotlib is a comprehensive library for creating interactive, static and animated visualizations in python. Using general-purpose GUI toolkits like wxPython, SciPy, Tkinter or SciPy, it provides an object-oriented API for embedding plots into applications. Matplotlib.pyplot is a collection of functions that makes Matplotlib work like MATLAB.

    Here, we will be exploring loglog() function of Matplotlib.pyplot. It is used to plot a log scale over both x and y-axis.

    Syntax:

    loglog(X,Y)

    Where,

    X and Y refer to x and y coordinates respectively.

    Other function used is linespace(). It returns evenly spaced numbers over a specified interval.

    Syntax:

    np.linspace(start, stop, num, endpoint, retstep, dtype, axis)

    Where,

    • Start : The starting value of sequence from where you want to show the line, or we can say starting point of line
    • Stop : It is the end value of the sequence at where the line stops, unless ‘endpoint’ is set to False.
    • Num : Number of samples to generate. Must be non-negative. By default, it is 50.
    • Endpoint : It works same as stop. If it is True then stop is the last sample else stop is excluded from the sequence.
    • Retstep : If True, return (‘samples’, ‘step’), where `step` is the spacing between samples.
    • Dtype : The type of the output array.
    • Axis : The axis in the result to store the samples and it is relevant only if start or stop are array-like

    Example : Without loglog()

    Python

    import matplotlib.pyplot as plt

    import numpy as np

    x_input = np.linspace(0, 10, 50000)

    y_input = x_input**8

    plt.plot(x_input, y_input)

    Output: 

    Hướng dẫn dùng python loglog python

    Example : With loglog()

    Python3

    import matplotlib.pyplot as plt

    import numpy as np

    x_input = np.linspace(0, 10, 50000)

    y_input = x_input**8

    plt.loglog(x_input, y_input)

    Output:

    Hướng dẫn dùng python loglog python


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

    matplotlib.axes.Axes.loglog() Function

    The Axes.errorbar() function in axes module of matplotlib library is used to make a plot with log scaling on both the x and y axis.

    Syntax:

    Axes.loglog(self, *args, **kwargs)

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

    • basex, basey: These parameter are Base of the x/y logarithm and are optional with default value 10.
    • subsx, subsy: These parameter are the sequence of location of the minor x/y ticks and are optional.
    • nonposx, nonposy: These parameter are non-positive values in x or y that can be masked as invalid, or clipped to a very small positive number.

    Returns: This returns the following:

    • lines:This returns the list of Line2D objects representing the plotted data..

    Below examples illustrate the matplotlib.axes.Axes.loglog() function in matplotlib.axes:

    Example-1:

    import numpy as np

    import matplotlib.pyplot as plt

    t = np.arange(0.01, 20.0, 0.01)

    fig, ax = plt.subplots()

    ax.loglog(t, 20 * np.exp(-t / 10.0), basex = 2)

    ax.set_title('matplotlib.axes.Axes.loglog Example2')

    plt.show()

    Output:

    Hướng dẫn dùng python loglog python

    Example-2:

    import numpy as np

    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(constrained_layout = True)

    x = np.arange(0.02, 1, 0.02)

    np.random.seed(19680801)

    y = np.random.randn(len(x)) ** 2

    ax.loglog(x, y)

    ax.set_xlabel('f [Hz]')

    ax.set_ylabel('PSD')

    ax.set_title('Random spectrum')

    def forward(x):

        return 1 / x

    def inverse(x):

        return 1 / x

    ax.set_title('matplotlib.axes.Axes.loglog Example2')

    plt.show()

    Output:

    Hướng dẫn dùng python loglog python