当前位置: 首页 > news >正文

python数据可视化Matplotlib

1.绘制简单的折线图

# -*- coding: utf-8 -*-
import matplotlib.pyplot as pltinput_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(input_values, squares, linewidth=3)   # 线条粗细# 设置图表标题并给坐标轴加标签
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("值", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)# 设置刻度标记大小
ax.tick_params(axis='both', labelsize=14)
# 在代码中添加如下语句 —— 设置字体为:SimHei(黑体)
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签(中文乱码问题)plt.show()

效果图:

 2.使用内置样式绘制散点图,自定义颜色,颜色映射,自动保存图片

import matplotlib.pyplot as pltx_values = range(1, 1001)
y_values = [x**2 for x in x_values]
plt.style.use('seaborn')
fig, ax = plt.subplots()
# 颜色映射cmap=plt.cm.Blues
ax.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=10)#设置图标标题并给坐标轴加标签
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("值", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)# 设置每个坐标轴的取值范围
ax.axis([0, 1100, 0, 1100000])
# 设置刻度标记的大小
ax.tick_params(axis='both', which='major', labelsize=14)
# 正常显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']# 显示
# plt.show()
# 保存
plt.savefig('squares_plot.png', bbox_inches='tight')

结果:

 3.随机漫步

random_walk.py

from random import choiceclass RandomWalk:def __init__(self, num_points=5000):self.num_point = num_pointsself.x_values = [0]self.y_values = [0]def fill_walk(self):while len(self.x_values) < self.num_point:# 决定前进方向以及沿这个方向前进的距离x_direction = choice([1, -1])x_distance = choice([0, 1, 2, 3, 4])x_step = x_direction * x_distancey_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和yx = self.x_values[-1] + x_stepy = self.y_values[-1] + y_stepself.x_values.append(x)self.y_values.append(y)

rw.visual.py

import matplotlib.pyplot as plt
from random_walk import RandomWalkwhile True:rw = RandomWalk(50_000)rw.fill_walk()plt.style.use('classic')fig, ax = plt.subplots(figsize=(16, 9))point_number = range(rw.num_point)ax.scatter(rw.x_values, rw.y_values, c=point_number, cmap=plt.cm.Blues, edgecolors='none', s=1)#突出起点和终点ax.scatter(0, 0, c='green', edgecolors='none', s=100)ax.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none', s=100)# 隐藏坐标轴ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)plt.show()# 模拟多次随机漫步keep_running = input("Make another walk? (y/n):")if keep_running == 'n':break

结果:美的像一朵云

 

http://www.lryc.cn/news/105104.html

相关文章:

  • firewalld防火墙
  • iMacros WebBrowser Component for .NET
  • 3,堆,桶排序,排序总结【p4-p5】
  • 使用langchain与你自己的数据对话(四):问答(question answering)
  • 如何快速开拓海外华人市场?附解决方案!
  • 【云原生-制品管理】制品管理的优势
  • Java爬虫----HttpClient方式(获取数据篇)
  • 计算机视觉实验:图像增强应用实践
  • ES6:Generator函数详解
  • 前端小练-产品宣传页面
  • arm学习之stm32设备树学习-中断控制led灯亮灭+字符设备指令控制led灯亮灭
  • 快速开发框架若依的基础使用详解
  • RabbitMQ 教程 | 第4章 RabbitMQ 进阶
  • 小程序如何从分类中移除商品
  • P1219 [USACO1.5] 八皇后 Checker Challenge
  • 如何在不使用脚本和插件的情况下手动删除 3Ds Max 中的病毒?
  • SpringCloud Gateway 在微服务架构下的最佳实践
  • Android studio修改app图标
  • <C++> 三、内存管理
  • 大模型开发(十五):从0到1构建一个高度自动化的AI项目开发流程(上)
  • HarmonyOS 开发基础(二)组件拼凑简单登录页面
  • flutter minio
  • ChatGPT:人工智能交互的新时代
  • C. Binary String Copying - 思维
  • 哈工大计算机网络课程网络安全基本原理详解之:密钥分发中心与公钥认证中心
  • md5sum
  • 图文档数字化:实现高效管理的几大步骤
  • 服务器磁盘占用过高分析
  • 【C语言】通讯录3.0 (文件存储版)
  • 【C#常用操作】