Hitesh
Hitesh

Reputation: 1325

values not visible clearly in plot

With the help of following code I am plotting a graph; however, the values are not clearly visible; they are mixed with graph:

for x111,y1 in zip(x1,loss_list):
    plt.text(x111, y1, '%.2f' % y1, ha='right', va= 'bottom', color='blue')
for x112,y2 in zip(x1,val_loss_list):
    plt.text(x112, y2, '%.2f' % y2, ha='left', va= 'top', color='orange')
plt.plot(xc,train_loss_history)
plt.plot(xc,val_loss_history)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
#plt.grid(True)
plt.legend(['train','val'])
plt.show() 

where x111 and other values and list i got from my code. and getting following plot enter image description here

how to make the visualization more readable?

Upvotes: 1

Views: 230

Answers (3)

C.med
C.med

Reputation: 601

You can change the color of your text and also the size: size='x-small'

for x111,y1 in zip(x1,loss_list):
    plt.text(x111, y1, '%.2f' % y1,color='black',horizontalalignment='center',verticalalignment='bottom')
for x112,y2 in zip(x1,val_loss_list):
    plt.text(x112, y2, '%.2f' % y2,color='black',horizontalalignment='center',verticalalignment='bottom')

Upvotes: 2

Hannes Ovrén
Hannes Ovrén

Reputation: 21831

I recently had the same issue. My solution is not pretty, but works if you

  • don't have too many values
  • don't need to do it dynamically, i.e. you only have this one dataset

Hopefully someone might have a nicer, dynamic solution for you as well, but this might at least solve your problem for now.

The solution is to simply add an y-offset to each point. E.g.

y1_offsets = [1., 2., -2.3, 4., ...]  # Define this by hand so it looks good

for x111, y1, yoff in zip(x1,loss_list, y1_offsets):
    plt.text(x111, y1 + yoff, '%.2f' % y1, ha='right', va= 'bottom', color='blue')

If you need to do this dynamically, one approach could be to look at the y-values "close" to where you are plotting, and pick something appropriate. E.g. a simple "max pooling" of the y-values with a suitable window size.

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36662

You can maybe tweak the alpha values (transparencies) of the colors used?

something like this maybe?

for x111,y1 in zip(x1, loss_list):
    plt.text(x111, y1, '%.2f' % y1, ha='right', va='bottom', color='blue')
for x112,y2 in zip(x1, val_loss_list):
    plt.text(x112, y2, '%.2f' % y2, ha='left', va='top', color='black')  # changed to black, but you can try some other color to see if you like it better
plt.plot(xc, train_loss_history, color='blue', alpha=0.5)
plt.plot(xc, val_loss_history, color='orange', alpha=0.5)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
#plt.grid(True)
plt.legend(['train', 'val'])
plt.show() 

You did not provide data, so it is hard to tweak the values to maximize readability.

Upvotes: 1

Related Questions