简体   繁体   English

如何格式化 x 轴日期以在每个月的开始显示月份名称,然后仅显示天数?

[英]how can I format x-axis date to show month-name on the start of every month, then day numbers only?

I have the below python code, it returns no x-axis labels.我有以下 python 代码,它不返回 x 轴标签。 How can I format the x-axis to show month names at the start of every month and then day numbers afterwards?如何格式化 x 轴以在每个月的开始显示月份名称,然后显示日期?

fig, axs = plt.subplots(1, 1, figsize=(15, 10))

boxProp = dict(ls='-', 
               lw=0.50, 
               color='darkgoldenrod', 
               edgecolor = '#3a5e42', 
               facecolor = 'white')

fliProp = dict(color = '#3a5e42', 
               marker = '8',
               mec = '#3a5e42',
               mew = 2,
               mfc = 'white',
               ms = 2)

whisProp = dict(color = '#3a5e42', lw=0.50)

capProp = dict(color = '#3a5e42', lw=0.50)

medProps = dict(color = '#3a5e42')


boxplot = sns.boxplot(ax=axs, 
                      x = HistPrices.index.date,
                      y = HistPrices.values, 
                      color = 'white',
                      width = 0.8,
                      boxprops = boxProp,
                      whiskerprops = whisProp,
                      capprops = capProp,
                      medianprops = medProps,
                      flierprops = fliProp)



axs.tick_params(direction='out', which='major', length=8, width=1)
axs.tick_params(direction='out', which='minor', length=4, width=1)


day_fmt = DateFormatter('%d')
axs.xaxis.set_major_locator( DayLocator() )
axs.xaxis.set_major_formatter(day_fmt)
 

axs.yaxis.set_major_locator( MultipleLocator(0.5) )
axs.yaxis.set_minor_locator( MultipleLocator(0.1) )

HistPrices.index.date returns the below: HistPrices.index.date 返回以下内容:

array([datetime.date(2021, 1, 4), datetime.date(2021, 1,4),...,datetime.date(2021, 7, 22)], dtype=object)数组([datetime.date(2021, 1, 4), datetime.date(2021, 1,4),...,datetime.date(2021, 7, 22)], dtype=object)

I think concise date formatter does exactly what you are asking:我认为简洁的日期格式化程序完全符合您的要求:

https://matplotlib.org/stable/gallery/ticks_and_spines/date_concise_formatter.html https://matplotlib.org/stable/gallery/ticks_and_spines/date_concise_formatter.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM