Skip to content

Commit

Permalink
switch to matplotlib.rc
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoup42 committed Oct 13, 2023
1 parent 608a423 commit c91c908
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cardano_account_pandas_dumper = "cardano_account_pandas_dumper.__main__:main"
where = ["src"]

[tool.setuptools.package-data]
cardano_account_pandas_dumper = ["*.jsonc", "*.webp"]
cardano_account_pandas_dumper = ["*.jsonc", "*.webp", "*.rc"]

[tool.mypy]
follow_imports = "normal"
Expand Down
21 changes: 14 additions & 7 deletions src/cardano_account_pandas_dumper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from json import JSONDecodeError

import jstyleson
import matplotlib as mpl
import matplotlib.pyplot as plt
from blockfrost import ApiError, BlockFrostApi
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
Expand Down Expand Up @@ -72,6 +73,12 @@ def _create_arg_parser():
type=str,

)
result.add_argument(
"--matplotlib_rc",
help="Path to matplotlib defaults file.",
type=str,
default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "matplotlib.rc"),
)
result.add_argument(
"--detail_level",
help="Level of detail of report (1=only own addresses, 2=other addresses as well).",
Expand Down Expand Up @@ -229,13 +236,13 @@ def main():
except OSError as exception:
warnings.warn(f"Failed to write .xlsx file: {exception}")
if args.graph_output:
reporter.plot_balance()
try:
plt.savefig(args.graph_output,
metadata=reporter.get_graph_metadata(args.graph_output),
pad_inches=0.5)
except OSError as exception:
warnings.warn(f"Failed to write graph file: {exception}")
with mpl.rc_context(fname=args.matplotlib_rc):
try:
reporter.plot_balance()
plt.savefig(args.graph_output,
metadata=reporter.get_graph_metadata(args.graph_output))
except OSError as exception:
warnings.warn(f"Failed to write graph file: {exception}")
print("Done.")


Expand Down
17 changes: 4 additions & 13 deletions src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,9 @@ def plot_balance(self):
inplace=True,
key=lambda i: [self.asset_names.get(x, x) for x in i],
)
font_properties = FontProperties(size="medium")
fig = pyplot.figure(constrained_layout=True,figsize=(20,14))
plot_ax=pyplot.subplot2grid(fig=fig,shape=(1,7),loc=(0,0),colspan=6)
legend_ax=pyplot.subplot2grid(fig=fig,shape=(1,7),loc=(0,6),colspan=1)
font_properties = FontProperties(size=mpl.rcParams['legend.fontsize'])
plot_ax=pyplot.subplot2grid(shape=(1,7),loc=(0,0),colspan=6)
legend_ax=pyplot.subplot2grid(shape=(1,7),loc=(0,6),colspan=1)

plot=balance.plot(
ax=plot_ax,
Expand All @@ -680,7 +679,6 @@ def plot_balance(self):
text=pyplot.text(x=0,y=0,s="M", font_properties=font_properties)
text_bbox=text.get_window_extent()
text.remove()
legend_font_scale=2
legend_ax.axis("off")
for text in legend_ax.legend(
plot.get_lines(),
Expand All @@ -692,16 +690,9 @@ def plot_balance(self):
asset_obj=self.data.assets.get(balance.columns[i],None))
for i in range(len(balance.columns))
},
labelcolor="linecolor",
prop=font_properties,
handleheight=legend_font_scale,
handlelength=legend_font_scale,
frameon=False,
loc="center right"

).get_texts():
text.set(y=text.get_window_extent().y0 + legend_font_scale * text_bbox.height / 2)
pyplot.tight_layout()
text.set(y=text.get_window_extent().y0 + mpl.rcParams['legend.handleheight'] * text_bbox.height / 2)

def get_graph_metadata(self, filename:str) -> Mapping :
"""Return graph metadata depending on file extension."""
Expand Down
9 changes: 9 additions & 0 deletions src/cardano_account_pandas_dumper/matplotlib.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
savefig.pad_inches:0.5
legend.fontsize:medium
legend.labelcolor:linecolor
legend.handleheight:2
legend.handlelength:2
legend.frameon:False
legend.loc:center
figure.autolayout: True
figure.figsize: 20,14

0 comments on commit c91c908

Please sign in to comment.