没有合适的资源?快使用搜索试试~ 我知道了~
matplotlib命令与格式之tick坐标轴日期格式(设置日期主副刻度)
5星 · 超过95%的资源 3 下载量 143 浏览量
2020-09-18
21:41:11
上传
评论
收藏 75KB PDF 举报
温馨提示
试读
3页
主要介绍了matplotlib命令与格式之tick坐标轴日期格式(设置日期主副刻度),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
资源推荐
资源详情
资源评论
matplotlib命令与格式之命令与格式之tick坐标轴日期格式坐标轴日期格式(设置日期主副刻度设置日期主副刻度)
主要介绍了matplotlib命令与格式之tick坐标轴日期格式(设置日期主副刻度),文中通过示例代码介绍的非常详细,对
大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1.横坐标设置时间格式
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# 配置横坐标为日期格式
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
例子:
from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
# 生成横纵坐标信息
dates = ['01/02/1991', '01/03/1991', '01/04/1991']
xs = [datetime.strptime(d, '%m/%d/%Y').date() for d in dates]
ys = range(len(xs))
# 配置横坐标
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# Plot
plt.plot(xs, ys)
plt.gcf().autofmt_xdate() # 自动旋转日期标记
plt.show()
2.设置日期坐标轴主副刻度值
所有坐标轴日期格式类型
MinuteLocator: locate minutes(f)
HourLocator: locate hours
DayLocator: locate specified days of the month
WeekdayLocator: Locate days of the week, e.g., MO, TU
MonthLocator: locate months, e.g., 7 for july
YearLocator: locate years that are multiples of base
RRuleLocator: locate using a matplotlib.dates.rrulewrapper. The rrulewrapper is a simple wrapper around adateutil.rrule (dateutil)
which allow almost arbitrary date tick specifications. See rrule example.
AutoDateLocator: On autoscale, this class picks the best MultipleDateLocator to set the view limits and the tick locations.
(1)获取坐标轴日期格式类型
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
#获取每月一日数据
monthdays = MonthLocator()
#获取每周一的日期数据
mondays = WeekdayLocator(MONDAY)
#获取每日数据
alldays = DayLocator()
# import constants for the days of the week
from matplotlib.dates import MO, TU, WE, TH, FR, SA, SU
# tick on mondays every week
loc = WeekdayLocator(byweekday=MO, tz=tz)
# tick on mondays and saturdays
loc = WeekdayLocator(byweekday=(MO, SA))
# tick on mondays every second week
loc = WeekdayLocator(byweekday=MO, interval=2)
# tick every 5th easter(每隔5个选1个)
rule = rrulewrapper(YEARLY, byeaster=1, interval=5)
loc = RRuleLocator(rule)
(2)设置坐标轴日期格式
#设置主副刻度
ax.xaxis.set_major_locator(mondays)ax.xaxis.set_minor_locator(alldays)
#设置坐标轴刻度标签格式
mondayFormatter = DateFormatter('%Y-%m-%d') # 如:2-29-2015dayFormatter = DateFormatter('%d') # 如:12ax.xaxis.set_major_formatter(mondayFormatter)
#字符串旋转
weixin_38676500
- 粉丝: 9
- 资源: 916
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页