Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Magic commands about graphs 

5 

6.. versionadded:: 1.1 

7""" 

8 

9from IPython.core.magic import magics_class, line_magic 

10from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers 

11 

12# do not import a module which imports matplotlib 

13# if this module is imported, this class is being tested and it affects sphinx 

14# when it generates the documentation 

15from .matplotlib_helper import mpl_switch_style 

16 

17 

18@magics_class 

19class MagicGraph(MagicClassWithHelpers): 

20 

21 """ 

22 Defines magic commands about graphs 

23 

24 .. versionadded:: 1.1 

25 """ 

26 

27 @staticmethod 

28 def mpl_style_parser(): 

29 """ 

30 defines the way to parse the magic command ``%mpl_style`` 

31 """ 

32 parser = MagicCommandParser( 

33 description='changes matplotlib style', prog="mpl_style") 

34 parser.add_argument( 

35 'style', 

36 type=str, 

37 help='style, ggplot for exemple', 

38 default="ggplot") 

39 return parser 

40 

41 @line_magic 

42 def mpl_style(self, line): 

43 """ 

44 defines ``%mpl_style`` 

45 which changes the style of matplotlib graphs, example: ``%mpl_style ggplot`` 

46 

47 .. nbref:: 

48 :title: mpl_style 

49 

50 This magic just does:: 

51 

52 import matplotlib.pyplot as plt 

53 plt.style.use('ggplot') 

54 

55 It should take place at the beginning of the notebook. 

56 """ 

57 parser = self.get_parser(MagicGraph.mpl_style_parser, "mpl_style") 

58 args = self.get_args(line, parser) 

59 

60 if args is not None: 

61 style = args.style 

62 mpl_switch_style(style) 

63 

64 

65def register_graph_magics(ip=None): # pragma: no cover 

66 """ 

67 register magics function, can be called from a notebook 

68 

69 @param ip from ``get_ipython()`` 

70 """ 

71 if ip is None: 

72 from IPython import get_ipython 

73 ip = get_ipython() 

74 ip.register_magics(MagicGraph)