4

I would like to remove seconds from my x-axis labels as they are unnecessary.

Also I want to center align the tick labels instead of have them positioned to the left of the tick mark.

Any suggestions on how to do this?

Here is some of the code that I've used if this helps

fig=plt.figure()
ax=fig.add_subplot(111)
line1 = plot(table.index,table[data],color=colors[0])

fig.autofmt_xdate(rotation=0)   

tickFormat =  matplotlib.ticker.LinearLocator(numticks=5)
ax.xaxis.set_major_locator(tickFormat)            

enter image description here

2
  • Can you show us some code of how you generated this graph? You just need to tweak an argument to a formatter. (but I would rather not guess which formatter you are using) Commented Apr 17, 2013 at 1:13
  • I have used the fig.autofmt_xdate() for formatting, is there a specific formatter I can use to manipulate the appearance of seconds? Commented Apr 17, 2013 at 1:33

1 Answer 1

8
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%H:%M')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)  
Sign up to request clarification or add additional context in comments.

3 Comments

There is also a way to tweak the auto-formatter. Remember to accept your own answer when it will let you
Regarding the format of the string argument, here is the reference: strftime.org
tacaswell: "There is also a way to tweak the auto-formatter" - how? Also, this solution breaks if you are also using ax.set_xticks() with floating point values: "ValueError: DateFormatter found a value of x=0, which is an illegal date. This usually occurs because you have not informed the axis that it is plotting dates, e.g., with ax.xaxis_date()". How can you get around this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.