pytexutils.graphs.pie_chart

 1def pie_chart(data : dict, caption : str = "image_caption", label : str = "image_label", preamble : bool = False, ctype : str = 'classic') -> str:
 2    '''
 3        Produces LaTeX code to display a pie chart.  
 4
 5        Parameters:  
 6        -----------  
 7        - data : dict
 8            dictionary containing data, e.g.
 9
10            ```python 
11                # Morning sales
12                data = {'coffee':38, 'tea':36, 'croissant':11, 'patries':10, 'juice':5s}
13            ```
14        - caption : str  
15            string for the caption of LaTeX graph (default: "image_caption")  
16        - label : str  
17            string for the label of LaTeX graph (default: "image_label")  
18        - preamble : bool  
19            If True the function will return a full LaTeX document, if False the function will return only the table (default: False)  
20        - ctype : str  
21            Type of pie chart, can be classic, polar, square or cloud
22
23        Returns:  
24        --------  
25        - p : str  
26            LaTeX code to display a pie chart  
27        
28        Usage:
29        ------
30
31        ```python
32        data = {'coffee':38, 'tea':36, 'croissant':11, 'pastries':10, 'juice':5}
33        latex_pie_chart = make_chart(data, caption='My pie chart 1', label='pie1', preamble=True, ctype="classic")
34        ```
35
36        Output:
37        -------
38
39        ```latex
40        \\documentclass[11pt]{article}
41        \\usepackage{pgf-pie}
42        \\usepackage{graphicx}
43        \\begin{document}
44
45        \\begin{figure}[!ht]
46                \\centering
47                    \\begin{tikzpicture}
48                    \\pie{38/coffee, 36/tea, 11/croissant, 10/pastries, 5/juice}
49                    \\end{tikzpicture}
50                \\caption{My pie chart 1}\\label{fig:pie1}
51        \\end{figure}
52
53        \\end{document}
54        ```
55    '''
56
57    if ctype not in ['classic', 'polar', 'square', 'cloud']:
58        raise Exception("Error Message: pie chart, can be classic, polar, square or cloud.")
59    
60    p = ""
61    # LaTeX preamble
62    if preamble:
63        p += "\\documentclass[11pt]{article}\n"
64        p += "\\usepackage{pgf-pie}\n"
65        p += "\\usepackage{graphicx}\n"
66        p += "\\begin{document}\n\n"
67        
68
69    # Pie Chart
70    p += "\\begin{figure}[!ht]\n"
71    p += "\t\\centering\n"
72    p += "\t\t\\begin{tikzpicture}\n"
73
74    if ctype == 'classic': p += "\t\t\\pie{"
75    else: p += "\t\t\\pie["+ctype+"]{"
76
77    for key, value in data.items():
78        p += str(value) + "/" + str(key) + ","
79    p = p[:-1]
80    p += "}\n"
81
82    p += "\t\t\\end{tikzpicture}\n"
83    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
84    p += "\\end{figure}\n"
85
86    if preamble:
87        # End document
88        p += "\n\\end{document}\n"
89
90    return p
def pie_chart( data: dict, caption: str = 'image_caption', label: str = 'image_label', preamble: bool = False, ctype: str = 'classic') -> str:
 2def pie_chart(data : dict, caption : str = "image_caption", label : str = "image_label", preamble : bool = False, ctype : str = 'classic') -> str:
 3    '''
 4        Produces LaTeX code to display a pie chart.  
 5
 6        Parameters:  
 7        -----------  
 8        - data : dict
 9            dictionary containing data, e.g.
10
11            ```python 
12                # Morning sales
13                data = {'coffee':38, 'tea':36, 'croissant':11, 'patries':10, 'juice':5s}
14            ```
15        - caption : str  
16            string for the caption of LaTeX graph (default: "image_caption")  
17        - label : str  
18            string for the label of LaTeX graph (default: "image_label")  
19        - preamble : bool  
20            If True the function will return a full LaTeX document, if False the function will return only the table (default: False)  
21        - ctype : str  
22            Type of pie chart, can be classic, polar, square or cloud
23
24        Returns:  
25        --------  
26        - p : str  
27            LaTeX code to display a pie chart  
28        
29        Usage:
30        ------
31
32        ```python
33        data = {'coffee':38, 'tea':36, 'croissant':11, 'pastries':10, 'juice':5}
34        latex_pie_chart = make_chart(data, caption='My pie chart 1', label='pie1', preamble=True, ctype="classic")
35        ```
36
37        Output:
38        -------
39
40        ```latex
41        \\documentclass[11pt]{article}
42        \\usepackage{pgf-pie}
43        \\usepackage{graphicx}
44        \\begin{document}
45
46        \\begin{figure}[!ht]
47                \\centering
48                    \\begin{tikzpicture}
49                    \\pie{38/coffee, 36/tea, 11/croissant, 10/pastries, 5/juice}
50                    \\end{tikzpicture}
51                \\caption{My pie chart 1}\\label{fig:pie1}
52        \\end{figure}
53
54        \\end{document}
55        ```
56    '''
57
58    if ctype not in ['classic', 'polar', 'square', 'cloud']:
59        raise Exception("Error Message: pie chart, can be classic, polar, square or cloud.")
60    
61    p = ""
62    # LaTeX preamble
63    if preamble:
64        p += "\\documentclass[11pt]{article}\n"
65        p += "\\usepackage{pgf-pie}\n"
66        p += "\\usepackage{graphicx}\n"
67        p += "\\begin{document}\n\n"
68        
69
70    # Pie Chart
71    p += "\\begin{figure}[!ht]\n"
72    p += "\t\\centering\n"
73    p += "\t\t\\begin{tikzpicture}\n"
74
75    if ctype == 'classic': p += "\t\t\\pie{"
76    else: p += "\t\t\\pie["+ctype+"]{"
77
78    for key, value in data.items():
79        p += str(value) + "/" + str(key) + ","
80    p = p[:-1]
81    p += "}\n"
82
83    p += "\t\t\\end{tikzpicture}\n"
84    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
85    p += "\\end{figure}\n"
86
87    if preamble:
88        # End document
89        p += "\n\\end{document}\n"
90
91    return p

Produces LaTeX code to display a pie chart.

Parameters:

  • data : dict dictionary containing data, e.g.

        # Morning sales
        data = {'coffee':38, 'tea':36, 'croissant':11, 'patries':10, 'juice':5s}
    
  • caption : str
    string for the caption of LaTeX graph (default: "image_caption")

  • label : str
    string for the label of LaTeX graph (default: "image_label")
  • preamble : bool
    If True the function will return a full LaTeX document, if False the function will return only the table (default: False)
  • ctype : str
    Type of pie chart, can be classic, polar, square or cloud

Returns:

  • p : str
    LaTeX code to display a pie chart

Usage:

data = {'coffee':38, 'tea':36, 'croissant':11, 'pastries':10, 'juice':5}
latex_pie_chart = make_chart(data, caption='My pie chart 1', label='pie1', preamble=True, ctype="classic")

Output:

\documentclass[11pt]{article}
\usepackage{pgf-pie}
\usepackage{graphicx}
\begin{document}

\begin{figure}[!ht]
        \centering
            \begin{tikzpicture}
            \pie{38/coffee, 36/tea, 11/croissant, 10/pastries, 5/juice}
            \end{tikzpicture}
        \caption{My pie chart 1}\label{fig:pie1}
\end{figure}

\end{document}