where condition in fill_between isn't working
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:
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:
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:
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
add a comment |
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:
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:
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:
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
add a comment |
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:
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:
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:
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
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:
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:
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:
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
python matplotlib
edited Jan 3 at 5:44


Nino Filiu
2,78031428
2,78031428
asked Jan 2 at 17:39
ketaningle80ketaningle80
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
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']
thenax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)
will work.
– Scott Boston
Jan 2 at 18:24
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
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']
thenax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)
will work.
– Scott Boston
Jan 2 at 18:24
add a comment |
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:
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']
thenax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)
will work.
– Scott Boston
Jan 2 at 18:24
add a comment |
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:
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:
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']
thenax1.fill_between(date, closep, closep[0], where = closep < closep[0], facecolor = 'r', alpha = 0.3)
will work.
– Scott Boston
Jan 2 at 18:24
add a comment |
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']
thenax1.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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