python5 matplotlib数据可视化

matplotlib.pyplot.plot绘制线图

import matplotlib.pyplot as plt
y= [1, 4, 9, 16, 25]
x=[1, 2, 3, 4, 5]
plt.plot(x, y, linewidth=5)
plt.title("xtyxyyyy", fontsize=14)
plt.xlabel("Value", fontsize=14)
plt.ylabel("SquareS", fontsize=14)
plt.tick_params(axis='both', labelsize=14)
plt.show()

这里写图片描述
matplotlib.pyploy.scatter绘制散点图

import matplotlib.pyplot as plt
xv=list(range(1, 1000))
yv=[x*x for x in xv]
plt.scatter(xv, yv, s=10)
plt.axis([0,1000,0,1000000])
plt.show()

这里写图片描述
删除数据点轮廓

plt.scatter(xv, yv, s=10, edgecolors="none")

自定义颜色,可用RGB颜色模式和颜色函数(映射)

plt.scatter(xv, yv, s=10, edgecolors="none", c="red")
                                               颜色
plt.scatter(xv, yv, s=10, edgecolors="none", c=(0,0,0.8))
                                               RGB颜色
plt.scatter(xv, yv, s=10, edgecolors="none", c=xv,cmap=plt.cm.Reds)
                                                颜色函数

这里写图片描述
执行时自动保存

plt.savefig("xty.png", bbox_inches="tight")
              名字           裁剪空白区域

生成随机漫步散点图

from random import choice
import matplotlib.pyplot as plt

#设定一个随机漫步类,拥有散点数,起始位置属性
class RandomWalk():
    def __init__(self, num_points=5000):
         self.num_points = num_points
         self.x_value = [0]
         self.y_value = [0]

    def fill_walk(self):
        while len(self.x_value) < self.num_points:
            x_direction = choice([1, -1])
            x_distance = choice([0, 1, 2, 3, 4])
            y_direction = choice([1, -1])
            y_distance = choice([0, 1, 2, 3, 4])
            #步数等于方向乘以步长
            x_step = x_direction * x_distance
            y_step = y_direction * y_distance
            #放置原地踏步
            if x_step == 0 and y_step == 0:
                continue
            #下一步等于现在位置,加下一步
            next_x = self.x_value[-1] + x_step
            next_y = self.y_value[-1] + y_step
            #附加到列表中
            self.x_value.append(next_x)
            self.y_value.append(next_y)


while True:
    rw = RandomWalk()
    rw.fill_walk()
    #调整图像尺寸
    plt.figure(figsize=(10,6),dpi=100)
    #开始漫步
    plt.scatter(rw.x_value, rw.y_value, s=1,c=list(range(rw.num_points)),cmap=plt.cm.Blues)
    #设置起点
    plt.scatter(0,0,c="green",edgecolors="none",s=10)
    #设置终点
    plt.scatter(rw.x_value[-1],rw.y_value[-1],c="red",edgecolors="none",s=10)
    #隐藏坐标轴
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)
    #开始展示
    plt.show()
    #判断是否再次随机漫步
    keep_running = input("play again?(Y/N)")
    if keep_running == "n"or"N":
        break

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_41832686/article/details/80632966