Python处理时间日期坐标轴过程详解

yipeiwu_com6年前Python基础

1. 前言

当日期数据作为图表的坐标轴时通常需要特殊处理,应为日期字符串比较长,容易产生重叠现象

2. 设定主/次刻度

2.1 引用库

from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY

2.2 获取每月/周/日数据

获取每月一日数据

monthdays = MonthLocator()

获取每周一的日期数据

mondays = WeekdayLocator(MONDAY) # 主要刻度

获取每日数据

alldays = DayLocator() # 次要刻度

2.3 设定主/次刻度

ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)

2.4 设定格式

mondayFormatter = DateFormatter('%Y-%m-%d') # 如:2-29-2015
dayFormatter = DateFormatter('%d') # 如:12
ax.xaxis.set_major_formatter(mondayFormatter)

3. 字符串旋转

for label in ax1.get_xticklabels():
label.set_rotation(30)
label.set_horizontalalignment('right')

4. 效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pyinstaller 打包exe教程及问题解决

安装 pip insatll Pyinstaller 参数 pyinstaller -Fw main.py 参数 概述...

Python logging管理不同级别log打印和存储实例

Python内置模块logging管理不同级别log打印和存储,非常方便,从此告别了使用print打桩记录,我们来看下logging的魅力吧 import logging lo...

Python3安装Scrapy的方法步骤

Python3安装Scrapy的方法步骤

本文介绍了Python3安装Scrapy的方法步骤,分享给大家,具体如下: 运行平台:Windows Python版本:Python3.x IDE:Sublime text...

Tornado 多进程实现分析详解

引子 Tornado 是一个网络异步的的web开发框架, 并且可以利用多进程进行提高效率, 下面是创建一个多进程 tornado 程序的例子. #!/usr/bin/env pyth...

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...