DEV Community

natamacm
natamacm

Posted on

Matplotlib and data visualization

Data processing, analysis and visualization has become one of the most important applications of Python in recent years.

Data visualization refers to presenting data as beautiful statistical charts and then further discovering the laws and hidden information contained in the data.

Data visualization is closely related to data mining and big data analytics, and the ultimate goal of these areas, as well as the much-talked-about "deep learning," is to predict the future from past data.

Installing matplotlib

PIP can be used to install matplotlib with the following command.

pip install matplotlib

Plotting

The code to draw a line plot is shown below:

# coding: utf-8
import matplotlib.pyplot as plt
def main():
    x_values = [x for x in range(1, 11)]
    y_values = [x ** 2 for x in range(1, 11)]

    plt.title('Square Numbers')
    plt.xlabel('Value', fontsize=18)
    plt.ylabel('Square', fontsize=18)
    plt.tick_params(axis='both', labelsize=16)
    plt.plot(x_values, y_values)
    plt.show()
if __name__ == '__main__':
    main()

matplotlib line chart

You can make all kinds of line charts with Python.

You can draw a histogram with matplotlib too.

We can use the normal function of NumPy's random module to generate normally distributed sampling data, where the three parameters represent the expectation, standard deviation and sample size, respectively, and then plot them as histograms, as shown in the code below.

# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
def main():
    data = np.random.normal(10.0, 5.0, 1000)
    plt.hist(data, 10)
    plt.show()
if __name__ == '__main__':
    main()

histogram

Related links:

Top comments (0)