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设计】。

相关文章

python对视频画框标记后保存的方法

需要画框取消注释rectangle import cv2 import os,sys,shutil import numpy as np # Open the input mov...

Python操作Sql Server 2008数据库的方法详解

Python操作Sql Server 2008数据库的方法详解

本文实例讲述了Python操作Sql Server 2008数据库的方法。分享给大家供大家参考,具体如下: 最近由于公司的一个项目需要,需要使用Sql Server 2008数据库,开发...

python 将对象设置为可迭代的两种实现方法

1、实现 __getitem__(self) class Library(object): def __init__(self): self.value=['a','b'...

Python使用cookielib模块操作cookie的实例教程

Python使用cookielib模块操作cookie的实例教程

cookielib是一个自动处理cookies的模块,如果我们在使用爬虫等技术的时候需要保存cookie,那么cookielib会让你事半功倍!他最常见的搭档模块就是python下的ur...

pycharm 配置远程解释器的方法

pycharm 配置远程解释器的方法

1、Pycharm -> References(进入设置界面): 2、点击 Project Interpreter: 3、点击 Add Remote 来添加远程解释器:...