Skip to content

Commit

Permalink
version is up and can control fontsizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
toshiakiasakura committed Apr 7, 2022
1 parent 9e2aeb5 commit aa81e58
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.csv
*.swp
~*
test_dir/

.DS_Store
build/
Expand Down
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ FROM jupyter/scipy-notebook:584f43f06586
WORKDIR /workdir
EXPOSE 8888

RUN pip install contextplt
# sphinx setting
RUN conda install sphinx -y && \
pip install sphinx-autodoc-typehints
RUN pip install jupyterlab_vim

RUN pip install twine && \
pip install wheel

RUN pip install sphinx && \
pip install sphinx_rtd_theme && \
pip install sphinx-autodoc-typehints && \
pip install nbsphinx
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# contextplt
[![python](https://img.shields.io/pypi/pyversions/contextplt)](https://www.python.org/)
[![PyPI](https://img.shields.io/pypi/v/contextplt.svg)](https://pypi.org/project/py-simple-report/)
[![license](https://img.shields.io/pypi/l/contextplt?color=blue)](https://github.com/toshiakiasakura/py-simple-report/blob/main/LICENSE)

Source code repository for the contextplt package.

You can create a matplotlib figure using context manager.
Expand Down
2 changes: 1 addition & 1 deletion contextplt/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version_info__ = (0,1,6)
__version_info__ = (0,2,1)
__version__ = ".".join(map(str, __version_info__))
50 changes: 34 additions & 16 deletions contextplt/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union, Optional, List, Dict, Tuple, Any

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import matplotlib.dates as mdates
Expand Down Expand Up @@ -27,9 +29,25 @@ class Single():
>>> with contextplt.Single(**kargs) as p:
>>> p.ax.plot(x,y)
"""
def __init__(self, xlim=None, ylim=None, xlabel="", ylabel="",title="",tight=True,
rotation : int = None, save_path=None, figsize=(5,3), dpi=150,
savefig_kargs : dict = {}):
def __init__(
self,
xlim : Optional[List[float]] = None,
ylim : Optional[List[float]] = None,
xlabel : Optional[str] = None,
ylabel : Optional[str] = None,
xlabelfontsize : Optional[float] = None,
ylabelfontsize : Optional[float] = None,
xtickfontsize : Optional[float] = None,
ytickfontsize : Optional[float] = None,
title : Optional[str] =None,
titlefontsize : Optional[float] = None,
tight : bool =True,
rotation : Optional[int] = None,
save_path : Optional[str] =None,
figsize : Tuple[float, float] =(5,3),
dpi : int =150,
savefig_kargs : dict = {}
):
"""Set various parameters.
Attributes:
Expand All @@ -38,30 +56,28 @@ def __init__(self, xlim=None, ylim=None, xlabel="", ylabel="",title="",tight=Tru
title (str) : title.
tight (bool) : tight_layout or not.
"""
vars_ = locals()
for k,v in vars_.items():
setattr(self, k, v)

self.fig = plt.figure(figsize=figsize,dpi=dpi)
self.ax = self.fig.add_subplot(111)

self.xlabel = xlabel
self.ylabel = ylabel

self.ax.set_xlim(xlim) if xlim else None
self.ax.set_ylim(ylim) if ylim else None
self.save_path = save_path
self.title = title
self.tight = tight
self.rotation = rotation
self.savefig_kargs = savefig_kargs

def __enter__(self):
return(self)

def __exit__(self,exc_type, exc_value, exc_traceback):
self.option()

self.ax.set_xlabel(self.xlabel)
self.ax.set_ylabel(self.ylabel)
plt.title(self.title)
plt.xticks(rotation=self.rotation)
self.ax.set_xlabel(self.xlabel, fontsize=self.xlabelfontsize)
self.ax.set_ylabel(self.ylabel, fontsize=self.ylabelfontsize)
self.ax.tick_params(axis='x', which='major', labelsize=self.xtickfontsize,
rotation=self.rotation)
self.ax.tick_params(axis='y', which='major', labelsize=self.ytickfontsize)
plt.title(self.title, fontsize=self.titlefontsize)
plt.tight_layout() if self.tight else None
if self.save_path:
plt.savefig(self.save_path, **self.savefig_kargs)
Expand Down Expand Up @@ -120,7 +136,8 @@ def __init__(self, figsize=(8,6), dpi=150,grid=(2,2) ,suptitle="",

plt.suptitle(suptitle)

def set_ax(self,index,xlim=None, ylim=None, xlabel="", ylabel="",title=""):
def set_ax(self,index,xlim=None, ylim=None,
xlabel="", ylabel="",title="", rotation : int = 0):
"""Return axis object.
Args:
Expand All @@ -132,6 +149,7 @@ def set_ax(self,index,xlim=None, ylim=None, xlabel="", ylabel="",title=""):
ax.set_xlim(xlim) if xlim else None
ax.set_ylim(ylim) if ylim else None
ax.set_title(title)
plt.xticks(rotation=rotation)
return(ax)

def __enter__(self):
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
lab:
image: contextlib
# image: contextlib
# used when create a new image.
build:
context: .
Expand Down

0 comments on commit aa81e58

Please sign in to comment.