生成数据

绘  图

参考:https://blog.csdn.net/anneqiqi/article/details/64125186

折线图

import matplotlib.pyplot as plt

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

修改标签文字、线条粗细

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]
plt.plot(squares, linewidth = 5)

# 设置标签
plt.title("Square Number", fontsize = 24)
plt.xlabel("Value", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

校正图形

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)  # 第一个参数为x轴的值即输入值
"""
plot()函数默认第一个数据点为(0, 0)
"""

# 设置标签
plt.title("Square Number", fontsize = 24)
plt.xlabel("Value", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

使用 scatter() 函数绘制散点图、设置其样式

import matplotlib.pyplot as plt

plt.scatter(2, 4)   # 设置一个坐标
plt.show()
import matplotlib.pyplot as plt

plt.scatter(2, 4, s = 200)   # 设置一个坐标

plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Valve", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

使用 scatter() 绘制一系列的点

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)    # s参数表示点的直径

plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

自动计算数据

"""自动计算数据"""

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 = 4)


plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

删除数据点的轮廓

"""自动计算数据"""

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, edgecolor = 'none', s = 4)
# edgecolor 参数蓝色点黑色轮廓,去掉黑色轮廓后显示为蓝色实心点


plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

自定义颜色

"""自动计算数据"""

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', edgecolor = 'none', s = 4)
# edgecolor 参数蓝色点黑色轮廓,去掉黑色轮廓后显示为蓝色实心点
# c 参数表示颜色,可以用元组 RGB 值表示颜色


plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

使用颜色映射

"""自动计算数据"""

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 = y_values,cmap = plt.cm.Blues, edgecolor = 'none', s = 4)
# c 参数的值越大,blues 颜色的值越深,渐变


plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

自动保存图表

"""自动计算数据"""

import matplotlib.pyplot as plt

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

plt.scatter(x_values, y_values, c = y_values,cmap = plt.cm.Blues, edgecolor = 'none', s = 4)
# c 参数的值越大,blues 颜色的值越深,渐变


plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)

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

plt.show()

plt.savefig('xieyi.png', bbox_inches = 'tight')
# 自动保存图表,第二个参数裁剪图表周围多余空白区域

随机漫步

from random import choice

import matplotlib.pyplot as plt

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

    def __init__(self, num_points = 500):
        self.num_points = num_points

        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

            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)

rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s = 10)

plt.title("Numbers", fontsize = 24)
plt.xlabel("Values", fontsize = 20)
plt.ylabel("Numbers of Values", fontsize = 20)

plt.show()

模拟多次随机漫步

from random import choice

import matplotlib.pyplot as plt

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

    def __init__(self, num_points = 500):
        self.num_points = num_points

        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

            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)

while True:
    rw = RandomWalk()
    rw.fill_walk()
    plt.scatter(rw.x_values, rw.y_values, s = 10)

    plt.title("Numbers", fontsize = 24)
    plt.xlabel("Values", fontsize = 20)
    plt.ylabel("Numbers of Values", fontsize = 20)

    plt.show()

    keep_runing = input("Make another walk?(y/n):")
    if keep_runing == 'n':
        break

设置随机漫步图的样式

猜你喜欢

转载自www.cnblogs.com/xieyi-newlife/p/9064432.html
今日推荐