I have a string sequence for two variables (x, y) for a number of different values โโof the variable z. Usually I add lines with such legends:
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) # suppose mydata is a list of tuples containing (xs, ys, z) # where xs and ys are lists of x and y and z is a number. legns = [] for(xs,ys,z) in mydata: pl = ax.plot(xs,ys,color = (z,0,0)) legns.append("z = %f"%(z)) ax.legends(legns) plt.show()
But I have too many graphs, and legends will cover the graph. I would prefer the color bar to indicate the z value corresponding to the color. I can not find anything like this in the gallery, and all my attempts to cope with the color panel failed. Apparently, I should create a collection of plots before trying to add a color panel.
Is there an easy way to do this? Thank.
EDIT (clarification):
I wanted to do something like this:
import matplotlib.pyplot as plt import matplotlib.cm as cm fig = plt.figure() ax = fig.add_subplot(111) mycmap = cm.hot
But this will not work according to the Matplotlib link, because the list of graphs is not โdisplayedโ, whatever that means.
I created an alternative chart function using LineCollection :
def myplot(ax,xs,ys,zs, cmap): plot = lc([zip(x,y) for (x,y) in zip(xs,ys)], cmap = cmap) plot.set_array(array(zs)) x0,x1 = amin(xs),amax(xs) y0,y1 = amin(ys),amax(ys) ax.add_collection(plot) ax.set_xlim(x0,x1) ax.set_ylim(y0,y1) return plot
xs and ys are lists of x and y coordinate lists, and zs is a list of different conditions for coloring each line. It feels a bit like masonry, though ... I thought there would be a tidier way to do this. I like the flexibility of the plt.plot() function.