月份为轴刻度 [英] Months as axis ticks
本文介绍了月份为轴刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在此图上添加每月的价格变动-它目前显示2015年和2016年的价格变动,我希望显示1月,2月... 12月.
I would like to add monthly ticks to this plot - and it is currently showing ticks of 2015 and 2016 where i would like it to show Jan, Feb...Dec, Jan.
我从2015年1月1日开始到2015年12月31日结束的数据的索引.到目前为止,我的代码是:
The index of the data that I have begins on 2015-01-01 and ends on 2015-12-31. The code I have so far is:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
plt.figure(figsize=(20,12))
#ax = plt.add_subplot(111)
plt.plot_date(list(max_group_by15.index), max_group_by15['Data_Value'])
plt.plot_date(list(min_group_by15.index), min_group_by15['Data_Value'])
plt.plot_date(list(min_group_by14.index), min_group_by14['Data_Value'], '-b')
plt.plot_date(list(max_group_by14.index), max_group_by14['Data_Value'], '-r')
plt.legend(['2015 Temp Exceed Prev High', '2015 Temp Below Prev Low', 'Min Temp 2005-2014', 'Max Temp 2005-2014'])
plt.gca().fill_between(min_group_by14.index, min_group_by14['Data_Value'], max_group_by14['Data_Value'], facecolor = 'blue', alpha =.25)
plt.xlabel('Month')
plt.ylabel('Degrees (Celsius)')
plt.title('Daily High and low temp in Ann Arbor area from 2005-2014')
locator = mdate.MonthLocator()
plt.gca().xaxis.set_major_locator(locator)
产生此图:
推荐答案
在没有确切数据的情况下很难尝试,但我认为您只是错过了对set_major_formatter
的呼叫.
Tough to try without your exact data, but I think you're simply missing a call to set_major_formatter
.
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Generate some random date-time data
numdays = 500
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(0, numdays)]
y = np.random.rand(numdays)
# Set the locator
locator = mdates.MonthLocator() # every month
# Specify the format - %b gives us Jan, Feb...
fmt = mdates.DateFormatter('%b')
plt.plot(date_list,y)
X = plt.gca().xaxis
X.set_major_locator(locator)
# Specify formatter
X.set_major_formatter(fmt)
plt.show()
这篇关于月份为轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文