I have two arrays that i am graphing like this
dates = [datetime.date(2015, 11, 22), datetime.date(2015, 11, 23), datetime.date(2015, 11, 24)]
points = [3L, 1L, 2L]
And then I plot like this
plt.plot(dates, points, 'r-o')
plt.xticks(rotation=70)
plt.tight_layout()
plt.savefig('test.pdf')
Which produces a graph like this
But i don't want the times to appear, I want to dates to appear and I have an array of dates, not date times or times, so what is the x axis showing times?
How can I get it to be like this on the x axis 2015年11月22日, 2015年11月23日, 2015年11月24日?
asked Nov 24, 2015 at 22:31
spen123
3,53411 gold badges41 silver badges56 bronze badges
1 Answer 1
Try this :
dates = [datetime.date(2015, 11, 22), datetime.date(2015, 11, 23), datetime.date(2015, 11, 24)]
points = [3L, 1L, 2L]
x = [x for x in range(0,len(points))]
plt.plot(points, 'r-o')
plt.xticks(rotation=70)
plt.xticks(x,dates)
plt.tight_layout()
plt.savefig('test.pdf')
I hope it's help
Sign up to request clarification or add additional context in comments.
3 Comments
spen123
I still get no labels on x axis
spen123
Not sure if this makes a difference but when I do
print dates I get this [datetime.date(2015, 11, 22), datetime.date(2015, 11, 23), datetime.date(2015, 11, 24)] like in the question and when i do print dates[0] I get 2015年11月22日A.H
No, it's normal, stringification of datetime.date is what you have when you try to print a date with the print function. Try to remove rotation from xticks. Don't forget to close open pdf files (to correct handle rewrite of it) I know it's not a answer, but code works here ;) Try to copy/paste my code just to see. Try to do a plt.show() instead of plt.savefig() to see quickly the result. Never forget to close windows...
lang-py