13

i am trying to annotate all my data points, but the text while annotating is over lapping with each other,

fig = plt.figure(figsize=(70,2))
fig.autofmt_xdate()
plt.style.use('ggplot')
axess= plt.subplot(1,1,1)
axess.plot_date(idx2,stock2,'--')

axess.set_title("Doc_ID:7377")
axess.set_ylabel('1 - Online, 0 - Offline')

for i,txt in enumerate(date_match_df['service_name_'].tolist()):
    print(txt)
    axess.annotate(txt,(mdates.date2num(idx2[i]),stock2[i]),)
axess.yaxis.grid(True)
axess.xaxis.grid(True)

axess.xaxis.set_major_locator(dates.MonthLocator())
axess.xaxis.set_major_formatter(dates.DateFormatter('\n\n%b-%Y'))

axess.xaxis.set_minor_locator(dates.DayLocator())
axess.xaxis.set_minor_formatter(dates.DateFormatter('\n%d-%a'))
plt.tight_layout()

fig.savefig('happynewyear.svg')

the graph looks like this: graph pic

how to rotate the text?

1
  • Method .annotate() has rotation parameter with zero default value. Try rotation=90. Commented Jun 27, 2018 at 12:58

1 Answer 1

22

Additional arguments to the .annotate()-method are passed to Text, so you can do e.g.:

for i,txt in enumerate(date_match_df['service_name_'].tolist()):
    print(txt)
    axess.annotate(txt,(mdates.date2num(idx2[i]),stock2[i]), ha='left', rotation=60)

Where the change from your code is the addition of ha='left', rotation=60 in the end.

Sign up to request clarification or add additional context in comments.

2 Comments

can we have line spacing between text if the there are multiple points on the same date.?
I'm not sure what the best approach to increase readability of overlapping texts is in your case. I think perhaps the text appears in each annotation box, that you explicit position. What does it look like now? Might be better to create a new question of SO to get more answers to that problem too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.