pytexutils.graphs.line_chart

  1def line_chart(data : dict, x_label : str = "xlabel", y_label : str = "ylabel", caption : str = "image_caption", label : str = "image_label", preamble : bool = False) -> str:
  2    '''
  3        Produces LaTeX code to display a bar plot.  
  4
  5        Parameters:  
  6        -----------  
  7        - data : dict
  8            dictionary containing data. Must contains  
  9                - 1) x axis value
 10                - 2) y axis value 
 11                - 3) label
 12                - 4) marker type
 13                - 5) color in rgb format e.g.
 14
 15            ```python 
 16                data = {
 17                    'men' : {
 18                        'x'     : [2012,   2011,   2010,   2009]
 19                        'y'     : [408184, 408348, 414870, 412156]
 20                        'color' : [0.54, 0, 0],
 21                        'marker': 'square',
 22                    },
 23                    'women' : {
 24                        'x'     : [2012,   2011,   2010,   2009]
 25                        'y'     : [388950, 393007, 398449, 395972]
 26                        'color' : [0, 0.50, 0.50],
 27                        'marker': 'diamonds',
 28                    }
 29                }
 30            ```
 31
 32        - caption : str  
 33            string for the caption of LaTeX graph (default: "image_caption")  
 34        - label : str  
 35            string for the label of LaTeX graph (default: "image_label")  
 36        - preamble : bool  
 37            If True the function will return a full LaTeX document, if False the function will return only the table (default: False)  
 38
 39        Returns:  
 40        --------  
 41        - p : str  
 42            LaTeX code to display a pie chart  
 43        
 44        Usage:
 45        ------
 46
 47        ```python
 48        data = {
 49                'men' : {
 50                    'x'     : [2012,   2011,   2010,   2009]
 51                    'y'     : [408184, 408348, 414870, 412156]
 52                    'color' : [0.54, 0, 0],
 53                    'marker': 'square',
 54                },
 55                'women' : {
 56                    'x'     : [2012,   2011,   2010,   2009]
 57                    'y'     : [388950, 393007, 398449, 395972]
 58                    'color' : [0, 0.50, 0.50],
 59                    'marker': 'diamonds',
 60                }
 61            }
 62        latex_line_chart = line_chart(data, caption='My line chart 1', label='line1', preamble=True)
 63        ```
 64
 65        Output:
 66        -------
 67
 68        ```latex
 69        \\documentclass[11pt]{article}
 70        \\usepackage{pgfplotstable}
 71        \\usepackage{pgf-pie}
 72        \\usepackage{graphicx}
 73        \\usepackage{xcolor}
 74        \\pgfplotsset{compat=1.17}
 75        \\begin{document}
 76
 77        \\definecolor{color1}{rgb}{0.54,0,0}
 78        \\definecolor{color2}{rgb}{0,0.5,0.5}
 79
 80        \\pgfplotstableread{
 81                x       men     women
 82                0       408184  388950
 83                1       408348  393007
 84                2       414870  398449
 85                3       412156  395972
 86        }\\datatable
 87
 88        \\begin{figure}[!ht]
 89                \\centering
 90                \\resizebox{\\columnwidth}{!}{
 91                \\begin{tikzpicture}
 92                \\begin{axis}[
 93                ylabel=ylabel,
 94                xlabel=xlabel,
 95                xtick=data,
 96                legend pos=north west,
 97                width=10cm,
 98                height=7cm,
 99                ]
100
101                \\addplot[color = color1, mark=square] table [y index=1] {\\datatable};
102                \\addplot[color = color2, mark=diamonds] table [y index=2] {\\datatable};
103
104                \\legend{men,women}
105
106                \\end{axis}
107                \\end{tikzpicture}}
108                \\caption{My line chart 1}\\label{fig:line1}
109        \\end{figure}
110
111        \end{document}
112        ```
113    '''
114
115    p = ""
116    if preamble:
117        p += "\\documentclass[11pt]{article}\n"
118        p += "\\usepackage{pgfplotstable}\n"
119        p += "\\usepackage{pgf-pie}\n"
120        p += "\\usepackage{graphicx}\n"
121        p += "\\usepackage{xcolor}\n"
122        p += "\\pgfplotsset{compat=1.17}\n"
123        p += "\\begin{document}\n\n"
124
125    # Define colors
126    for i, col in enumerate(data):
127        rgb = data[col]['color']
128        p += "\\definecolor{color"+str(i+1)+"}{rgb}{"+str(rgb[0])+","+str(rgb[1])+","+str(rgb[2])+"}\n"
129    p += "\n"
130
131    # Data in pgf format
132    p += "\\pgfplotstableread{\n"
133    p += "\tx\t" 
134    for col in data: p += f"{col}\t"
135    p += "\n\t"
136
137    for i in range(len(data[col]['x'])):
138        #x = data[col]['x'][i]
139        p += f"{i}\t"
140        for col in data:
141            y = data[col]['y'][i]
142            p += f"{y}\t"
143        p += "\n\t"
144    p = p[:-1]
145    p += "}\\datatable\n\n"
146                 
147
148    # Line Chart
149    p += "\\begin{figure}[!ht]\n"
150    p += "\t\\centering\n"
151    p += "\t\\resizebox{\columnwidth}{!}{\n"
152    p += "\t\\begin{tikzpicture}\n"
153
154    p += "\t\\begin{axis}[\n"
155    p += "\t  ylabel="+str(y_label)+",\n"
156    p += "\t  xlabel="+str(x_label)+",\n"
157    p += "\t  xtick=data,\n"
158    p += "\t  legend pos=north west,\n"
159    p += "\t  width=10cm,\n"
160    p += "\t  height=7cm,\n\t]\n\n"
161    
162    for i, col in enumerate(data):
163        p += "\t\\addplot[color = color"+str(i+1)+", mark="+ data[col]['marker']+"] table [y index="+str(i+1)+"] {\\datatable};\n"
164
165    p += "\n"
166    p += "\t\\legend{"
167
168    for col in data: p += f"{col},"
169    p = p[:-1]
170    p += "}\n\n"
171    p += "\t\\end{axis}\n"
172
173    p += "\t\\end{tikzpicture}}\n"
174    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
175    p += "\\end{figure}\n"
176
177    if preamble:
178        # End document
179        p += "\n\\end{document}\n"
180
181    return p
def line_chart( data: dict, x_label: str = 'xlabel', y_label: str = 'ylabel', caption: str = 'image_caption', label: str = 'image_label', preamble: bool = False) -> str:
  2def line_chart(data : dict, x_label : str = "xlabel", y_label : str = "ylabel", caption : str = "image_caption", label : str = "image_label", preamble : bool = False) -> str:
  3    '''
  4        Produces LaTeX code to display a bar plot.  
  5
  6        Parameters:  
  7        -----------  
  8        - data : dict
  9            dictionary containing data. Must contains  
 10                - 1) x axis value
 11                - 2) y axis value 
 12                - 3) label
 13                - 4) marker type
 14                - 5) color in rgb format e.g.
 15
 16            ```python 
 17                data = {
 18                    'men' : {
 19                        'x'     : [2012,   2011,   2010,   2009]
 20                        'y'     : [408184, 408348, 414870, 412156]
 21                        'color' : [0.54, 0, 0],
 22                        'marker': 'square',
 23                    },
 24                    'women' : {
 25                        'x'     : [2012,   2011,   2010,   2009]
 26                        'y'     : [388950, 393007, 398449, 395972]
 27                        'color' : [0, 0.50, 0.50],
 28                        'marker': 'diamonds',
 29                    }
 30                }
 31            ```
 32
 33        - caption : str  
 34            string for the caption of LaTeX graph (default: "image_caption")  
 35        - label : str  
 36            string for the label of LaTeX graph (default: "image_label")  
 37        - preamble : bool  
 38            If True the function will return a full LaTeX document, if False the function will return only the table (default: False)  
 39
 40        Returns:  
 41        --------  
 42        - p : str  
 43            LaTeX code to display a pie chart  
 44        
 45        Usage:
 46        ------
 47
 48        ```python
 49        data = {
 50                'men' : {
 51                    'x'     : [2012,   2011,   2010,   2009]
 52                    'y'     : [408184, 408348, 414870, 412156]
 53                    'color' : [0.54, 0, 0],
 54                    'marker': 'square',
 55                },
 56                'women' : {
 57                    'x'     : [2012,   2011,   2010,   2009]
 58                    'y'     : [388950, 393007, 398449, 395972]
 59                    'color' : [0, 0.50, 0.50],
 60                    'marker': 'diamonds',
 61                }
 62            }
 63        latex_line_chart = line_chart(data, caption='My line chart 1', label='line1', preamble=True)
 64        ```
 65
 66        Output:
 67        -------
 68
 69        ```latex
 70        \\documentclass[11pt]{article}
 71        \\usepackage{pgfplotstable}
 72        \\usepackage{pgf-pie}
 73        \\usepackage{graphicx}
 74        \\usepackage{xcolor}
 75        \\pgfplotsset{compat=1.17}
 76        \\begin{document}
 77
 78        \\definecolor{color1}{rgb}{0.54,0,0}
 79        \\definecolor{color2}{rgb}{0,0.5,0.5}
 80
 81        \\pgfplotstableread{
 82                x       men     women
 83                0       408184  388950
 84                1       408348  393007
 85                2       414870  398449
 86                3       412156  395972
 87        }\\datatable
 88
 89        \\begin{figure}[!ht]
 90                \\centering
 91                \\resizebox{\\columnwidth}{!}{
 92                \\begin{tikzpicture}
 93                \\begin{axis}[
 94                ylabel=ylabel,
 95                xlabel=xlabel,
 96                xtick=data,
 97                legend pos=north west,
 98                width=10cm,
 99                height=7cm,
100                ]
101
102                \\addplot[color = color1, mark=square] table [y index=1] {\\datatable};
103                \\addplot[color = color2, mark=diamonds] table [y index=2] {\\datatable};
104
105                \\legend{men,women}
106
107                \\end{axis}
108                \\end{tikzpicture}}
109                \\caption{My line chart 1}\\label{fig:line1}
110        \\end{figure}
111
112        \end{document}
113        ```
114    '''
115
116    p = ""
117    if preamble:
118        p += "\\documentclass[11pt]{article}\n"
119        p += "\\usepackage{pgfplotstable}\n"
120        p += "\\usepackage{pgf-pie}\n"
121        p += "\\usepackage{graphicx}\n"
122        p += "\\usepackage{xcolor}\n"
123        p += "\\pgfplotsset{compat=1.17}\n"
124        p += "\\begin{document}\n\n"
125
126    # Define colors
127    for i, col in enumerate(data):
128        rgb = data[col]['color']
129        p += "\\definecolor{color"+str(i+1)+"}{rgb}{"+str(rgb[0])+","+str(rgb[1])+","+str(rgb[2])+"}\n"
130    p += "\n"
131
132    # Data in pgf format
133    p += "\\pgfplotstableread{\n"
134    p += "\tx\t" 
135    for col in data: p += f"{col}\t"
136    p += "\n\t"
137
138    for i in range(len(data[col]['x'])):
139        #x = data[col]['x'][i]
140        p += f"{i}\t"
141        for col in data:
142            y = data[col]['y'][i]
143            p += f"{y}\t"
144        p += "\n\t"
145    p = p[:-1]
146    p += "}\\datatable\n\n"
147                 
148
149    # Line Chart
150    p += "\\begin{figure}[!ht]\n"
151    p += "\t\\centering\n"
152    p += "\t\\resizebox{\columnwidth}{!}{\n"
153    p += "\t\\begin{tikzpicture}\n"
154
155    p += "\t\\begin{axis}[\n"
156    p += "\t  ylabel="+str(y_label)+",\n"
157    p += "\t  xlabel="+str(x_label)+",\n"
158    p += "\t  xtick=data,\n"
159    p += "\t  legend pos=north west,\n"
160    p += "\t  width=10cm,\n"
161    p += "\t  height=7cm,\n\t]\n\n"
162    
163    for i, col in enumerate(data):
164        p += "\t\\addplot[color = color"+str(i+1)+", mark="+ data[col]['marker']+"] table [y index="+str(i+1)+"] {\\datatable};\n"
165
166    p += "\n"
167    p += "\t\\legend{"
168
169    for col in data: p += f"{col},"
170    p = p[:-1]
171    p += "}\n\n"
172    p += "\t\\end{axis}\n"
173
174    p += "\t\\end{tikzpicture}}\n"
175    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
176    p += "\\end{figure}\n"
177
178    if preamble:
179        # End document
180        p += "\n\\end{document}\n"
181
182    return p

Produces LaTeX code to display a bar plot.

Parameters:

  • data : dict dictionary containing data. Must contains
    - 1) x axis value - 2) y axis value - 3) label - 4) marker type - 5) color in rgb format e.g.

        data = {
            'men' : {
                'x'     : [2012,   2011,   2010,   2009]
                'y'     : [408184, 408348, 414870, 412156]
                'color' : [0.54, 0, 0],
                'marker': 'square',
            },
            'women' : {
                'x'     : [2012,   2011,   2010,   2009]
                'y'     : [388950, 393007, 398449, 395972]
                'color' : [0, 0.50, 0.50],
                'marker': 'diamonds',
            }
        }
    
  • 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)

Returns:

  • p : str
    LaTeX code to display a pie chart

Usage:

data = {
        'men' : {
            'x'     : [2012,   2011,   2010,   2009]
            'y'     : [408184, 408348, 414870, 412156]
            'color' : [0.54, 0, 0],
            'marker': 'square',
        },
        'women' : {
            'x'     : [2012,   2011,   2010,   2009]
            'y'     : [388950, 393007, 398449, 395972]
            'color' : [0, 0.50, 0.50],
            'marker': 'diamonds',
        }
    }
latex_line_chart = line_chart(data, caption='My line chart 1', label='line1', preamble=True)

Output:

\documentclass[11pt]{article}
\usepackage{pgfplotstable}
\usepackage{pgf-pie}
\usepackage{graphicx}
\usepackage{xcolor}
\pgfplotsset{compat=1.17}
\begin{document}

\definecolor{color1}{rgb}{0.54,0,0}
\definecolor{color2}{rgb}{0,0.5,0.5}

\pgfplotstableread{
        x       men     women
        0       408184  388950
        1       408348  393007
        2       414870  398449
        3       412156  395972
}\datatable

\begin{figure}[!ht]
        \centering
        \resizebox{\columnwidth}{!}{
        \begin{tikzpicture}
        \begin{axis}[
        ylabel=ylabel,
        xlabel=xlabel,
        xtick=data,
        legend pos=north west,
        width=10cm,
        height=7cm,
        ]

        \addplot[color = color1, mark=square] table [y index=1] {\datatable};
        \addplot[color = color2, mark=diamonds] table [y index=2] {\datatable};

        \legend{men,women}

        \end{axis}
        \end{tikzpicture}}
        \caption{My line chart 1}\label{fig:line1}
\end{figure}

\end{document}