数据可视化训练第一天(matplotlib直线;散点图,随机漫步)

前言

本人自己的练习记录;如有错误请指正;
https://matplotlib.org/stable/gallery/lines_bars_and_markers/index.html
官方有许多例子,可以找到自己需要的图像模仿进行绘制

1.一个简单的直线例子

就如同我们学习C语言的第一个helloword时候一样;我们也了解最基本的例子;关于具体细节可以不需要第一时间了解

import matplotlib.pyplot as plt
#准备数据
x_values=list(range(0,10))
y_values=list(range(0,10))
#绘制图像
fig,ax=plt.subplots()
plt.plot(x_values,y_values)
plt.show()

产生的直线图像
现在;你可以任意更改x_values与y_values的值;来画出一条简单的直线。

2.将简单的直线完善一下

parts of figure
通过这张图片,我们可以了解到更多东西。现在我们试着为这幅图像设置标题,x轴名字,y轴名字等属性

import matplotlib.pyplot as plt
#准备数据
x_values=list(range(0,10))
y_values=list(range(0,10))
#绘制图像
fig,ax=plt.subplots()

plt.plot(x_values,y_values,linewidth=10,c='red')
ax.set_title("example",fontsize=24)
ax.set_xlabel('x',fontsize=14)
ax.set_ylabel('y',fontsize=14)
#设置刻度的大小;axis=both表示x轴与y轴都选;大小变为14
#也可以axis='y'或者'x'
ax.tick_params(axis='both',labelsize=14)

plt.show()

3.插入figure(图形)和axes的介绍

可以简单的理解figure就是一个空白的图层,创建axes就是在里面创建坐标轴

fig=plt.figure()#一个空的图形对象;没有axes
fig,ax=plt.subplots()#一个图形对象对应一个axes
fig,axs=plt.subplots(2,2)#一个图像有四个网格的axes
#创建三个axes,一个在左侧;另外两个在右侧
fig,axs=plt.subplot_mosaic([['left','right_top'],['left','right_bottom']])
#这样使用子图层
axs['left'].set_title("left")

plt.show()

3绘制多条颜色不同的直线

import matplotlib.pyplot as plt
#准备数据
x_values=list(range(0,10))
y_values=list(range(0,10))
y_values1=[value**2 for value in range(0,10)]
y_values2=[value**3 for value in range(0,10)]
#绘制图像
fig,ax=plt.subplots()

ax.plot(x_values,y_values,linewidth=2,c='red')
ax.set_title("example",fontsize=24)
ax.set_xlabel('x',fontsize=14)
ax.set_ylabel('y',fontsize=14)
ax.tick_params(axis='both',labelsize=14)
ax.plot(x_values,y_values1,c='blue',linewidth=2)
ax.plot(x_values,y_values2,c='yellow',linewidth=2)

plt.show()

在这里插入图片描述

4绘制简单的散点图

import matplotlib.pyplot as plt
from random import randint

x_values=[randint(0,10) for i in range(0,10)]
y_values=[randint(0,20) for j in range(0,10)]

fig,ax=plt.subplots(figsize=(5,2.7))
ax.scatter(x_values,y_values,linewidth=2,c='red')
ax.set_title('san dian tu',fontsize=24)
ax.set_xlabel('x',fontsize=14)
ax.set_ylabel('y',fontsize=14)

plt.show()

在这里插入图片描述
我的中文显示有问题;这里用拼音
使用颜色映射;根据y值,进行从浅到深的映射

ax.scatter(x_values,y_values,linewidth=2,c=y_values,cmap=plt.cm.Reds)

5随机漫步实战

抽象一个漫步类,默认步数是5000,用scatter打印出来

from random import choice
import matplotlib.pyplot as plt
import matplotlib

class RandomWalk:
    """随机漫步类"""
    
    def __init__(self,num_points=5000):
        self.num_points=num_points
        self.x_values=[0]
        self.y_values=[0]
        
    def walk(self):
        
        while len(self.x_values) < self.num_points:
            x_direction=choice([-1,1])
            y_direction=choice([-1,1])
            x_distance=choice([0,1,2,3,4,5])
            y_distance=choice([0,1,2,3,4,5])
            x_step=x_direction*x_distance
            y_step=y_direction*y_distance
            
            #不允许原地踏步
            if x_step == 0 and y_step == 0:
                continue
            x=self.x_values[-1]+x_step
            y=self.y_values[-1]+y_step
            self.x_values.append(x)
            self.y_values.append(y)
            

num_points=5000
walkrandom=RandomWalk(num_points)
walkrandom.walk()

fig,ax=plt.subplots()
#保存各点的先后顺序
point_nums=range(walkrandom.num_points)
ax.scatter(walkrandom.x_values,walkrandom.y_values,s=3,c=point_nums,cmap=plt.cm.Blues,edgecolors='none')
ax.set_title('random walk')
ax.set_xlabel('x')
ax.set_ylabel('y')

#将开始点设置的醒目一些
ax.scatter(0,0,s=20,c='red')
#结尾点同理
ax.scatter(walkrandom.x_values[-1],walkrandom.y_values[-1],s=20,c='green')

#隐藏坐标轴
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

plt.show()
        

在这里插入图片描述

相关推荐

  1. 数据Matplotlib

    2024-05-09 08:02:08       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 08:02:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 08:02:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 08:02:08       82 阅读
  4. Python语言-面向对象

    2024-05-09 08:02:08       91 阅读

热门阅读

  1. C#语言进阶(四) 枚举器和迭代器

    2024-05-09 08:02:08       35 阅读
  2. Spring Boot配置类实例讲解

    2024-05-09 08:02:08       34 阅读
  3. git 常用命令及注释

    2024-05-09 08:02:08       25 阅读
  4. Github 2024-05-08 开源项目日报 Top10

    2024-05-09 08:02:08       39 阅读
  5. git对远程和本地分支进行重命名

    2024-05-09 08:02:08       30 阅读
  6. mybatis使用及配置相关,仅做个人记录

    2024-05-09 08:02:08       40 阅读
  7. 【Vue3】setup通过defineProps获取props为null

    2024-05-09 08:02:08       31 阅读
  8. AirSim 如何获取 settings 里面的传感器的数据

    2024-05-09 08:02:08       29 阅读
  9. 使用自关联方法处理多表关系

    2024-05-09 08:02:08       34 阅读
  10. 【CV】视频图像滤波技术

    2024-05-09 08:02:08       32 阅读