1 year ago
#325841

Zen4ttitude
How to plot the last n records of a dataframe using pandas.DataFrame.tail()
I'd like to understand this odd behaviour from tail() when it is used inside a function. Here is the data:
| Date | Open | High | Low | Close | Adj Close | Volume | SMA_200 | middle_band | stddev | upper_band | lower_band | RSI |
|:--------------------|-------:|-------:|-------:|--------:|------------:|------------:|----------:|--------------:|---------:|-------------:|-------------:|---------:|
| 2022-03-09 00:00:00 | 839.48 | 860.56 | 832.01 | 858.97 | 858.97 | 1.9728e+07 | 843.345 | 856.394 | 44.6167 | 943.843 | 768.946 | 80.1658 |
| 2022-03-10 00:00:00 | 851.45 | 854.45 | 810.36 | 838.3 | 838.3 | 1.95495e+07 | 844.513 | 851.709 | 41.0356 | 932.139 | 771.28 | 46.4027 |
| 2022-03-11 00:00:00 | 840.2 | 843.8 | 793.77 | 795.35 | 795.35 | 2.22728e+07 | 845.395 | 846.249 | 40.8995 | 926.412 | 766.086 | 16.872 |
| 2022-03-14 00:00:00 | 780.61 | 800.7 | 756.04 | 766.37 | 766.37 | 2.37174e+07 | 846.072 | 841.568 | 44.4474 | 928.685 | 754.451 | 9.07682 |
| 2022-03-15 00:00:00 | 775.27 | 805.57 | 756.57 | 801.89 | 801.89 | 2.22804e+07 | 846.956 | 837.874 | 44.5258 | 925.145 | 750.604 | 57.3646 |
| 2022-03-16 00:00:00 | 809 | 842 | 802.26 | 840.23 | 840.23 | 2.80096e+07 | 848.037 | 833.764 | 39.8592 | 911.889 | 755.64 | 80.1371 |
| 2022-03-17 00:00:00 | 830.99 | 875 | 825.72 | 871.6 | 871.6 | 2.21943e+07 | 849.37 | 831.175 | 35.1321 | 900.034 | 762.316 | 89.4011 |
| 2022-03-18 00:00:00 | 874.49 | 907.85 | 867.39 | 905.39 | 905.39 | 3.34085e+07 | 851.032 | 832.627 | 37.6102 | 906.343 | 758.911 | 94.7131 |
| 2022-03-21 00:00:00 | 914.98 | 942.85 | 907.09 | 921.16 | 921.16 | 2.73272e+07 | 852.643 | 835.836 | 42.2493 | 918.645 | 753.027 | 96.3981 |
| 2022-03-22 00:00:00 | 930 | 997.8 | 921.75 | 993.98 | 993.98 | 3.52895e+07 | 854.587 | 844.458 | 54.8841 | 952.031 | 736.886 | 99.0866 |
Here is one way of plotting it: import plotly.express as px ticker = 'TSLA'
def boll(ticker,points):
colours = ['steelblue', 'tan', 'black', 'red', 'green']
fig = px.line(df.tail(points), x=df.tail(points).index, y=['middle_band',
'SMA_200', 'upper_band', 'lower_band','Close'], title=f'{ticker}',
color_discrete_sequence=colours, width=800, height=600)
fig.update_traces(textfont_size=12, showlegend=True)
fig.update_layout(legend=dict(
yanchor="top",
y=0.9,
xanchor="left",
x=0.02
))
fig.update_xaxes(title_text='Price evolution over time', title_font = {"size":
16},)
fig.update_yaxes(title_text='Price', tickprefix="$")
fig.show()
boll(ticker,10)
But if I try to use tail() in a function I get a funny scale as if the first records of the entire dataframe (going back to 2015 and not provided here to keep sample small) are considered to determine the scale. (Thanks to Derek Banas for a great part of this code).
def plot_with_boll_bands(df,points,ticker,plot):
fig = go.Figure()
candle = go.Candlestick(x=df.tail(points).index, open=df['Open'],
high=df['High'], low=df['Low'],
close=df['Close'], name="Candlestick")
upper_line = go.Scatter(x=df.tail(points).index, y=df['upper_band'],
line=dict(color='rgba(250, 0, 0, 0.75)',
width=1), name="Upper Band")
mid_line = go.Scatter(x=df.tail(points).index, y=df['middle_band'],
line=dict(color='rgba(0, 0, 250, 0.75)',
width=0.7), name="Middle Band")
lower_line = go.Scatter(x=df.tail(points).index, y=df['lower_band'],
line=dict(color='rgba(0, 250, 0, 0.75)',
width=1), name="Lower Band")
mavg_line = go.Scatter(x=df.tail(points).index, y=df['SMA_200'],
line=dict(color='rgba(119, 136, 153, 1)',
width=1), name="Moving Avg 200d")
close_line = go.Scatter(x=df.tail(points).index, y=df['Close'],
line=dict(color='rgba(5, 5, 5, 1)',
width=1), name="Close Price")
fig.add_trace(candle)
fig.add_trace(upper_line)
fig.add_trace(mid_line)
fig.add_trace(lower_line)
fig.add_trace(mavg_line)
fig.add_trace(close_line)
fig.update_xaxes(title="Date", rangeslider_visible=True)
fig.update_yaxes(title="Price")
fig.update_layout(title=ticker + " Bollinger Bands",
height=1200, width=1800, showlegend=True)
if plot == 'y':
fig.write_image(PATH + ticker + "bollinger.png")
fig.show()
Calling the function:
plot_with_boll_bands(df,10,'TSLA','y') #To get 10 last records
When plotting we get:
Obtaining an obvious error in the scale as the prices are well above $800. Any idea?
plotly.express was imported as px
plotly.graph_objects was imported as go
python
pandas
graph
plotly
tail
0 Answers
Your Answer