Apply dynamic magnitude (KMBTP) to y-axis plots (#1565)

* Apply magnitude functions to y-axis ticker

* Fix testing failing

* Adjust datetime format for x-axis

* Handle small magnitude case

Co-authored-by: jmaslek <jmaslek11@gmail.com>
This commit is contained in:
minhhoang1023
2022-03-29 03:27:35 +07:00
committed by GitHub
parent 94d3242a3f
commit 8bdc35e82f
5 changed files with 45 additions and 4 deletions

View File

@@ -14,7 +14,12 @@ from gamestonk_terminal.config_terminal import theme
from gamestonk_terminal.common.technical_analysis import overlap_model
from gamestonk_terminal.config_plot import PLOT_DPI
from gamestonk_terminal.decorators import log_start_end
from gamestonk_terminal.helper_funcs import export_data, plot_autoscale, reindex_dates
from gamestonk_terminal.helper_funcs import (
export_data,
plot_autoscale,
reindex_dates,
lambda_long_number_format_y_axis,
)
from gamestonk_terminal.rich_config import console
logger = logging.getLogger(__name__)
@@ -166,13 +171,14 @@ def view_vwap(
candle_chart_kwargs["addplot"] = mpf.make_addplot(
df_vwap, width=theme.line_width
)
fig, _ = mpf.plot(day_df, **candle_chart_kwargs)
fig, ax = mpf.plot(day_df, **candle_chart_kwargs)
fig.suptitle(
f"{s_ticker} {s_interval} VWAP",
x=0.055,
y=0.965,
horizontalalignment="left",
)
lambda_long_number_format_y_axis(day_df, "Volume", ax)
theme.visualize_output(force_tight_layout=False)
else:
if len(external_axes) != 3:

View File

@@ -574,6 +574,30 @@ def lambda_long_number_format(num, round_decimal=3) -> str:
return num
def lambda_long_number_format_y_axis(df, y_column, ax):
max_values = df[y_column].values.max()
magnitude = 0
while abs(max_values) >= 1000:
magnitude += 1
max_values /= 1000.0
magnitude_sym = " KMBTP"[magnitude]
# Second y label axis -
if magnitude_sym == " ":
ax[2].set_ylabel(f"{y_column}")
else:
ax[2].set_ylabel(f"{y_column} [{magnitude_sym}]")
divider_map = {" ": 1, "K": 1000, "M": 1000000, "B": 1000000000}
divider = divider_map[magnitude_sym]
ax[2].get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, _: int(x / divider))
)
def lambda_clean_data_values_to_float(val: str) -> float:
"""Cleans data to float based on string ending"""
# Remove any leading or trailing parentheses and spaces

View File

@@ -15,7 +15,10 @@ from gamestonk_terminal.rich_config import console
from gamestonk_terminal.stocks.options import chartexchange_model
from gamestonk_terminal.config_terminal import theme
from gamestonk_terminal.helper_funcs import plot_autoscale
from gamestonk_terminal.helper_funcs import (
plot_autoscale,
lambda_long_number_format_y_axis,
)
logger = logging.getLogger(__name__)
@@ -65,6 +68,7 @@ def display_raw(
"volume_width": 0.8,
},
"warn_too_much_data": 10000,
"datetime_format": "%Y-%b-%d",
}
# This plot has 2 axes
option_type = "call" if call else "put"
@@ -81,6 +85,7 @@ def display_raw(
y=0.965,
horizontalalignment="left",
)
lambda_long_number_format_y_axis(df, "Volume", ax)
theme.visualize_output(force_tight_layout=False)
ax[0].legend()
else:

View File

@@ -21,6 +21,7 @@ from gamestonk_terminal.helper_funcs import (
patch_pandas_text_adjustment,
plot_autoscale,
print_rich_table,
lambda_long_number_format_y_axis,
)
from gamestonk_terminal.rich_config import console
from gamestonk_terminal.stocks.options import op_helpers, tradier_model
@@ -636,19 +637,21 @@ def display_historical(
"volume_linewidth": 0.8,
"volume_width": 0.8,
},
"datetime_format": "%Y-%b-%d",
}
if external_axes is None:
candle_chart_kwargs["returnfig"] = True
candle_chart_kwargs["figratio"] = (10, 7)
candle_chart_kwargs["figscale"] = 1.10
candle_chart_kwargs["figsize"] = plot_autoscale()
fig, _ = mpf.plot(df_hist, **candle_chart_kwargs)
fig, ax = mpf.plot(df_hist, **candle_chart_kwargs)
fig.suptitle(
f"Historical {strike} {op_type.title()}",
x=0.055,
y=0.965,
horizontalalignment="left",
)
lambda_long_number_format_y_axis(df_hist, "volume", ax)
theme.visualize_output(force_tight_layout=False)
else:
if len(external_axes) != 2:

View File

@@ -31,6 +31,7 @@ from gamestonk_terminal.helper_funcs import (
plot_autoscale,
get_user_timezone_or_invalid,
print_rich_table,
lambda_long_number_format_y_axis,
)
from gamestonk_terminal.rich_config import console
@@ -379,7 +380,9 @@ def display_candle(
candle_chart_kwargs["figratio"] = (10, 7)
candle_chart_kwargs["figscale"] = 1.10
candle_chart_kwargs["figsize"] = plot_autoscale()
fig, ax = mpf.plot(df_stock, **candle_chart_kwargs, **kwargs)
lambda_long_number_format_y_axis(df_stock, "Volume", ax)
fig.suptitle(
f"{asset_type} {s_ticker}",