格式化时间后,Pyplot刻度消失 [英] Pyplot ticks disappear when formatting time

查看:100
本文介绍了格式化时间后,Pyplot刻度消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib来绘制每周的一系列值.
我创建的数据框(请参见下面的代码)如下所示:

I am using matplotlib to plot a serie of values per week.
The dataframe I create (see code below) looks like this:

Dataframe: df_dates   

| Name_Of_index |       |  
|------------   |----   |  
| 2017-01-22    | 13    |  
| 2017-01-29    | 0     |  
| 2017-02-05    | 5     |  
| 2017-02-12    | 37    |    

Freq: W-SUN, dtype: int64

我的代码应该为每周/时间戳的数量绘制条形图.

My code is supposed to draw a bar plot for number per week/timestamp.

def plot_contact_with_waters_per_week(dataframe, target_name,contact_with_water_value='contact_with_water', date_col='TBL_DAT'):
   df_contact_with_waters=dataframe[dataframe[target_name]==contact_with_water_value]
   contact_with_waters_per_week=pd.Series((df_contact_with_waters[target_name].values),index=df_contact_with_waters[date_col]).resample(rule='W').count()
   axis_now=contact_with_waters_per_week.plot(kind='bar')
   axis_now.set_ylabel("Total number of contact_with_waters per week")
   axis_now.xaxis_date() 
   axis_now.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=SU))
   axis_now.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
   plt.show()

困扰我的是,一旦我格式化X轴,所有的刻度都将变得不可见.

The thing that bothers me is that as soon as I format the x-axis, all ticks become invisible.

现在,如果我只删除三条xaxis行:

Now, if I just delete the three xaxis lines:

   axis_now.xaxis_date() 
   axis_now.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=SU))
   axis_now.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))

我的地图确实带有时间戳,但是它们有完整长度,而且看起来并不漂亮:

I do get my plot with timestamps, but they have the full length and do not look pretty:

现在,我想我在格式化时间戳记时出错了,您能看看一下并给我一些建议吗?非常感谢.

Now, I suppose I make a mistake in formatting my timestamps, can you please have a look and give me some advice? It would be much appreciated.

推荐答案

万一有人遇到相同的问题,下面的代码会起作用:

In case anybody ever runs into the same issue, here is the code that worked:

def plot_contact_with_waters_per_week(dataframe, target_name,contact_with_water_value='contact_with_water', date_col='TBL_DAT'):
   df_contact_with_waters=dataframe[dataframe[target_name]==contact_with_water_value]
   contact_with_waters_per_week=pd.Series((df_contact_with_waters[target_name].values),index=df_contact_with_waters[date_col]).resample(rule='W').count()
   fig, ax = plt.subplots()
   plt.bar(height=contact_with_waters_per_week,left=contact_with_waters_per_week.index,width=3)
   plt.ylabel("Total number of contact_with_waters per week")
   ax.xaxis_date() 
   ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=SU))
   ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
   my_xticks = ax.get_xticks()
   my_xticks_biweekly=my_xticks[::2].copy()
   plt.xticks(my_xticks_biweekly,visible=True, rotation="vertical")
   plt.show()

非常感谢您 ImportanceOfBeingErnest

这篇关于格式化时间后,Pyplot刻度消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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