Legend generate additional space when it combines add_legend function with the legend option in plot function #532
DanielCampoo
started this conversation in
General
Replies: 1 comment
-
You might be able to get rid of the spacing by using the same HTML-layout technique used for legend previously. You probably have to look into pyqtgraph to be able to adjust that margin or layout. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When I add a legend using the Add_legend function to display the opening, high, low, and closing data of the stock based on the cursor position, and combine it with the 'legend' option of the plot function, extra spaces are generated in the legend label of the chart (see photos). How can this problem be resolved?
The code used is:
import finplot as fplt
import pandas as pd
import requests
from io import StringIO
from time import time
load data and convert date
end_t = int(time())
start_t = end_t - 2430246060 # 24 months
symbol = 'SPY'
interval = '1d'
url = 'https://query1.finance.yahoo.com/v7/finance/download/%s?period1=%s&period2=%s&interval=%s&events=history' % (symbol, start_t, end_t, interval)
r = requests.get(url, headers={'user-agent':'Mozilla/5.0'})
df = pd.read_csv(StringIO(r.text))
df['Date'] = pd.to_datetime(df['Date']).astype('int64') # use finplot's internal representation, which is ns
ax,ax2 = fplt.create_plot('S&P 500 MACD', rows=2)
plot macd with standard colors first
macd = df.Close.ewm(span=12).mean() - df.Close.ewm(span=26).mean()
signal = macd.ewm(span=9).mean()
df['macd_diff'] = macd - signal
fplt.volume_ocv(df[['Date','Open','Close','macd_diff']], ax=ax2, colorfunc=fplt.strength_colorfilter)
fplt.plot(macd, ax=ax2, legend='MACD')
fplt.plot(signal, ax=ax2, legend='Signal')
change to b/w coloring templates for next plots
fplt.candle_bull_color = fplt.candle_bear_color = fplt.candle_bear_body_color = '#000'
fplt.volume_bull_color = fplt.volume_bear_color = '#333'
fplt.candle_bull_body_color = fplt.volume_bull_body_color = '#fff'
plot price and volume
fplt.candlestick_ochl(df[['Date','Open','Close','High','Low']], ax=ax)
hover_label = fplt.add_legend('', ax=ax)
axo = ax.overlay()
fplt.volume_ocv(df[['Date','Open','Close','Volume']], ax=axo)
fplt.plot(df.Volume.ewm(span=24).mean(), ax=axo, color=1)
Plot SMA20
sma = df.Close.rolling(20).mean()
fplt.plot(sma, ax=ax, legend='SMA20')
Plot SMA50
sma = df.Close.rolling(50).mean()
fplt.plot(sma, ax=ax, legend='SMA50')
#######################################################
update crosshair and legend when moving the mouse
def update_legend_text(x, y):
row = df.loc[df.Date==x]
# format html with the candle and set legend
fmt = '%%.2f' % ('0b0' if (row.Open<row.Close).all() else 'a00')
rawtxt = '%%s %%s O%s C%s H%s L%s' % (fmt, fmt, fmt, fmt)
values = [v.iloc[0] for v in (row.Open, row.Close, row.High, row.Low)]
hover_label.setText(rawtxt % tuple([symbol, interval.upper()] + values))
def update_crosshair_text(x, y, xtext, ytext):
ytext = '%s (Close%+.2f)' % (ytext, (y - df.iloc[x].Close))
return xtext, ytext
fplt.set_mouse_callback(update_legend_text, ax=ax, when='hover')
fplt.add_crosshair_info(update_crosshair_text, ax=ax)
fplt.show()
Beta Was this translation helpful? Give feedback.
All reactions