Python T-04

版权声明:本文为博主(離桜|LostSakura)原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq282465134/article/details/81288229

test11.py

# pyplot存有大量绘制函数图像的方法
import matplotlib.pyplot as plt

input_value = [1, 2, 3, 4, 5, 6, 7, 8, 9]
squares = [1, 4, 9, 16, 25, 36, 49, 64, 81]
# 载入数据 可以同时传入x和y两轴的数据,也可以只传入y轴,x默认从零开始
plt.plot(input_value, squares, linewidth=5)

# 设置图表标题
plt.title("Squares Numbers", fontsize=24)
plt.xlabel("Value", fontsize=24)
plt.ylabel("Square of Value", fontsize=24)

# 设置刻度标记的大小 axis表示影响xy两轴都影响
plt.tick_params(axis='both', labelsize=14)

# 打开查看器
plt.show()

运行结果: 

运行后的图表图像


test12.py 

import matplotlib.pyplot as plt

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

# 绘制散点 x轴,y轴,颜色,是否要轮廓,大小
plt.scatter(x_values, y_values, c='red', edgecolors='none', s=40)
# 设置标题 并加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=24)
plt.ylabel("Value of Square", fontsize=24)

# 设置刻度
plt.tick_params(axis='both', which='major', labelsize=14)

plt.show()

运行结果: 

运行所得图表


test13.py 

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

            self.x_values.append(next_x)
            self.y_values.append(next_y)

test14.py 

import matplotlib.pyplot as plt

from test13 import RandomWalk
# 创建一个实例并绘制图像
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s=15)
plt.show()

运行结果: 

运行后所得图表


 2018/07/30

猜你喜欢

转载自blog.csdn.net/qq282465134/article/details/81288229