2

I am having four different lists-

x=['18ww25', '18ww27', '18ww28', '18ww28.1', '18ww29', '18ww29.1', '18ww29.2']

r=[[27, 27, 27, 27, 27, 27, 27, 43, 43, 43],
   [18, 18, 20, 23, 30, 30, 30, 16, 16, 16],
   [24, 25, 28, 32, 39, 39, 43, 74, 74, 74],
   [43, 43, 44, 44, 43, 45, 45, 1, 1, 1],
   [0, 0, 0, 0, 0, 0, 0, 42, 42, 42], 
   [14, 14, 16, 16, 16, 17, 17, 57, 57, 57],
   [14, 14, 14, 14, 14, 14, 14, 5, 5, 5], 
   [0, 0, 0, 0, 0, 0, 0, 4, 4, 4], 
   [8, 8, 8, 8, 8, 8, 8, 3, 3, 3]]

 tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),    
         (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),    
         (148, 103, 189)]
 l=['PIF', 'ucode patch mem', 'ucode patch match', 'pcode patch mem', 
   'pcode global patch match', 'pcode local patch match', 'vcode patch mem',
   'vcode global patch match', 'vcode local patch match'] 

where x and r are the lists that are used for plotting lines. Every list in r list is plotted against x list. tableau20 is a list that contains the colors in the RGB format. "l" is a list that is the text labels for the different lines which is kept in the same seuence in which the line is plotted.

Now i am plotting a graph using the below code-

for i in range(len(tableau20)):  #Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.  
    r, g, b = tableau20[i]    
    tableau20[i] = (r / 255., g / 255., b / 255.) 

fig = plt.figure(figsize=(8,4)) #height x width
ax = plt.subplot(1,1,1)    
ax.spines["top"].set_visible(False)    
ax.spines["bottom"].set_visible(False)    
ax.spines["right"].set_visible(False)    
ax.spines["left"].set_visible(False)
ax.get_xaxis().tick_bottom()    
ax.get_yaxis().tick_left()
maximum=maxi(r)
print(maximum)
plt.ylabel("UTILIZATION [%]",fontsize=7)
plt.xlabel("WORKING WEEK (WW)",fontsize=7)

plt.ylim(0,maximum+11)
plt.yticks(range(0, maximum+11, 10), [str(x) + "%" for x in range(0, maximum+11, 10)], fontsize=4)
plt.tick_params(axis='both', which='both', bottom=False, top=False, labelbottom=True, left=False, right=False, labelleft=True,labelsize=4)
title="SKX "+gt+" PATCH UTILIZATION"
plt.title(title,fontsize=9)

for i in range(len(r)):
        ax.plot(x,r[i],color=tableau20[i])
textno=0
for line, name in zip(ax.lines, l):
    y = line.get_ydata()[-1]
    ax.annotate(l[textno], xy=(.95,y), xytext=(3,0), color=tableau20[textno],xycoords = ax.get_yaxis_transform(), textcoords="offset points",size=7, va="center")
    textno+=1

Graph that i am getting

rikki
  • 431
  • 1
  • 8
  • 18
  • Does the code you posted work for you? – Joe Oct 05 '18 at 13:41
  • Actually the main code is quite big that's why I only wanted people to get Idea of what i wanted. The thing that I want is to know how to put text labels for each line at the end of line-@Joe – rikki Oct 05 '18 at 13:47
  • ah ok. I cannot take a deep look into it now, but take a look to `enumerate` for the `for loops`, it simplify your coding: eg `for index, i in enumerate(tableau20): r, g, b = tableau20[index]` – Joe Oct 05 '18 at 13:49
  • No problem is not that. I am able to plot lines with different colors mentioned in the tableau20 list but how to get the text labels in the same order of indexing from l list-@Joe – rikki Oct 05 '18 at 13:59
  • I was able to get what i wanted but I am getting overlapping text as I have edited in the question and also added the graph I am getting. I have also posted the actual code that i am using. Can you please tell me how to remove the problem of overlapping text at the end of lines for the lines which are very close-@Joe – rikki Oct 06 '18 at 10:00

1 Answers1

2

I'm sure there is a duplicate of this very question somewhere, but I can't seem to find quite the right one. So here is a quick demonstration of how to do this. Refer to the documentation of annotate() for an explanation of the parameters.

l = ['label {:d}'.format(i) for i in range(1,5)]
x = [0,1]
yy = np.transpose([np.arange(1,5),np.arange(1,5)])

fig, ax = plt.subplots()
for y,l in zip(yy,l):
    ax.plot(x,y)
    ax.annotate(xy=(x[-1],y[-1]), xytext=(5,0), textcoords='offset points', text=l, va='center')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Using annotate I am able to do it and getting the desired result but the text are overlapping for the lines which are ending very closely. I am editing the code and the picture of the graph plotted.Can you please tell me how to remove the overlappiing of text-@Diziet Asahi – rikki Oct 06 '18 at 09:52