where condition in fill_between isn't working












1















Trying to use the following code:



ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


It isn't working at all



I did the following things:
1. Tried running just the '>' condition: it filled the entire figure
2. Tried running just the '<' condition: it didn't fill anything



import datetime as dt
import matplotlib.pyplot as plt
import pandas_datareader.data as web

start = dt.datetime(2000, 1, 1)
end = dt.datetime(2016, 12, 31)
df = web.DataReader('TWTR', 'yahoo', start, end)
df.reset_index(inplace = True)

stock = 'Twitter'

date = df['Date'].tolist()

closep = df['Close'].tolist()

fig = plt.figure()

ax1 = plt.subplot2grid((1,1), (0,0))
ax1.plot_date(date, closep, '-', label = 'Price')
ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3)
ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)

for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)

ax1.grid(True)
ax1.xaxis.label.set_color('c')
ax1.yaxis.label.set_color('r')
plt.xlabel('date')
plt.ylabel('Close Price')
plt.title(stock)
plt.legend()
plt.show()


On running the above I get:



result1: All Fill



I tried the following:



#ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


This gives me:



result2: No Fill



Next I tried this:



ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
#ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


Which gave me:
result3: All Fill



Ideally, the are above closep[0] should have been coloured and the one below it should have been blank.



However, everything is getting filled. Moreover, just running the '>' fills everything and just running the '<' condition doesn't fill anything



I use python2.7










share|improve this question





























    1















    Trying to use the following code:



    ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
    ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


    It isn't working at all



    I did the following things:
    1. Tried running just the '>' condition: it filled the entire figure
    2. Tried running just the '<' condition: it didn't fill anything



    import datetime as dt
    import matplotlib.pyplot as plt
    import pandas_datareader.data as web

    start = dt.datetime(2000, 1, 1)
    end = dt.datetime(2016, 12, 31)
    df = web.DataReader('TWTR', 'yahoo', start, end)
    df.reset_index(inplace = True)

    stock = 'Twitter'

    date = df['Date'].tolist()

    closep = df['Close'].tolist()

    fig = plt.figure()

    ax1 = plt.subplot2grid((1,1), (0,0))
    ax1.plot_date(date, closep, '-', label = 'Price')
    ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3)
    ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)

    for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)

    ax1.grid(True)
    ax1.xaxis.label.set_color('c')
    ax1.yaxis.label.set_color('r')
    plt.xlabel('date')
    plt.ylabel('Close Price')
    plt.title(stock)
    plt.legend()
    plt.show()


    On running the above I get:



    result1: All Fill



    I tried the following:



    #ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
    ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


    This gives me:



    result2: No Fill



    Next I tried this:



    ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
    #ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


    Which gave me:
    result3: All Fill



    Ideally, the are above closep[0] should have been coloured and the one below it should have been blank.



    However, everything is getting filled. Moreover, just running the '>' fills everything and just running the '<' condition doesn't fill anything



    I use python2.7










    share|improve this question



























      1












      1








      1








      Trying to use the following code:



      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      It isn't working at all



      I did the following things:
      1. Tried running just the '>' condition: it filled the entire figure
      2. Tried running just the '<' condition: it didn't fill anything



      import datetime as dt
      import matplotlib.pyplot as plt
      import pandas_datareader.data as web

      start = dt.datetime(2000, 1, 1)
      end = dt.datetime(2016, 12, 31)
      df = web.DataReader('TWTR', 'yahoo', start, end)
      df.reset_index(inplace = True)

      stock = 'Twitter'

      date = df['Date'].tolist()

      closep = df['Close'].tolist()

      fig = plt.figure()

      ax1 = plt.subplot2grid((1,1), (0,0))
      ax1.plot_date(date, closep, '-', label = 'Price')
      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3)
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)

      for label in ax1.xaxis.get_ticklabels():
      label.set_rotation(45)

      ax1.grid(True)
      ax1.xaxis.label.set_color('c')
      ax1.yaxis.label.set_color('r')
      plt.xlabel('date')
      plt.ylabel('Close Price')
      plt.title(stock)
      plt.legend()
      plt.show()


      On running the above I get:



      result1: All Fill



      I tried the following:



      #ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      This gives me:



      result2: No Fill



      Next I tried this:



      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      #ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      Which gave me:
      result3: All Fill



      Ideally, the are above closep[0] should have been coloured and the one below it should have been blank.



      However, everything is getting filled. Moreover, just running the '>' fills everything and just running the '<' condition doesn't fill anything



      I use python2.7










      share|improve this question
















      Trying to use the following code:



      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      It isn't working at all



      I did the following things:
      1. Tried running just the '>' condition: it filled the entire figure
      2. Tried running just the '<' condition: it didn't fill anything



      import datetime as dt
      import matplotlib.pyplot as plt
      import pandas_datareader.data as web

      start = dt.datetime(2000, 1, 1)
      end = dt.datetime(2016, 12, 31)
      df = web.DataReader('TWTR', 'yahoo', start, end)
      df.reset_index(inplace = True)

      stock = 'Twitter'

      date = df['Date'].tolist()

      closep = df['Close'].tolist()

      fig = plt.figure()

      ax1 = plt.subplot2grid((1,1), (0,0))
      ax1.plot_date(date, closep, '-', label = 'Price')
      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3)
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)

      for label in ax1.xaxis.get_ticklabels():
      label.set_rotation(45)

      ax1.grid(True)
      ax1.xaxis.label.set_color('c')
      ax1.yaxis.label.set_color('r')
      plt.xlabel('date')
      plt.ylabel('Close Price')
      plt.title(stock)
      plt.legend()
      plt.show()


      On running the above I get:



      result1: All Fill



      I tried the following:



      #ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      This gives me:



      result2: No Fill



      Next I tried this:



      ax1.fill_between(date, closep, closep[0], where = closep > closep[0], facecolor = 'g', alpha = 0.3) 
      #ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)


      Which gave me:
      result3: All Fill



      Ideally, the are above closep[0] should have been coloured and the one below it should have been blank.



      However, everything is getting filled. Moreover, just running the '>' fills everything and just running the '<' condition doesn't fill anything



      I use python2.7







      python matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 5:44









      Nino Filiu

      2,78031428




      2,78031428










      asked Jan 2 at 17:39









      ketaningle80ketaningle80

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try:



          import datetime as dt
          import matplotlib.pyplot as plt
          import pandas_datareader.data as web

          start = dt.datetime(2000, 1, 1)
          end = dt.datetime(2016, 12, 31)
          df = web.DataReader('TWTR', 'yahoo', start, end)
          df.reset_index(inplace = True)

          stock = 'Twitter'

          date = df['Date'].tolist()

          closep = df['Close'].tolist()

          fig = plt.figure()

          ax1 = plt.subplot2grid((1,1), (0,0))
          ax1.plot_date(date, closep, '-', label = 'Price')
          ax1.fill_between(date, closep, closep[0], where = [i > closep[0] for i in closep], facecolor = 'g', alpha = 0.3)
          ax1.fill_between(date, closep, closep[0], where = [i < closep[0] for i in closep] , facecolor = 'r', alpha = 0.3)

          for label in ax1.xaxis.get_ticklabels():
          label.set_rotation(45)

          ax1.grid(True)
          ax1.xaxis.label.set_color('c')
          ax1.yaxis.label.set_color('r')
          plt.xlabel('date')
          plt.ylabel('Close Price')
          plt.title(stock)
          plt.legend()
          plt.show()


          Output:



          enter image description here






          share|improve this answer


























          • It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

            – ketaningle80
            Jan 2 at 17:56











          • @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

            – Scott Boston
            Jan 2 at 18:24












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010770%2fwhere-condition-in-fill-between-isnt-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try:



          import datetime as dt
          import matplotlib.pyplot as plt
          import pandas_datareader.data as web

          start = dt.datetime(2000, 1, 1)
          end = dt.datetime(2016, 12, 31)
          df = web.DataReader('TWTR', 'yahoo', start, end)
          df.reset_index(inplace = True)

          stock = 'Twitter'

          date = df['Date'].tolist()

          closep = df['Close'].tolist()

          fig = plt.figure()

          ax1 = plt.subplot2grid((1,1), (0,0))
          ax1.plot_date(date, closep, '-', label = 'Price')
          ax1.fill_between(date, closep, closep[0], where = [i > closep[0] for i in closep], facecolor = 'g', alpha = 0.3)
          ax1.fill_between(date, closep, closep[0], where = [i < closep[0] for i in closep] , facecolor = 'r', alpha = 0.3)

          for label in ax1.xaxis.get_ticklabels():
          label.set_rotation(45)

          ax1.grid(True)
          ax1.xaxis.label.set_color('c')
          ax1.yaxis.label.set_color('r')
          plt.xlabel('date')
          plt.ylabel('Close Price')
          plt.title(stock)
          plt.legend()
          plt.show()


          Output:



          enter image description here






          share|improve this answer


























          • It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

            – ketaningle80
            Jan 2 at 17:56











          • @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

            – Scott Boston
            Jan 2 at 18:24
















          0














          Try:



          import datetime as dt
          import matplotlib.pyplot as plt
          import pandas_datareader.data as web

          start = dt.datetime(2000, 1, 1)
          end = dt.datetime(2016, 12, 31)
          df = web.DataReader('TWTR', 'yahoo', start, end)
          df.reset_index(inplace = True)

          stock = 'Twitter'

          date = df['Date'].tolist()

          closep = df['Close'].tolist()

          fig = plt.figure()

          ax1 = plt.subplot2grid((1,1), (0,0))
          ax1.plot_date(date, closep, '-', label = 'Price')
          ax1.fill_between(date, closep, closep[0], where = [i > closep[0] for i in closep], facecolor = 'g', alpha = 0.3)
          ax1.fill_between(date, closep, closep[0], where = [i < closep[0] for i in closep] , facecolor = 'r', alpha = 0.3)

          for label in ax1.xaxis.get_ticklabels():
          label.set_rotation(45)

          ax1.grid(True)
          ax1.xaxis.label.set_color('c')
          ax1.yaxis.label.set_color('r')
          plt.xlabel('date')
          plt.ylabel('Close Price')
          plt.title(stock)
          plt.legend()
          plt.show()


          Output:



          enter image description here






          share|improve this answer


























          • It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

            – ketaningle80
            Jan 2 at 17:56











          • @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

            – Scott Boston
            Jan 2 at 18:24














          0












          0








          0







          Try:



          import datetime as dt
          import matplotlib.pyplot as plt
          import pandas_datareader.data as web

          start = dt.datetime(2000, 1, 1)
          end = dt.datetime(2016, 12, 31)
          df = web.DataReader('TWTR', 'yahoo', start, end)
          df.reset_index(inplace = True)

          stock = 'Twitter'

          date = df['Date'].tolist()

          closep = df['Close'].tolist()

          fig = plt.figure()

          ax1 = plt.subplot2grid((1,1), (0,0))
          ax1.plot_date(date, closep, '-', label = 'Price')
          ax1.fill_between(date, closep, closep[0], where = [i > closep[0] for i in closep], facecolor = 'g', alpha = 0.3)
          ax1.fill_between(date, closep, closep[0], where = [i < closep[0] for i in closep] , facecolor = 'r', alpha = 0.3)

          for label in ax1.xaxis.get_ticklabels():
          label.set_rotation(45)

          ax1.grid(True)
          ax1.xaxis.label.set_color('c')
          ax1.yaxis.label.set_color('r')
          plt.xlabel('date')
          plt.ylabel('Close Price')
          plt.title(stock)
          plt.legend()
          plt.show()


          Output:



          enter image description here






          share|improve this answer















          Try:



          import datetime as dt
          import matplotlib.pyplot as plt
          import pandas_datareader.data as web

          start = dt.datetime(2000, 1, 1)
          end = dt.datetime(2016, 12, 31)
          df = web.DataReader('TWTR', 'yahoo', start, end)
          df.reset_index(inplace = True)

          stock = 'Twitter'

          date = df['Date'].tolist()

          closep = df['Close'].tolist()

          fig = plt.figure()

          ax1 = plt.subplot2grid((1,1), (0,0))
          ax1.plot_date(date, closep, '-', label = 'Price')
          ax1.fill_between(date, closep, closep[0], where = [i > closep[0] for i in closep], facecolor = 'g', alpha = 0.3)
          ax1.fill_between(date, closep, closep[0], where = [i < closep[0] for i in closep] , facecolor = 'r', alpha = 0.3)

          for label in ax1.xaxis.get_ticklabels():
          label.set_rotation(45)

          ax1.grid(True)
          ax1.xaxis.label.set_color('c')
          ax1.yaxis.label.set_color('r')
          plt.xlabel('date')
          plt.ylabel('Close Price')
          plt.title(stock)
          plt.legend()
          plt.show()


          Output:



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 18:57

























          answered Jan 2 at 17:45









          Scott BostonScott Boston

          57.3k73258




          57.3k73258













          • It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

            – ketaningle80
            Jan 2 at 17:56











          • @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

            – Scott Boston
            Jan 2 at 18:24



















          • It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

            – ketaningle80
            Jan 2 at 17:56











          • @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

            – Scott Boston
            Jan 2 at 18:24

















          It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

          – ketaningle80
          Jan 2 at 17:56





          It worked! Thanks! Can you please explain why the for loop was necessary? Haven't come across it anywhere. Also, why was there a different output when the '>/<' conditions were used separately?

          – ketaningle80
          Jan 2 at 17:56













          @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

          – Scott Boston
          Jan 2 at 18:24





          @ketaningle80 well, you can't compare a list to a value in python. Now, if you didn't do the .tolist() at the end of df['close'] you, it would have worked because you can compare a np.array to a value. closep = df['Close'] then ax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3) will work.

          – Scott Boston
          Jan 2 at 18:24




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010770%2fwhere-condition-in-fill-between-isnt-working%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith