matplotlib shows no legend [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Python legend attribute error

    1 answer




I have a dataframe which i had to transfer into numpy arrays, because i want to plot over the index which is a datetime but i get this error:



KeyError: "DatetimeIndex(['2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01',n               '2004-01-01', '2005-01-01', '2006-01-01', '2007-01-01',n               '2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01',n               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',n               '2016-01-01', '2017-01-01'],n              dtype='datetime64[ns]', name='year', freq=None) not in index"


Here is my dataframe:



rel_uncertain   rel_modal_wk    rel_positive    rel_modal_me    rel_modal_st    rel_cnstrain    rel_litigious   rel_negative    year
0 0.003264 0.000568 0.007659 0.002442 0.011443 0.002170 0.004246 0.003857 2000-01-01
1 0.006195 0.002193 0.007991 0.002325 0.009876 0.002229 0.007343 0.006189 2001-01-01
2 0.005952 0.000719 0.011846 0.001773 0.009697 0.001392 0.001874 0.003313 2002-01-01
3 0.006306 0.001038 0.008452 0.001925 0.009386 0.001180 0.002856 0.001219 2003-01-01
4 0.005013 0.001374 0.007348 0.001017 0.008259 0.001533 0.003306 0.004437 2004-01-01


this is the way i transfermed it np arrays in order to plot it



    year=np.array(df12.year)
un=np.array(df12.rel_uncertain,)
neg= np.array(df12.rel_negative)


then im plotting it



plt.plot(x,neg, label = 'neg')
plt.plot(x,pos,)


works fine except for the legend
i tried to use mlp's legend function and to create the legend manually
therefore i altered my code:



negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
plt.plot(x,pos,)
plt.legend(handles=[negative])


which gives me the following error:



AttributeError: 'list' object has no attribute 'get_label'









share|improve this question















marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Check the parameter type you are passing.
    – Hayat
    Nov 22 at 17:28












  • what exactly do you mean?
    – user10395806
    Nov 22 at 17:45










  • Also see this question
    – ImportanceOfBeingErnest
    Nov 22 at 19:40















up vote
0
down vote

favorite













This question already has an answer here:




  • Python legend attribute error

    1 answer




I have a dataframe which i had to transfer into numpy arrays, because i want to plot over the index which is a datetime but i get this error:



KeyError: "DatetimeIndex(['2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01',n               '2004-01-01', '2005-01-01', '2006-01-01', '2007-01-01',n               '2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01',n               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',n               '2016-01-01', '2017-01-01'],n              dtype='datetime64[ns]', name='year', freq=None) not in index"


Here is my dataframe:



rel_uncertain   rel_modal_wk    rel_positive    rel_modal_me    rel_modal_st    rel_cnstrain    rel_litigious   rel_negative    year
0 0.003264 0.000568 0.007659 0.002442 0.011443 0.002170 0.004246 0.003857 2000-01-01
1 0.006195 0.002193 0.007991 0.002325 0.009876 0.002229 0.007343 0.006189 2001-01-01
2 0.005952 0.000719 0.011846 0.001773 0.009697 0.001392 0.001874 0.003313 2002-01-01
3 0.006306 0.001038 0.008452 0.001925 0.009386 0.001180 0.002856 0.001219 2003-01-01
4 0.005013 0.001374 0.007348 0.001017 0.008259 0.001533 0.003306 0.004437 2004-01-01


this is the way i transfermed it np arrays in order to plot it



    year=np.array(df12.year)
un=np.array(df12.rel_uncertain,)
neg= np.array(df12.rel_negative)


then im plotting it



plt.plot(x,neg, label = 'neg')
plt.plot(x,pos,)


works fine except for the legend
i tried to use mlp's legend function and to create the legend manually
therefore i altered my code:



negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
plt.plot(x,pos,)
plt.legend(handles=[negative])


which gives me the following error:



AttributeError: 'list' object has no attribute 'get_label'









share|improve this question















marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Check the parameter type you are passing.
    – Hayat
    Nov 22 at 17:28












  • what exactly do you mean?
    – user10395806
    Nov 22 at 17:45










  • Also see this question
    – ImportanceOfBeingErnest
    Nov 22 at 19:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Python legend attribute error

    1 answer




I have a dataframe which i had to transfer into numpy arrays, because i want to plot over the index which is a datetime but i get this error:



KeyError: "DatetimeIndex(['2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01',n               '2004-01-01', '2005-01-01', '2006-01-01', '2007-01-01',n               '2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01',n               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',n               '2016-01-01', '2017-01-01'],n              dtype='datetime64[ns]', name='year', freq=None) not in index"


Here is my dataframe:



rel_uncertain   rel_modal_wk    rel_positive    rel_modal_me    rel_modal_st    rel_cnstrain    rel_litigious   rel_negative    year
0 0.003264 0.000568 0.007659 0.002442 0.011443 0.002170 0.004246 0.003857 2000-01-01
1 0.006195 0.002193 0.007991 0.002325 0.009876 0.002229 0.007343 0.006189 2001-01-01
2 0.005952 0.000719 0.011846 0.001773 0.009697 0.001392 0.001874 0.003313 2002-01-01
3 0.006306 0.001038 0.008452 0.001925 0.009386 0.001180 0.002856 0.001219 2003-01-01
4 0.005013 0.001374 0.007348 0.001017 0.008259 0.001533 0.003306 0.004437 2004-01-01


this is the way i transfermed it np arrays in order to plot it



    year=np.array(df12.year)
un=np.array(df12.rel_uncertain,)
neg= np.array(df12.rel_negative)


then im plotting it



plt.plot(x,neg, label = 'neg')
plt.plot(x,pos,)


works fine except for the legend
i tried to use mlp's legend function and to create the legend manually
therefore i altered my code:



negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
plt.plot(x,pos,)
plt.legend(handles=[negative])


which gives me the following error:



AttributeError: 'list' object has no attribute 'get_label'









share|improve this question
















This question already has an answer here:




  • Python legend attribute error

    1 answer




I have a dataframe which i had to transfer into numpy arrays, because i want to plot over the index which is a datetime but i get this error:



KeyError: "DatetimeIndex(['2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01',n               '2004-01-01', '2005-01-01', '2006-01-01', '2007-01-01',n               '2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01',n               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',n               '2016-01-01', '2017-01-01'],n              dtype='datetime64[ns]', name='year', freq=None) not in index"


Here is my dataframe:



rel_uncertain   rel_modal_wk    rel_positive    rel_modal_me    rel_modal_st    rel_cnstrain    rel_litigious   rel_negative    year
0 0.003264 0.000568 0.007659 0.002442 0.011443 0.002170 0.004246 0.003857 2000-01-01
1 0.006195 0.002193 0.007991 0.002325 0.009876 0.002229 0.007343 0.006189 2001-01-01
2 0.005952 0.000719 0.011846 0.001773 0.009697 0.001392 0.001874 0.003313 2002-01-01
3 0.006306 0.001038 0.008452 0.001925 0.009386 0.001180 0.002856 0.001219 2003-01-01
4 0.005013 0.001374 0.007348 0.001017 0.008259 0.001533 0.003306 0.004437 2004-01-01


this is the way i transfermed it np arrays in order to plot it



    year=np.array(df12.year)
un=np.array(df12.rel_uncertain,)
neg= np.array(df12.rel_negative)


then im plotting it



plt.plot(x,neg, label = 'neg')
plt.plot(x,pos,)


works fine except for the legend
i tried to use mlp's legend function and to create the legend manually
therefore i altered my code:



negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
plt.plot(x,pos,)
plt.legend(handles=[negative])


which gives me the following error:



AttributeError: 'list' object has no attribute 'get_label'




This question already has an answer here:




  • Python legend attribute error

    1 answer








python python-3.x pandas matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:21

























asked Nov 22 at 17:12









user10395806

356




356




marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by ImportanceOfBeingErnest matplotlib
Users with the  matplotlib badge can single-handedly close matplotlib questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Check the parameter type you are passing.
    – Hayat
    Nov 22 at 17:28












  • what exactly do you mean?
    – user10395806
    Nov 22 at 17:45










  • Also see this question
    – ImportanceOfBeingErnest
    Nov 22 at 19:40


















  • Check the parameter type you are passing.
    – Hayat
    Nov 22 at 17:28












  • what exactly do you mean?
    – user10395806
    Nov 22 at 17:45










  • Also see this question
    – ImportanceOfBeingErnest
    Nov 22 at 19:40
















Check the parameter type you are passing.
– Hayat
Nov 22 at 17:28






Check the parameter type you are passing.
– Hayat
Nov 22 at 17:28














what exactly do you mean?
– user10395806
Nov 22 at 17:45




what exactly do you mean?
– user10395806
Nov 22 at 17:45












Also see this question
– ImportanceOfBeingErnest
Nov 22 at 19:40




Also see this question
– ImportanceOfBeingErnest
Nov 22 at 19:40












1 Answer
1






active

oldest

votes

















up vote
0
down vote













solved for the numpy array by calling



negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
positive= plt.plot(x,pos,label = 'positive')
plt.legend(handles=[negative[0],positive[0]])


although i dont really understand it.
found the answer here: Python legend attribute error






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    solved for the numpy array by calling



    negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
    positive= plt.plot(x,pos,label = 'positive')
    plt.legend(handles=[negative[0],positive[0]])


    although i dont really understand it.
    found the answer here: Python legend attribute error






    share|improve this answer

























      up vote
      0
      down vote













      solved for the numpy array by calling



      negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
      positive= plt.plot(x,pos,label = 'positive')
      plt.legend(handles=[negative[0],positive[0]])


      although i dont really understand it.
      found the answer here: Python legend attribute error






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        solved for the numpy array by calling



        negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
        positive= plt.plot(x,pos,label = 'positive')
        plt.legend(handles=[negative[0],positive[0]])


        although i dont really understand it.
        found the answer here: Python legend attribute error






        share|improve this answer












        solved for the numpy array by calling



        negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
        positive= plt.plot(x,pos,label = 'positive')
        plt.legend(handles=[negative[0],positive[0]])


        although i dont really understand it.
        found the answer here: Python legend attribute error







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 17:51









        user10395806

        356




        356















            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo