python data visualization (matplotlib, scatter)

data visualization

1.matplotlib

Matplotlib probably Python 2D- drawing areas most widely used suite. It allows users to easily graphic data, and provides a variety of output formats. Here we will explore the common usage of matplotlib.

Installation matplotib

pip install -i https://pypi.douban.com/simple/ matplotlib

Test matplotib

$python
>>>import matplotlib
>>>
#没有错误信息输出,则表示matplotlib安装成功。

This may not identify pyCharm, perform the following operations

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

python data visualization (matplotlib, scatter)

Examples of a (line)

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show()

python data visualization (matplotlib, scatter)

Examples of di (Line)

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]

#修改线条的宽度: linewidth
plt.plot(squares,linewidth=5)

#设置图标的标题,并且给坐标轴加上标签
plt.title('queares number',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('quares value',fontsize=24)

# 设置刻度标记的大小
plt.tick_params(axis="both",labelsize=14)

plt.show()

python data visualization (matplotlib, scatter)

Three examples (lines)

import matplotlib.pyplot as plt

#捕入值
input_values = [1,2,3,4,5]
#输出值
squares = [1,4,9,16,25]

#修改线条的宽度: linewidth
plt.plot(input_values,squares,linewidth=5)

#设置图标的标题,并且给坐标轴加上标签
plt.title('queares number',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('quares value',fontsize=24)

# 设置刻度标记的大小
plt.tick_params(axis="both",labelsize=14)

plt.show()

python data visualization (matplotlib, scatter)

Four examples (single point)

import matplotlib.pyplot as plt

plt.scatter(2,4)
plt.show()

python data visualization (matplotlib, scatter)

Examples of five (single point)

import matplotlib.pyplot as plt

plt.scatter(2,4)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

plt.show()

python data visualization (matplotlib, scatter)

Examples of six (multi-point)

import matplotlib.pyplot as plt

x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]
plt.scatter(x_values,y_values,s=100)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

plt.show()

python data visualization (matplotlib, scatter)

Examples of seven (multi-point connection)

import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x ** 2 for x in x_values]
plt.scatter(x_values,y_values,s=100)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

#设置每个坐标轴的取值范围
plt.axis([0,1100,0,1100000])

plt.show()

python data visualization (matplotlib, scatter)

Analyze

python data visualization (matplotlib, scatter)

Examples of eight (multi-point connection, custom color)

# 自定义颜色
import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x ** 2 for x in x_values]
plt.scatter(x_values,y_values,c='red',s=100)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

#设置每个坐标轴的取值范围
plt.axis([0,1100,0,1100000])

plt.show()

python data visualization (matplotlib, scatter)

Example IX (multiple points, custom color)

# 自定义颜色
import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x ** 2 for x in x_values]

#参数c表示紅绿蓝3种颜色的分量
plt.scatter(x_values,y_values,c=(0,0.5,0.2),s=100)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

#设置每个坐标轴的取值范围
plt.axis([0,1100,0,1100000])

plt.show()

python data visualization (matplotlib, scatter)

Examples ten (multi-point connection, custom colors, gradient, save picture)

# 自定义颜色
import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x ** 2 for x in x_values]

# 将参数c设置为一个y值的列表,使用参数cmap告诉plot使用哪个颜色映射
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,s=100)

#设置图标标题,并且给坐标轴加上标签
plt.title('squares numbers',fontsize=24)
plt.xlabel('value',fontsize=24)
plt.ylabel('squares of value',fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis="both",which='major',labelsize=14)

#设置每个坐标轴的取值范围
plt.axis([0,1100,0,1100000])

# plt.show()

# bbox_inches='tight' -->将图表多余的空白区域哉减掉
# 保存图片为squares1.png
plt.savefig('squares1.png',bbox_inches='tight') 

python data visualization (matplotlib, scatter)

Examples of eleven (multiple points, custom colors, gradients, images saved)

import matplotlib.pyplot as plt

# plt.scatter(2,4)
x_values = list(range(1, 1001))
y_values = [x ** 2 for x in x_values]
# plt.scatter(x_values, y_values,c='red', s=50)
## 参数c表示红绿蓝3种颜色的分量
# plt.scatter(x_values, y_values,c=(0,0.5,0.2), s=50)
## 将参数c设置为一个y值的列表,使用参数cmap告诉plot使用哪个颜色映射
plt.scatter(x_values, y_values,c=y_values,cmap=plt.cm.Reds, s=50)

# 设置图标标题,并且 给坐标轴加上标签
plt.title('squares numbers', fontsize=24)
plt.xlabel('value', fontsize=24)
plt.ylabel('square of value', fontsize=14)

# 设置刻度的标记大小
plt.tick_params(axis='both', which='major', labelsize=14)

# 设置每个坐标轴的取值范围
plt.axis([0,1100,0,1100000])

# plt.show()
# 保存图片为squares22.png
plt.savefig('squares22.png',bbox_inches='tight')

python data visualization (matplotlib, scatter)

2, Random Walk

# 随机漫步
from random import choice

class RandomWalk():
    """-个生成随机漫步数据的类"""

    def __init__(self,num_points=5000):
        """初始化随机漫步的属性"""
        self.num_points = num_points

        # 所有随机漫步都始于(0,0)
        self.x_values = [0]
        self.y_values = [0]

    def fill_walk(self):
        """计算随机漫步包含的所有点"""

        # 不断漫步,直到列表达到指定的长度
        while len(self.x_values) < self.num_points:
            # 决定前进方向以及沿着这个方向前进的距离
            x_direction = choice([1,-1])
            x_distance = choice([0,1,2,3,4])
            x_step = x_direction * x_distance

            y_direction = choice([1,-1])
            y_distance = choice([0,1,2,3,4])
            y_step = y_direction * y_distance

            # 拒绝原地踏步
            if x_step == 0 and y_step ==0:
                continue

            # 计算下一个点的x和y的值
            next_x =self.x_values[-1] + x_step
            next_y =self.y_values[-1] + y_step

            #
            # 不断漫步,直到列表达到指定的长度
        while len(self.x_values) < self.num_points:
            # 决定前进方向以及沿着这个方向前进的距离
            x_direction = choice([1, -1])
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance

            y_direction = choice([1, -1])
            y_distance = choice([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance

            # 决绝原地踏步
            if x_step == 0 and y_step == 0:
                continue

            # 计算下一个点的x和y的值
            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)

Examples of a (random walk, custom color)

import matplotlib.pyplot as plt
from 示例.mpl_squares import RandomWalk

# 创建RandomWalk实例,并且将包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()

# 给点着色
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Greens,s=15)

# 隐藏边框
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)
plt.show()

python data visualization (matplotlib, scatter)

Examples of bis (random walk, whether to continue generating)

import matplotlib.pyplot as plt
from 示例.mpl_squares import RandomWalk

while True:
    # 创建RandomWalk实例,并且将包含的点都绘制出来
    rw = RandomWalk()
    rw.fill_walk()
    plt.scatter(rw.x_values, rw.y_values, s=15)

    # 隐藏边框
    # plt.axes().get_xaxis().set_visible(False)
    # plt.axes().get_yaxis().set_visible(False)

    plt.show()

    keep_running = input('继续漫步吗?(y/n)')
    if keep_running == 'n':
        break

python data visualization (matplotlib, scatter)

Output:
继续漫步吗?(y/n) y

python data visualization (matplotlib, scatter)

Examples of tris (random walk, control points, the distance between multiple points)

import matplotlib.pyplot as plt
from 示例.mpl_squares import RandomWalk

# 创建RandomWalk实例,并且将包含的点都绘制出来
rw = RandomWalk()
rw.fill_walk()

# 给点着色
point_numbers = list(range(rw.num_points))

plt.scatter(0,0,c='green',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',s=100)

# plt.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Greens,s=15)

# 隐藏边框
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)
plt.show()

python data visualization (matplotlib, scatter)

Example IV (random walk, the control points (the distance between the multi-point control) + Custom points)

import matplotlib.pyplot as plt
from 示例.mpl_squares import RandomWalk

# 创建RandomWalk实例,并且将包含的点都绘制出来
rw = RandomWalk(500000)
rw.fill_walk()

# 给点着色
point_numbers = list(range(rw.num_points))

plt.scatter(0,0,c='green',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',s=100)
plt.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Blues,s=1)

# 隐藏边框
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)

plt.figure(dpi=128, figsize=(10,6))
plt.show()

python data visualization (matplotlib, scatter)

Guess you like

Origin blog.51cto.com/14320361/2486144