где условие в fill_between не работает - PullRequest
0 голосов
/ 02 января 2019

Попытка использовать следующий код:

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)

Не работает вообще

Я сделал следующие вещи: 1. Пытался выполнить только условие «>»: он заполнил всю фигуру 2. Попытка выполнить только условие <: ничего не заполнилось </p>

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()

При выполнении вышеизложенного я получаю:

result1: All Fill

Я попробовал следующее:

#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)

Это дает мне:

result2: No Fill

Далее я попробовал это:

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)

Что дало мне: result3: All Fill

В идеале, верхний сгусток [0] должен быть цветным, а нижний - пустым.

Однако все наполняется. Более того, простое выполнение «>» заполняет все, а простое выполнение условия «<» ничего не заполняет </p>

Я использую python2.7

1 Ответ

0 голосов
/ 02 января 2019

Попробуйте:

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()

Выход:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...