This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add magic command %ggplot + refactoring
- Loading branch information
Showing
7 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
@brief test log(time=1s) | ||
You should indicate a time in seconds. The program ``run_unittests.py`` | ||
will sort all test files by increasing time and run them. | ||
""" | ||
|
||
|
||
import sys, os, unittest, shlex | ||
|
||
|
||
try : | ||
import src | ||
import pyquickhelper | ||
except ImportError : | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", ".."))) | ||
if path not in sys.path : sys.path.append (path) | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..", "..", "pyquickhelper", "src"))) | ||
if path not in sys.path : sys.path.append (path) | ||
import src | ||
import pyquickhelper | ||
|
||
from pyquickhelper import fLOG, get_temp_folder | ||
from src.pyensae.graph_helper.magic_graph import MagicGraph | ||
|
||
|
||
class TestGraph (unittest.TestCase): | ||
|
||
def test_graph_style(self) : | ||
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") | ||
path = os.path.abspath(os.path.dirname(__file__)) | ||
mg = MagicGraph() | ||
cmd = "ggplot" | ||
res = mg.mpl_style(cmd) | ||
fLOG(res) | ||
|
||
|
||
if __name__ == "__main__" : | ||
unittest.main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#-*- coding: utf-8 -*- | ||
""" | ||
@file | ||
@brief Magic commands about graphs | ||
.. versionadded:: 1.1 | ||
""" | ||
import sys, os, pandas | ||
|
||
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic | ||
from IPython.core.magic import line_cell_magic | ||
from IPython.core.display import HTML | ||
|
||
from pyquickhelper import MagicCommandParser, MagicClassWithHelpers | ||
from .matplotlib_helper import mpl_switch_style | ||
|
||
@magics_class | ||
class MagicGraph(MagicClassWithHelpers): | ||
""" | ||
Defines magic commands to list the content of a folder | ||
.. versionadded:: 1.1 | ||
""" | ||
|
||
@staticmethod | ||
def mpl_style_parser(): | ||
""" | ||
defines the way to parse the magic command ``%mpl_style`` | ||
""" | ||
parser = MagicCommandParser(description='changes matplotlib style') | ||
parser.add_argument('style', type=str, help='style, ggplot for exemple', default="ggplot") | ||
return parser | ||
|
||
@line_magic | ||
def mpl_style(self, line): | ||
""" | ||
defines ``%mpl_style`` | ||
which changes the style of matplotlib graphs, example: ``%mpl_style ggplot`` | ||
""" | ||
parser = self.get_parser(MagicGraph.mpl_style_parser, "mpl_style") | ||
args = self.get_args(line, parser) | ||
|
||
if args is not None: | ||
style = args.style | ||
mpl_switch_style(style) | ||
|
||
|
||
def register_graph_magics(): | ||
""" | ||
register magics function, can be called from a notebook | ||
""" | ||
from IPython import get_ipython | ||
ip = get_ipython() | ||
ip.register_magics(MagicGraph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#-*- coding: utf-8 -*- | ||
""" | ||
@file | ||
@brief Various functions about matplotlib | ||
""" | ||
|
||
import matplotlib.pyplot | ||
|
||
|
||
def mpl_switch_style(style="ggplot"): | ||
""" | ||
changes the graph style | ||
@param style see `Customizing plots with style sheets <http://matplotlib.org/users/style_sheets.html>`_ | ||
.. versionadded:: 1.1 | ||
""" | ||
matplotlib.pyplot.style.use(style) |