1

The code below with commented "offset annotation" lines produce the image below, however, the annotation is not neat; I would like to offset these annotations to make them look neat; hence the code lines with "offset annotation", when uncommented, are supposed to offset (adjust "MN={}" a little to the left) these annotations, instead, it produces the same graph without any annotations. Is any solution for this? I noted that this problem is similar to Annotate Time Series plot in Matplotlib, but I could not adapt the answer to this problem. enter image description here

import datetime
from matplotlib import pyplot as  plt

#create time points
datetime1 = datetime.timedelta(hours=0,minutes=0,seconds=19)
datetime2 = datetime.timedelta(hours=0,minutes=4,seconds=12)
datetime3 = datetime.timedelta(hours=0,minutes=19,seconds=50)
datetime4 = datetime.timedelta(hours=0,minutes=57,seconds=3)
datetime5 = datetime.timedelta(hours=2,minutes=20,seconds=20)
datetime6 = datetime.timedelta(hours=5,minutes=57,seconds=45)



points   =  [10568,40297,85357,149339,237323,357508]
times    =  [str(datetime1),str(datetime2),str(datetime3),str(datetime4),str(datetime5),str(datetime6)]
machine_numbers =  [1.5,2.4,3.3,4.2,5.1,6.0]



fig, ax = plt.subplots()
ax.set_title("produced coins vs time elapsed") 
ax.plot(times,points)
for machine_number,time,point in zip(machine_numbers,times,points):
    #offset the coin annotation to look better
    #indexts = time.split(":")[-1]               #offset annotation
    #indexts_m = float(indexts)-1                #offset annotation
    #time = time.replace(indexts,str(indexts_m)) #offset annotation
    #insert annotions
    ax.annotate(r'$MN={}$'.format(machine_number),(time,point))
#Legends    
ax.set_xlabel("Time [hours:minutes:seconds]")
ax.set_ylabel("Earned coins")
plt.show()
  • I can't quite tell what the offset code is supposed to do, but if you just want the annotation a bit to the right, the easiest is to insert spaces: `r' $MN={}$'.format...` – Jody Klymak Aug 04 '22 at 16:37
  • Thank you! Jody, for your comment. That is precisely what I would like to do, to annotate a bit to the right or left. – Tirelo SHIKWAMABANA Aug 04 '22 at 21:10
  • @Jody: I tried the method you suggested however it works only for the right-ward annotation only; what would resolve the problem is the left-wards annotation since the right-ward result in the last annotation moving passed the borders of the plots, which is still untidy. – Tirelo SHIKWAMABANA Aug 04 '22 at 21:29
  • Align right, and add spaces to the end? There is not automagical way to do this. – Jody Klymak Aug 04 '22 at 23:45
  • Adding spaces did not work; I tried it as an alternative to adding space at the beginning. It results in the graph above. – Tirelo SHIKWAMABANA Aug 05 '22 at 00:39
  • I tried adding space to the right, but it did not work. I also tried ax.transData did not work; I think I agree with you when you say there is no automatic way of doing this. – Tirelo SHIKWAMABANA Aug 05 '22 at 02:57

0 Answers0