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

前言

本人自己的练习记录;如有错误请指正;

https://matplotlib.org/stable/gallery/lines_bars_and_markers/index.html

官方有许多例子,可以找到自己需要的图像模仿进行绘制

1.一个简单的直线例子

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

python 复制代码
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.将简单的直线完善一下

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

python 复制代码
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就是在里面创建坐标轴

python 复制代码
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绘制多条颜色不同的直线

python 复制代码
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绘制简单的散点图

python 复制代码
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值,进行从浅到深的映射

python 复制代码
ax.scatter(x_values,y_values,linewidth=2,c=y_values,cmap=plt.cm.Reds)

5随机漫步实战

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

python 复制代码
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()
        
相关推荐
小白学大数据44 分钟前
R语言爬虫实战:如何爬取分页链接并批量保存
开发语言·爬虫·信息可视化·r语言
kaikaile199514 小时前
使用Python进行数据可视化的初学者指南
开发语言·python·信息可视化
数据饕餮3 天前
Python数据分析基础03:探索性数据分析
python·信息可视化·数据分析
线条13 天前
Matplotlib 安装部署与版本兼容问题解决方案(pyCharm)
matplotlib
wx_ywyy67983 天前
《推客分销系统架构设计:从零搭建高并发社交裂变引擎》
信息可视化·推客系统·推客小程序·推客系统开发·推客小程序开发·推客分销系统
HuashuiMu花水木4 天前
Matplotlib笔记4----------图像处理
图像处理·笔记·matplotlib
云天徽上4 天前
【PaddleOCR】OCR常见关键信息抽取数据集,包含FUNSD、XFUND、WildReceipt等整理,持续更新中......
人工智能·计算机视觉·信息可视化·paddlepaddle·paddleocr·文本识别
杨超越luckly4 天前
ArcGISPro应用指南:ArcGISPro制图全流程详解
arcgis·信息可视化·gis·制图·arcgispro
GIS之路5 天前
GeoTools 结合 OpenLayers 实现属性查询(二)
前端·信息可视化
DataGear5 天前
如何在DataGear 5.4.1 中快速制作SQL服务端分页的数据表格看板
javascript·数据库·sql·信息可视化·数据分析·echarts·数据可视化