Nick T
Nick T

Reputation: 683

Filling between x and y values on matplotlib with range of x values

I'm using panda and matplotlib. With some sort of color, I would like to fill in the space between my x-axis and the price(aka close). How would I go about this? I am a beginner with python.

import datetime as dt
import pandas_datareader.data as web

import matplotlib.pyplot as plt
import pandas as pd


# Defining Axis
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0)) 


# Data Section
start = dt.datetime(2017,1,1)
end   = dt.datetime(2018,1,1)


sp500 = web.DataReader("TSLA", "yahoo", start, end)
close = sp500['Adj Close']



# Axis Settings Section
ax1.fill_between(start, close, 0, color='grey')  #here is where I'm having issues. I think the issues im having partly involve the fact that im dealing with a range of dates
ax1.plot(close, label = 'price')
ax1.grid(True, color= 'lightgreen', linestyle= '-')
ax1.xaxis.label.set_color('dodgerblue')
ax1.yaxis.label.set_color('indianred')
ax1.set_yticks([0,100,200,300,400])                 




# Graph/Chart Settings
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.xticks(rotation=45)
plt.subplots_adjust(left=0.09, bottom=0.20, top=0.90, wspace=0.20, hspace=0.0)
plt.show()

Upvotes: 1

Views: 1822

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Try this. You need an array of x values, not just the one scalar value in 'start':

ax1.fill_between(sp500.index, close, 0, color='grey')  

Output:

enter image description here

Upvotes: 2

Owen
Owen

Reputation: 448

You could try using matplotlib.pyplot.fill_between(). Doc: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.fill_between.html

EDIT: I see you're already using it. Maybe you should include your problem in your question instead of in your code example. I'll look a little closer to see if I can find a solution.

Upvotes: 0

Related Questions