figure
tells you the call signature:
from matplotlib.pyplot import figure
figure(figsize=(8, 6), dpi=80)
figure(figsize=(1,1))
would create an inch-by-inch image, which would be 80-by-80 pixels unless you also give a different dpi argument.
The column names (which are strings) cannot be sliced in the manner you tried.
Here you have a couple of options. If you know from context which variables you want to slice out, you can just return a view of only those columns by passing a list into the __getitem__
syntax (the []'s).
df1 = df[['a', 'b']]
Alternatively, if it matters to index them numerically and not by their name (say your code should automatically do this without knowing the names of the first two columns) then you can do this instead:
df1 = df.iloc[:, 0:2] # Remember that Python does not slice inclusive of the ending index.
Additionally, you should familiarize yourself with the idea of a view into a Pandas object vs. a copy of that object. The first of the above methods will return a new copy in memory of the desired sub-object (the desired slices).
Sometimes, however, there are indexing conventions in Pandas that don't do this and instead give you a new variable that just refers to the same chunk of memory as the sub-object or slice in the original object. This will happen with the second way of indexing, so you can modify it with the .copy()
method to get a regular copy. When this happens, changing what you think is the sliced object can sometimes alter the original object. Always good to be on the look out for this.
df1 = df.iloc[0, 0:2].copy() # To avoid the case where changing df1 also changes df
To use iloc
, you need to know the column positions (or indices). As the column positions may change, instead of hard-coding indices, you can use iloc
along with get_loc
function of columns
method of dataframe object to obtain column indices.
{df.columns.get_loc(c): c for idx, c in enumerate(df.columns)}
Now you can use this dictionary to access columns through names and using iloc
.
Best Answer
See here for my answer to a similar question and here for further information regarding mathplotlib's finance candlestick graph.
To get just the adj close from your sp500, you would use something like
sp500["Adj Close"]
and then pass that to the relevant matplotlib plot commandplt.plot(datelist, sp500["Adj Close"] )
where datelist is your list of dates on the x axis.I believe you can get datelist by referencing
sp500.index
, see here for more information.As for your issue with passing it to the plot command, something like
datelist = [date2num(x) for x in sp500.index]
where the function date2num is from matplotlib.dates package.After setting up the relevant subplot, and then call the appropriate fill command to
fill_between_alpha
the area under the line like the Yahoo graph you linked to.See here under the Fill Between and Alpha heading for another snippet that shows a filled line graph, with correct date printing.
The initial link has a sample matplotlib snippet which also covers the date format and formatting in more detail.