Files
OpenBB/openbb_terminal/economy/plot_view.py
teh_coderer 59d8b36bb0 [IMPROVE] Fix Docstring formatting/Fix missing, incomplete type hints (#3412)
* Fixes

* Update stocks_helper.py

* update git-actions set-output to new format

* Update stocks_helper.py

* Update terminal_helper.py

* removed LineAnnotateDrawer from qa_view

* lint

* few changes

* updates

* sdk auto gen modules done

* Update stocks_helper.py

* updates to changed imports, and remove first sdk_modules

* Update generate_sdk.py

* Update generate_sdk.py

* pylint

* revert stocks_helper

* Update generate_sdk.py

* Update sdk.py

* Update generate_sdk.py

* full auto generation, added sdk.py/controllers creation

* missed enable forecasting

* added running black in subprocess after sdk files generation completes

* removed deleted sdk_arg_logger

* comment out tests

* property doc fix

* clean up

* Update generate_sdk.py

* make trailmap classes useable for doc generation

* Update generate_sdk.py

* added lineon to trailmap class for linking to func in markdown

* changed lineon to dict

* added full_path to trailmap for linking in docs

* updated portfolio

* feat: initial files

* feat: added meta head

* feat: added funcdef

* added func_def to trailmap attributes for markdown in docs, added missing type hints to covid functions

* feat: added view and merged with jaun

* Update generate_sdk.py

* Update generate_sdk.py

* Update generate_sdk.py

* Update generate_sdk.py

* init

* fix returns

* fix: random stuff

* fix: random

* fixed encoding issue on windows

* fix: generate tabs

* update

* Update generate_sdk_markdown.py

* Create .pydocstyle.ini

* added type hint classes for views

* fixes

* alt, ba

* alt-economy

* Update finviz_compare_model.py

* fixs

* Update substack_model.py

* Update generate_sdk.py

* last of my section

* porfolio

* po

* Update optimizer_model.py

* fixing more things

* few more

* keys done

* update

* fixes

* Update generate_sdk_markdown.py

* Update generate_sdk_markdown.py

* mypy forecast fix

* Update generate_sdk_markdown.py

* Update generate_sdk_markdown.py

* Update generate_sdk_markdown.py

* fixes

* forecast fixes

* one more fix

* Update coinbase_model.py

* Update generate_sdk_markdown.py

Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: jose-donato <zmcdonato@gmail.com>
Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>
2022-11-15 16:37:49 -05:00

158 lines
4.0 KiB
Python

""" Plot Controller """
import os
from typing import Optional, List, Dict, Any
from textwrap import fill
import pandas as pd
from matplotlib import pyplot as plt
from openbb_terminal.config_plot import PLOT_DPI
from openbb_terminal.config_terminal import theme
from openbb_terminal.helper_funcs import (
plot_autoscale,
export_data,
print_rich_table,
)
def show_plot(
dataset_yaxis_1,
dataset_yaxis_2,
export,
external_axes: Optional[List[plt.Axes]] = None,
):
"""
The ability to plot any data coming from EconDB, FRED or Yahoo Finance.
Parameters
----------
dataset_yaxis_1: pd.DataFrame
Data you wish to plot on the first y-axis.
dataset_yaxis_2 : pd.DataFrame
Data you wish to plot on the second y-axis.
external_axes: Optional[List[plt.axes]]
External axes to plot on
Returns
-------
Plots the data.
"""
if external_axes is None:
_, ax1 = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
else:
ax1, ax2 = external_axes
color_palette = theme.get_colors()
ax_1_coloring = 0
ax_2_coloring = -1
dataset_yaxis_1 = dataset_yaxis_1.dropna()
for column in dataset_yaxis_1:
ax1.plot(
dataset_yaxis_1[column],
label=column.replace("_", " "),
color=color_palette[ax_1_coloring],
)
ax_1_coloring += 1
ax1.legend(
[fill(column, 45) for column in dataset_yaxis_1.columns],
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
prop={"size": 9},
)
theme.style_primary_axis(ax1)
if not dataset_yaxis_2.empty:
ax2 = ax1.twinx()
dataset_yaxis_2 = dataset_yaxis_2.dropna()
for column in dataset_yaxis_2:
ax2.plot(
dataset_yaxis_2[column],
label=column,
color=color_palette[ax_2_coloring],
)
ax_2_coloring += -1
theme.style_twin_axis(ax2)
ax2.legend(
[fill(column, 45) for column in dataset_yaxis_2.columns],
bbox_to_anchor=(0.55, 0.40, 1, -0.52),
loc="upper left",
mode="expand",
borderaxespad=0,
prop={"size": 9},
)
if external_axes is None:
theme.visualize_output()
if export:
df = pd.concat([dataset_yaxis_1, dataset_yaxis_2], axis=1)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"plot_macro_data",
df,
)
def show_options(
datasets: Dict[Any, pd.DataFrame], raw: str = "", limit: int = 10, export: str = ""
):
"""
The ability to plot any data coming from EconDB, FRED or Yahoo Finance.
Parameters
----------
datasets: Dict[Any, pd.DataFrame]
A dictionary with the format {command: data}.
raw : bool
Whether you wish to show the data available.
limit: int
The amount of rows you wish to show.
export: bool
Whether you want to export the data.
Returns
-------
Plots the data.
"""
if raw or export:
df = pd.DataFrame()
for _, data in datasets.items():
df = pd.concat([df, data], axis=1)
if raw:
print_rich_table(
df.tail(limit),
show_index=True,
headers=list(df.columns),
title="Macro data",
)
if export:
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"dataset",
df,
)
else:
options = {
command: ", ".join(values.keys()) for command, values in datasets.items()
}
print_rich_table(
pd.DataFrame.from_dict(options, orient="index", columns=["Options"]),
show_index=True,
index_name="Command",
title="Options available to plot",
)