如何在matplotlib图中将xtick标签的年份缩写为2位数字 [英] How to abbreviate xtick labels years to 2 digits in a matplotlib plot

查看:67
本文介绍了如何在matplotlib图中将xtick标签的年份缩写为2位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的时间序列超过10年,并且希望将xtick标签缩写为2位数字的年份. 我该怎么办?

I have a timeseries of 10+ years and want to abbreviate the xtick labels to 2-digit years. How can I do that ?

import matplotlib.pyplot as plt
import datetime as dt
import pandas as pd
import pandas.io.data as web

stocklist = ['MSFT']

# read historical prices for last 11 years
def get_px(stock, start):
    return web.get_data_yahoo(stock, start)['Adj Close']

today = dt.date.today()
start = str(dt.date(today.year-11, today.month, today.day))
px = pd.DataFrame({n: get_px(n, start) for n in stocklist})
plt.plot(px.index, px[stocklist[0]])
plt.show()

推荐答案

这以可疑的方式深入研究了pandas内部,但是

This digs into the pandas internals in questionable ways, but

ax = plt.gca()
ax.get_xaxis().get_major_formatter().scaled[365] = '%y'
plt.draw()

格式规范

这篇关于如何在matplotlib图中将xtick标签的年份缩写为2位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆