2

I am trying to include a legend for my stackplot. I know that you cannot do it in the normal way so I followed the instructions from this similar post however there is still an error.

x=data[:,-1]
y1=map(int,data[:,1])
y2=map(int,data[:,2])
y3=map(int,data[:,3])
y4=map(int,data[:,4])
y5=map(int,data[:,5])
y6=map(int,data[:,6])
y7=map(int,data[:,7])
y8=map(int,data[:,8])
y9=map(int,data[:,9])
y10=map(int,data[:,0])

xnew=np.linspace(0,len(x),50)

smooth_y1=spline(np.arange(len(x)),y1,xnew)
smooth_y2=spline(np.arange(len(x)),y2,xnew)
smooth_y3=spline(np.arange(len(x)),y3,xnew)
smooth_y4=spline(np.arange(len(x)),y4,xnew)
smooth_y5=spline(np.arange(len(x)),y5,xnew)
smooth_y6=spline(np.arange(len(x)),y6,xnew)
smooth_y7=spline(np.arange(len(x)),y7,xnew)
smooth_y8=spline(np.arange(len(x)),y8,xnew)
smooth_y9=spline(np.arange(len(x)),y9,xnew)
smooth_y10=spline(np.arange(len(x)),y10,xnew)

plt.stackplot(np.arange(50),smooth_y1,smooth_y2,smooth_y3,smooth_y4,smooth_y5,smooth_y6,smooth_y7,smooth_y8,smooth_y9,smooth_y10)


plt.ylim([0,30])
plt.legend([smooth_y1,smooth_y2,smooth_y3,smooth_y4,smooth_y5,smooth_y6,smooth_y7,smooth_y8,smooth_y9,smooth_y10],['hfsdkjfhs','sldjfhsdkj','sdrtryf','sdfsd','sdkjf','sdfsd','sdrtdf','sfsd','sdaaafs','sdffghs'])
plt.show()

However an error occurs on the line with the legend. It says

File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3381, in legend
    ret = gca().legend(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4778, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 366, in __init__
    self._init_legend_box(handles, labels)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 606, in _init_legend_box
    handler = self.get_legend_handler(legend_handler_map, orig_handle)
  File "C:\Python27\lib\site-packages\matplotlib\legend.py", line 546, in get_legend_handler
    if orig_handle in legend_handler_keys:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Does anyone know how to solve this?

2
  • smooth_y* are not matplotlib artists so this won't work as written. Commented Dec 2, 2013 at 20:16
  • the lack of the possibility to add labels to stacked plots is discussed on the matplotlib issue tracker and as @tcaswell suggested, the use of proxy artists seems to currently recommended way to go Commented Jul 1, 2014 at 9:48

1 Answer 1

6
from matplotlib.patches import Rectangle

label_list = ['hfsdkjfhs','sldjfhsdkj','sdrtryf','sdfsd','sdkjf','sdfsd','sdrtdf','sfsd','sdaaafs','sdffghs']

x = data[:, -1]
xnew=np.linspace(0,len(x),50)

smoothed_data = [spline(np.arange(len(x)),data[:, j%10],xnew) 
                 for j in range(1, 11)]

# get a figure and axes
fig, ax = plt.subplots()
# make the stack plot
stack_coll = ax.stackplot(xnew, smoothed_data)
# set the ylim
ax.set_ylim([0,30])
# make proxy artists
proxy_rects = [Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0]) for pc in stack_coll]
# make the legend
ax.legend(proxy_rects, label_list)
# re-draw the canvas
plt.draw()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.