python - matplotlib using text instead of number in y ticks -
i want y axis' tick text label rather number, , x series of datetime object, want plot represent track of man's event during day, that's scene.
this code :
import datetime matplotlib import pyplot plt matplotlib import dates import matplotlib mpl y_text_dict = ["go dinner", "visit mom", "go park", "work"] x_str=["2016-06-30 09:11", "2016-06-30 10:11", "2016-06-30 12:10", "2016-06-30 16:57", "2016-06-30 17:17", "2016-06-30 17:33", "2016-06-30 17:33", "2016-06-30 17:48", "2016-06-30 17:58", "2016-06-30 18:27"] x = [datetime.datetime.strptime(time, "%y-%m-%d %h:%m") time in x_str] y = [0,3,2,1,2,2,1,1,3,2] #so event series "go dinner"->"work"->"go park"->"visit mom"... y_text = [y_text_dict[i] in y] zhfont1 = mpl.font_manager.fontproperties(fname='/library/fonts/songti.ttc') fig = plt.figure() ax = fig.add_subplot(111) ax.set_yticklabels(y_text_dict,fontproperties=zhfont1) #ax.set_yticks(y_text) ax.set_xticks(x) ax.xaxis.set_major_formatter(dates.dateformatter('%d/%m/%y %h:%m')) ax.grid(true) fig.autofmt_xdate(rotation=90) plt.step(x, y, = 'post') plt.show() the result of code above :
apparently not expect. also, wonder if there better way illustrate such event tracking ?
update: can use these 2 lines make expect:
ax.set_yticks(xrange(len(y_text_dict))) ax.set_yticklabels(y_text_dict)
this should trick:
plt.xticks(tick_loc, tick_names) tick_loc sets locations of ticks, , tick_names display.

Comments
Post a Comment