Improve plotting by placing the legend outside the graph

This commit is contained in:
Jeroen Bouma
2022-03-15 17:25:47 +01:00
parent 51ba91e9d1
commit b4a63367d8
5 changed files with 54 additions and 10 deletions

View File

@@ -92,15 +92,30 @@ def show_macro_data(
if len(parameters) > 1 and len(countries) > 1:
ax.plot(df_rounded[column], label=f"{country_label} [{parameter_label}]")
ax.set_title(f"Macro data {denomination}")
ax.legend()
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
elif len(parameters) > 1:
ax.plot(df_rounded[column], label=parameter_label)
ax.set_title(f"{country_label} {denomination}")
ax.legend()
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
elif len(countries) > 1:
ax.plot(df_rounded[column], label=country_label)
ax.set_title(f"{parameter_label} {denomination}")
ax.legend()
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
else:
ax.plot(df_rounded[column])
ax.set_title(f"{parameter_label} of {country_label} {denomination}")
@@ -182,7 +197,12 @@ def show_treasuries(
ax.plot(maturities_data[maturity], label=f"{treasury} [{maturity}]")
ax.set_title("U.S. Treasuries")
ax.legend()
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
theme.style_primary_axis(ax)

View File

@@ -577,11 +577,9 @@ Performance & Valuations
convert_currency=ns_parser.convert_currency,
)
print(self.DATASETS["macro"].columns)
self.DATASETS["macro"].columns = [
"_".join(column) for column in self.DATASETS["macro"].columns
]
print(self.DATASETS["macro"].columns)
else:
econdb_view.show_macro_data(
parameters=ns_parser.parameters,

View File

@@ -147,7 +147,12 @@ def display_fred_series(
else title,
)
ax.legend(prop={"size": 10}, bbox_to_anchor=(0, 1), loc="lower left")
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
ax.set_xlim(data.index[0], data.index[-1])
theme.style_primary_axis(ax)

View File

@@ -47,7 +47,6 @@ def show_plot(
theme.style_primary_axis(ax1)
ax1.yaxis.set_label_position("left")
ax1.legend()
if not dataset_yaxis_2.empty:
ax2 = ax1.twinx()
@@ -63,7 +62,24 @@ def show_plot(
ax2.yaxis.set_label_position("right")
theme.style_twin_axis(ax2)
ax2.legend(loc="upper right")
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
ax1.legend(
h1 + h2,
l1 + l2,
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
else:
ax1.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
if external_axes is None:
theme.visualize_output()

View File

@@ -84,7 +84,12 @@ def show_indices(
ax.plot(indices_data[index], label=label)
ax.set_title("Indices")
ax.legend()
ax.legend(
bbox_to_anchor=(0, 0.40, 1, -0.52),
loc="upper right",
mode="expand",
borderaxespad=0,
)
theme.style_primary_axis(ax)