itthrill
itthrill

Reputation: 1376

plt.annotate() in a new line in matplotlib

Is there a way to plt.annotate() in matplotlib such that each sublist is shown in a new line? I am trying to implement something in line of the code I have commented out below. Basically I am trying to eliminate the need for separate annotation for each sublist because number of sublist is not known and I am looking for some better way than a loop to achieve it.

import matplotlib.pyplot as plt


x = [['A','B','C'], ['X','Y','Z'],['D','E']] 

plt.annotate(x[0], xy=(0.5, 0.5))
plt.annotate(x[1], xy=(0.5, 0.4))
plt.annotate(x[2], xy=(0.5, 0.3))

#plt.annotate(x,xy=(0.5, 0.5))  Is there  a way that each sublist is shown in a new line automatically

plt.show()

Upvotes: 0

Views: 1129

Answers (1)

Bill Chen
Bill Chen

Reputation: 1749

Try this:

x = [['A','B','C'], ['X','Y','Z'],['D','E']]
s = ''
for i in x:
    s += (str(i) + '\n')
plt.annotate(s, xy=(0.5, 0.5))

If you using line-break, it will show in three lines, however, you have to convert everything to string.

Upvotes: 1

Related Questions