How does legend() work on a dynamic data set?
I need a legend for a code which generates data from a dict. I don't know how much keys exist in the dict is there a way to do 'dynamic' a legend on this?
import matplotlib.pyplot as plt
for开发者_如何转开发 host in d.keys():
plt.plot(range(100),d[host])
plt.show()
If I understand you, then sure: you simply need to turn the key into a label somehow, even if it's as simple as calling str on it.
import matplotlib.pyplot as plt
import numpy
x = numpy.arange(10.)
d = dict((i, numpy.sin(x+i)) for i in range(5))
for k in sorted(d): # sort purely to make deterministic
plt.plot(x,d[k],label=str(k))
plt.legend(loc=2)
plt.draw()
精彩评论