A discussion on the Pyplot module of Python actual combat data visualization

Source: Internet
Author: User
Tags manual writing

Frontier

Python provides a number of modules for data visualization, including Matplotlib, Pygal. I refer to the online popular books "Python programming from the beginning to the actual combat", in the test and learning process encountered a few problems to solve, just write down this project experience, for the basic part of the Python is not detailed, mainly the project core points and solutions described. This section first describes the basic use of the Pyplot module.

Tips for Beginners

For beginners, I really feel that you do not directly use the Python download to develop the idle, because the function is too little, also not good use. My advice is for Python beginners, first install Anaconda, which is a python-based data processing and scientific computing platform, it has built up a number of very useful third-party libraries, we installed anaconda, it is equivalent to the dozens of third-party modules to install automatically, Very easy to use, add anaconda to the PATH environment variable in the installation interface, so that the environment variable is added automatically. Anaconda comes with an editor-spyder, and can write code using the Spyder, knowing that this editor is good. Then install a pycharm, which is a Python IDE with a complete set of tools to help users improve their efficiency when developing with the Python language.
Anaconda and Pycharm installation procedures and methods of documentation links, I am a summary of useful information on the Internet to summarize: Link: https://pan.baidu.com/s/10KcfLLvI9omIRSJ6JMK9Uw Password: cgf8

Draw a line chart using the plot function of the Pyplot module

We first import the module Pyplot, then use the plot function of the module to draw a line chart, and then call the module's related functions to adjust, set the chart title, horizontal label, tick mark content or size. Note that the plot function of the Pyplot module can receive input and output parameters, as well as line weights, but if the plot function specifies only the output parameter (list), the input parameter defaults to 0.
(1) The plot function specifies the output parameter (1,2,3,4,5 table of squares)

import matplotlib.pyplot as plt# pyplot模块的plot函数可以接收输入参数和输出参数,还有线条粗细等参数,,例如下方的示例squares = [1, 4, 9, 16, 25]plt.plot(squares, linewidth=5)  # 这里只指定了一个列表,那么就当作是输出参数,输入参数从0开始,就会发现没有正确绘制数据plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.tick_params(axis=‘both‘, labelsize=14)  # 参数axis值为both,代表要设置横纵的刻度标记,标记大小为14plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

(2) The plot function specifies input parameters and output parameters
We know that the above does not draw the graph according to our wishes, the Y axis is specified as [1,4,9,16,25], and the default input parameter is used to process the x-axis into [0,1,2,3,4]. The x-axis should have a corresponding value of [1,2,3,4,5] for our purposes, so we must specify both input and output parameters. Observe the x-axis change of the running result graph.

import matplotlib.pyplot as plt# 我也可以指定输入参数和输出参数,这样就能按照我的意愿绘制图形了input_values = [1, 2, 3, 4, 5]  # 指定输入参数squares = [1, 4, 9, 16, 25]  # 指定输出参数plt.plot(input_values, squares, linewidth=5)  # 调用绘制函数,传入输入参数和输出参数plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.tick_params(axis=‘both‘, labelsize=14)  # 参数axis值为both,代表要设置横纵的刻度标记,标记大小为14plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

Drawing scatter plots using the scatter function of the Pyplot module

Plotting a scatter plot is just a different way of drawing a function, from the above plot to scatter, to other settings such as headings, horizontal labels, and so on.
(1) Scatter function draws a single point

import matplotlib.pyplot as pltplt.scatter(2, 4, s=200)  # 传递一对x和y坐标。它将在指定位置绘制一个点,参数s是设置绘制图形时使用的点的尺寸plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.tick_params(axis=‘both‘, labelsize=14)  # 参数axis值为both,代表要设置横纵的刻度标记,标记大小为14plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

(2) Scatter function draws a series of points

import matplotlib.pyplot as pltx_values = [1, 2, 3, 4, 5]y_values = [1, 4, 9, 16, 25]plt.scatter(x_values, y_values, s=100)  # 传入两个列表,列表x_values的元素作为x坐标,列表y_values的元素作为y坐标,两个组合成一个点的坐标,所以一共有5个点plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.tick_params(axis=‘both‘, labelsize=14)  # 参数axis值为both,代表要设置横纵的刻度标记,标记大小为14plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

(3) automatic calculation of y-axis data
The previous two examples are quite simple, but also just our own definition of the short list, if you want to draw a lot of points, then also manual writing of course not practical, so we write a according to our designated x-axis data, automatic calculation of the y-axis data is much more convenient.

import matplotlib.pyplot as pltx_values = list(range(1, 1001))  # 我们是利用range函数生成一个从1到1000的可迭代对象(不包括1001),然后强制转换为列表y_values = [x ** 2 for x in x_values]  # 这个语法是列表推导式,将x_values每个元素的值进行平方再逐一放入列表,最后这个列表推导式返回整个列表plt.scatter(x_values, y_values, s=40)  # 在2.0.0版本后的matplotlib中,scatter()函数的实参edgecolor(数据点的轮廓)默认为‘none‘,则删除轮廓。plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.axis([0, 1100, 0, 1100000])  # 设置每个坐标轴的取值范围。其实最右侧就是1100,但是没有显示标签而已plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

Note that the scatter () function's argument edgecolor (the outline of the data point) in Matplotlib after 2.0.0 is not written by default to ' None ', which represents the deletion of the contour. You can also modify the code by:

plt.scatter(x_values, y_values, s=40, edgecolor=‘red‘)

The above modified code specifies that the color of the data point outline is red, because the default point color is blue, so you will see the following, only the upper right corner is blue, the other is red, this is because the drawing of a lot of points, the red outline is glued together, so it is not visible.
After modifying the code, run the following results:

It said that the default data point color is blue, we can also change the color of the data point by the parameter C, as for the color value can be directly written in English such as ' red ', ' black ', or use the RGB color mode to customize the color, this custom color is set to a tuple, which contains three 0~ A small number between 1, which represents red, green, and blue components, such as (0,0,0.8). You can modify the code by doing the following:

import matplotlib.pyplot as pltx_values = list(range(1, 1001))y_values = [x ** 2 for x in x_values]plt.scatter(x_values, y_values, c=(0, 0.8, 0), s=40)  # 指定了c参数,使用的是RGB颜色值方式plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.axis([0, 1100, 0, 1100000])  # 设置每个坐标轴的取值范围。其实最右侧就是1100,但是没有显示标签而已plt.show()  # 打开matplotlib查看器,并显示绘制的图形

The results of the operation are as follows:

(4) Use color mapping and auto Save chart
The description of the use of the Color Map section on the P294 page of the book may be a bit ambiguous for beginners, where my own code is used to understand what color mapping is, and what the principle is.
The test code is as follows:

import matplotlib.pyplot as pltx_values = [1, 2, 3, 4, 5]  # 含x值的列表y_values = [1, 4, 2, 6, 5]  # 含y值的列表#  我们知道根据上面两个列表,我们调用scatter可以绘制一系列的点# 模块pyplot内置了一组颜色映射,通过设置c参数为y列表的值(这个y列表的是[1,2,3,4,5])然后利用参数cmap根据y列表的大小映射到由x_values和y_values组成的五个点从浅到深的颜色,可以看出y列表[1,2,3,4,5]分别映射到(1,1),(2,4),(3,2),(4,6),(5,5)五个点,其中(1,1)点颜色最浅,(5,5)点颜色最深。plt.scatter(x_values, y_values, c=[1, 2, 3, 4, 5], cmap=plt.cm.Blues, s=100)plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.savefig("3.png", bbox_inches=‘tight‘)  # 打开matplotlib查看器,并显示绘制的图形# 值的注意的是,要让程序自动将图表保存到文件中,可将对plt.show()的调用替换为对plt.savefig()的调用。# 如果指定了bbox_inches=‘tight‘将图表多余的空白区域裁剪掉,明显更符合用户需求,如果没指定,生成的图片显示不出Y轴的标签。

The results are as follows (note that there is a very light blue point in the location of the () point, just shallow enough to see):

If you do not understand, then you can modify the following test code to run the observation:

plt.scatter(x_values, y_values, c=[1, 5, 3, 4, 5], cmap=plt.cm.Blues, s=100)

The result of the operation is as follows (you can see that the color of the (2,4) point becomes dark blue, too):

With the above understanding of the basis, we can modify the source code for the book, to observe the shadow of the Blue Comet, haha o (∩_∩) o
Book Source code:

import matplotlib.pyplot as pltx_values = list(range(1,1001)) # 含x值的列表y_values = [x ** 2 for x in x_values]  # 含y值的列表#  我们知道根据上面两个列表,我们调用scatter可以绘制一系列的点# 根据y列表的值大小进行颜色映射的,值大的颜色深,值小的颜色浅。如果y列表的值按顺序,并且映射到按顺序的点,那么自然颜色也是从浅到深。# 模块pyplot内置了一组颜色映射,通过设置c参数为y列表的值(这个y列表的是[1,2,3,4,5])然后利用参数cmap根据y列表的大小映射到由x_values和y_values组成的五个点从浅到深的颜色,可以看出y列表[1,2,3,4,5]分别映射到(1,1),(2,4),(3,2),(4,6),(5,5)五个点,其中(1,1)点颜色最浅,(5,5)点颜色最深。plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=40)plt.title("Square Numbers", fontsize=24)  # 指定标题,并设置标题字体大小plt.xlabel("Value", fontsize=14)  # 指定X坐标轴的标签,并设置标签字体大小plt.ylabel("Square of Value", fontsize=14)  # 指定Y坐标轴的标签,并设置标签字体大小plt.savefig("3.png", bbox_inches=‘tight‘)  # 打开matplotlib查看器,并显示绘制的图形

The running result diagram is as follows (Blue Comet remnant):

Using the scatter function of the Pyplot module to draw a random walk chart

To paraphrase a book, a random walk: Each walk is completely random, with no definite direction, and the result is determined by a series of random decisions.
In order to achieve a random walk, you need to do the following steps to complete:
1. Create a Randomwalk class to generate random walk data
2. Draw a random walk chart with the obtained random walk data
3. Simulate multiple random walks
4. Set the style of the Random walk chart
(1) Create a Randomwalk class to generate random walk data
Create a random_walk.py file in the project with the following code:

from random import choice# 一个生成随机漫步数据的类class RandomWalk:    # 默认为5000个点,代表5000步    def __init__(self, num_points=5000):        self.num_points = num_points        self.x_values = [0]        self.y_values = [0]        # 获取随机方向和步数的乘积    def get_step(self):        return choice([1, -1]) * choice([0, 1, 2, 3, 4])    def fill_walk(self):        while len(self.x_values) < self.num_points:                    # 获取往哪个方向走几步            x_step = self.get_step()            y_step = self.get_step()                        # 如果原地踏步则continue处理            if x_step == 0 and y_step == 0:                continue                        # 计算下一步走的位置            next_x = self.x_values[-1] + x_step            next_y = self.y_values[-1] + y_step                        # 将下一次走的位置保存在列表中            self.x_values.append(next_x)            self.y_values.append(next_y)

(2) Draw random walk graph using the random walk data obtained
Create a rw_visual.py file in the project with the following code:

import matplotlib.pyplot as pltfrom random_walk import RandomWalk# 创建一个RandomWalk实例,并指定走5000步rw = RandomWalk(5000)rw.fill_walk()  # 开始获取随机漫步数据,其实获取的是两个包含x和y值的数据点列表plt.scatter(rw.x_values, rw.y_values, s=15)plt.show()

The results of the operation are as follows:

(3) Simulate multiple random walks
Modify the code on the rw_visual.py file as follows:

import matplotlib.pyplot as pltfrom random_walk import RandomWalkwhile True:    rw = RandomWalk(5000)    rw.fill_walk()    plt.scatter(rw.x_values, rw.y_values,s=15)    plt.show()    keep_running = input("Make another walk?(y/n):")    if keep_running == ‘n‘:        break

The results are as follows (you can click the Close button in the upper right corner and ask if you want to continue strolling below Pycharm):

(4) Set the style of the Random walk chart
We'll set the style of a random walk chart, customize a nice looking random walk chart, have coloring for points, redraw the start and end points, hide the axes, resize to fit the screen.

import matplotlib.pyplot as pltfrom random_walk import RandomWalkwhile True:    rw = RandomWalk(5000)    rw.fill_walk()    plt.figure(figsize=(20, 6))    # 颜色映射就是用列表赋值给c,而这个列表的值可以随意,大的代表颜色深,小的代表颜色浅。    point_number = list(range(rw.num_points))  # [0-4999]    plt.scatter(rw.x_values, rw.y_values, c=point_number, cmap=plt.cm.Blues, s=4)    # 突出起点和终点,点变大,用不同颜色来显示起点和终点    plt.scatter(0, 0, c=‘green‘, s=100)    plt.scatter(rw.x_values[-1], rw.y_values[-1], c=‘red‘, s=100)    # 隐藏坐标轴    plt.axes().get_xaxis().set_visible(False)    plt.axes().get_yaxis().set_visible(False)    plt.show()    keep_running = input("Make another walk?(y/n):")    if keep_running == ‘n‘:        break

The results of the operation are as follows:

A discussion on the Pyplot module of Python actual combat data visualization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.