胖胖的枫叶
主页
博客
产品设计
企业架构
全栈开发
效率工具
数据分析
项目管理
方法论
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
主页
博客
产品设计
企业架构
全栈开发
效率工具
数据分析
项目管理
方法论
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
  • 标签索引
  • 2024年

    • 配置Mac环境
    • 业务知识会计管理
    • 业务知识会计基础
    • 业务知识什么是财务
  • 2023年

    • 项目 Boi
  • 2022年

    • 企业架构故障管理
    • 企业架构开发债务
  • 2021年

    • Python3.8 Matplotlib员工数据分析
    • Python3.8 Matplotlib IP折线图
    • Python3.8 词云 IP地址
    • Redis RediSearch
    • Rust第一个CLI程序
    • Rust所有权
    • Rust函数与控制流
    • Rust变量与数据类型
    • Rust入门
    • 企业架构分布式系统
    • 编程式权限设计
    • Java JVM优化
    • SpringBoot MyBatis 批量
    • SpringBoot 测试Mock
    • SpringBoot Redis布隆过滤器
    • CentOS7 Jenkins 部署
    • SpringBoot WebClient
    • Docker Drone 部署
    • SpringBoot MyBatis
    • SpringBoot Redisson
    • SpringBoot MyBatis 雪花算法
    • Java Netty
    • Redis 扫描
    • CentOS7 Jenkins本地部署分级
    • Mac 安装 Neo4j Jupyter
    • Mac OpenJDK11 JavaFX 环境
    • Mac 安装 Jenv
    • SpringBoot Redis 延时队列
    • SpringBoot MDC日志
    • SpringBoot 定时任务
    • CentOS7 Nginx GoAccess
    • SpringBoot MyBatis 分析
    • SpringBoot Lucene
    • 企业架构分布式锁
    • 学习技巧减少学习排斥心理
    • SpringBoot 动态数据源
    • Docker Compose SpringBoot MySQL Redis
    • SpringBoot 阻塞队列
    • Docker Compose Redis 哨兵
    • Docker Compose Redis 主从
    • 网络通信
  • 2020年

    • SpringBoot 延时队列
    • MySQL基础(四)
    • Java 雪花算法
    • Redis Geo
    • 网络通信 Tcpdump
    • Spring SPI
    • Java Zookeeper
    • SpringBoot JMH
    • 网络通信 Wireshark
    • Docker Compose Redis MySQL
    • CentOS7 Docker 部署
    • Netty 源码环境搭建
    • MySQL基础(三)
    • CentOS7 Selenium运行环境
    • CentOS7 Nginx HTTPS
    • Java JMH
    • SpringBoot 修改Tomcat版本
    • Java Eureka 钉钉通知
    • SpringBoot 错误钉钉通知
    • Java JVM
    • Git 合并提交
    • CentOS7 OpenResty 部署
  • 2019年

    • Redis CLI
    • CentOS7 Nginx 日志
    • 编程式代码风格
    • IDEA 插件
    • Skywalking 源码环境搭建
    • SpringBoot Redis 超时错误
    • 编程式 gRPC
    • Java Arthas
    • Docker Compose Redis 缓存击穿
    • Docker ElasticSearch5.6.8 部署
    • Docker Mysql5.7 部署
    • Spring Redis 字符串
    • Docker Zookeeper 部署
    • Docker Redis 部署
    • SpringBoot Dubbo
    • CentOS7 CMake 部署
    • 应用程序性能指标
    • Java Code 递归
    • CentOS7 ELK 部署
    • CentOS7 Sonarqube 部署
    • Java Selenium
    • Java JJWT JUnit4
    • Spring 源码环境搭建
    • Java JUnit4
    • Java Web JSON Token
    • 编程式 FastDFS
    • Java XPath
    • Redis基础(二)
    • Redis基础(一)
    • Java MyBatis JUnit4
    • Java MyBatis H2 JUnit4
    • MyBatis 源码环境搭建
    • Git 配置
    • Java 核心
    • Java Dubbo
    • Java JavaCollecionsFramework
    • Java Maven
    • Java MyBatis
    • Java Spring
    • Java SpringMVC
    • MySQL
    • Redis
  • 2018年

    • Java HashMap
    • Java HashSet
    • Java Code 交换值
    • Spring Upgrade SpringBoot
    • Mac 编程环境
    • Java Log4j
    • 网络通信 Modbus
    • MySQL基础(二)
    • MySQL基础(一)
    • Java Stack
    • Java Vector
    • CentOS7 RabbitMQ 部署
    • CentOS7 Redis 部署
    • CentOS7 MongoDB 部署
    • CentOS7 基础命令
    • Java Eureka Zookeeper
    • CentOS7 MySQL 部署
    • Git 分支
    • CentOS7 Java环境配置
    • Java LinkedList
    • Java ArrayList
    • Spring Annotation Aop

Python3.8 Matplotlib IP折线图

尝试下使用python Matplotlib可视化数据。

演示代码

  • python3.8运行环境
  • 测试数据库
  • jupyter notebook
  • pip3 install pymysql
➜  jupyter-example git:(main) python --version
Python 3.8.7
➜  jupyter-example git:(main) jupyter notebook --version
6.2.0

代码

#!/usr/bin/python3
# %matplotlib inline  作用是将Matplotlib绘制的图形内嵌在Jupyter Notebook的页面中,无需执行命令plt.show()便可以直接在页面内展示绘制的图形。

%matplotlib inline 
import pymysql
import time
import matplotlib.pyplot as plt
 
def get_address(date_address,index):
    strlist = date_address.split('|')	# 用逗号分割str字符串,并保存到列表
    address = strlist[index];
    return address
    
def get_timestamp(date_time):
    ts = date_time / 1000
    #转换成localtime
    time_local = time.localtime(ts)
    date = time.strftime("%Y-%m", time_local)
    return date

def get_content_from_db(sql):
    print('连接数据库!')
    dbhost='localhost'
    dbuser='root'
    dbpass='123456'
    dbname='ip_info'
     # 创建数据库连接
    db = pymysql.connect(host=dbhost,user=dbuser,password=dbpass,database=dbname,charset = "utf8mb4")
    try:
        # 使用cursor()方法获取操作游标 
        cursor = db.cursor()
        # 执行SQL语句
        cursor.execute(sql)
         # 获取所有记录列表
        results = cursor.fetchall()
    except:
        print ('Reading Error ')    
    finally:
        db.close()
    print ('Completion of data reading ')    
    return (results)

def line_show(results,save_file_name):
    indexList = []
    timeList = []
    total = 0
    for i in results:
        total += i[1]
        indexList.append(i[1])
        timeList.append(i[0])

    plt.rcParams["font.family"] = 'Arial Unicode MS' # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
    plt.rc('figure', figsize = (14, 7))
    plt.rc('font', size = 14)
    plt.rc('axes', grid = False)
    plt.rc('axes', facecolor = 'white')
    plt.plot(timeList,
             indexList,
             'ro-',
             color='#4169E1',
             alpha=0.8,
             linewidth=1,
             label='月增长')
    # 设置数字标签
    for a, b in zip(timeList, indexList):
        plt.text(a, b, b, ha='center', va='bottom', fontsize=15)
        
    plt.legend(loc="upper right")
  
    plt.title('用户注册量'+str(total))
    plt.xlabel('日期')
    plt.ylabel('注册数量')
    plt.savefig(save_file_name+'.png',facecolor='white',dpi= 200)
    plt.show()

# 查询当前数据库中的所有数据表
line_sql_1 = "SELECT FROM_UNIXTIME(create_time / 1000, '%Y-%m') AS date , COUNT(*) AS count FROM ip_registered GROUP BY date"
results = get_content_from_db(line_sql_1)
line_show(results,'line')

END

最近更新: 2025/12/27 18:51
Contributors: 庆峰
Prev
Python3.8 Matplotlib员工数据分析
Next
Python3.8 词云 IP地址