pytexutils.graphs.bar_chart

  1import numpy as np
  2
  3def bar_chart(data : dict, x_label : str = "xlabel", y_label : str = "ylabel", caption : str = "image_caption", label : str = "image_label", preamble : bool = False) -> str:
  4    '''
  5        Produces LaTeX code to display a bar plot.  
  6
  7        Parameters:  
  8        -----------  
  9        - data : dict
 10            dictionary containing data. Must contains  
 11                - 1) x axis value
 12                - 2) y axis value 
 13                - 3) label
 14                - 4) 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                    },
 23                    'women' : {
 24                        'x'     : [2012,   2011,   2010,   2009]
 25                        'y'     : [388950, 393007, 398449, 395972]
 26                        'color' : [0, 0.50, 0.50]
 27                    }
 28                }
 29            ```
 30
 31        - caption : str  
 32            string for the caption of LaTeX graph (default: "image_caption")  
 33        - label : str  
 34            string for the label of LaTeX graph (default: "image_label")  
 35        - preamble : bool  
 36            If True the function will return a full LaTeX document, if False the function will return only the table (default: False)  
 37
 38        Returns:  
 39        --------  
 40        - p : str  
 41            LaTeX code to display a pie chart  
 42        
 43        Usage:
 44        ------
 45
 46        ```python
 47        data = {
 48        'men' : {
 49                'x'     : [2012,   2011,   2010,   2009],
 50                'y'     : [408184, 408348, 414870, 412156],
 51                'color' : [0.54, 0, 0],
 52            },
 53        'women' : {
 54                'x'     : [2012,   2011,   2010,   2009],
 55                'y'     : [388950, 393007, 398449, 395972],
 56                'color' : [0, 0.50, 0.50],
 57            }
 58        }
 59        latex_bar_plot = bar_chart(data, caption='My pie chart 1', label='pie1', preamble=True)
 60        ```
 61
 62        Output:
 63        -------
 64
 65        ```latex
 66        \\documentclass[11pt]{article}
 67        \\usepackage{pgfplotstable}
 68        \\pgfplotsset{compat=1.17}
 69        \\usepackage{pgf-pie}
 70        \\usepackage{graphicx}
 71        \\usepackage{xcolor}
 72        \\begin{document}
 73
 74        \\definecolor{color1}{rgb}{0.54,0,0}
 75        \\definecolor{color2}{rgb}{0,0.5,0.5}
 76
 77        \\pgfplotstableread{
 78                x       men     women
 79                0       408184  388950
 80                1       408348  393007
 81                2       414870  398449
 82                3       412156  395972
 83        }\\datatable
 84
 85        \\begin{figure}[!ht]
 86                \\centering
 87                \\resizebox{\\columnwidth}{!}{    
 88                \\begin{tikzpicture}
 89                \\begin{axis}[
 90                ylabel=ylabel,
 91                xlabel=xlabel,
 92                xtick=data,
 93                xticklabels={2012,2011,2010,2009},
 94                legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},
 95                enlarge x limits=0.0005,
 96                ybar interval=0.7,
 97                width=10cm,
 98                height=7cm,
 99                ]
100
101                \\addplot[style = {fill=color1}] table [y index=1] {\\datatable};
102                \\addplot[style = {fill=color2}] table [y index=2] {\\datatable};
103
104                \\legend{men,women}
105
106                \\end{axis}
107                \\end{tikzpicture}}
108                \\caption{My bar chart 1}\\label{fig:bar1}
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
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    
146    p += f"{i+1}\t"
147    for col in data:
148        y = data[col]['y'][i]
149        p += f"{y}\t"
150    p += "\n\t"
151
152
153    p = p[:-1]
154    p += "}\\datatable\n\n"
155                 
156
157    # Bar Chart
158    p += "\\begin{figure}[!ht]\n"
159    p += "\t\\centering\n"
160    p += "\t\\resizebox{\columnwidth}{!}{\n"
161    p += "\t\\begin{tikzpicture}\n"
162
163    p += "\t\\begin{axis}[\n"
164    p += "\t  ylabel="+str(y_label)+",\n"
165    p += "\t  xlabel="+str(x_label)+",\n"
166    p += "\t  xtick=data,\n"
167    p += "\t  enlarge x limits=0.0005,\n"
168    p += "\t  xticklabels={"
169
170    for x in data[col]['x']:
171        p += f"{x},"
172    
173    p = p[:-1]
174    p += "},\n"
175    p += "\t  legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},\n"
176    p += "\t  ybar interval=0.7,\n"
177
178    width  = 6 + int(2 * np.sqrt(3*len(data[col]['x'])))
179    height = 3 + int(width*0.2)
180
181    p += "\t  width="+str(width)+"cm,\n"
182    p += "\t  height="+str(height)+"cm,\n\t]\n\n"
183    
184    for i,_ in enumerate(data):
185        p += "\t\\addplot[style = {fill=color"+str(i+1)+"}] table [y index="+str(i+1)+"] {\\datatable};\n"
186
187    p += "\n"
188    p += "\t\\legend{"
189
190    for col in data: p += f"{col},"
191    p = p[:-1]
192    p += "}\n\n"
193    p += "\t\\end{axis}\n"
194
195    p += "\t\\end{tikzpicture}}\n"
196    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
197    p += "\\end{figure}\n"
198
199    if preamble:
200        # End document
201        p += "\n\\end{document}\n"
202
203    return p
def bar_chart( data: dict, x_label: str = 'xlabel', y_label: str = 'ylabel', caption: str = 'image_caption', label: str = 'image_label', preamble: bool = False) -> str:
  4def bar_chart(data : dict, x_label : str = "xlabel", y_label : str = "ylabel", caption : str = "image_caption", label : str = "image_label", preamble : bool = False) -> str:
  5    '''
  6        Produces LaTeX code to display a bar plot.  
  7
  8        Parameters:  
  9        -----------  
 10        - data : dict
 11            dictionary containing data. Must contains  
 12                - 1) x axis value
 13                - 2) y axis value 
 14                - 3) label
 15                - 4) color in rgb format e.g.
 16
 17            ```python 
 18                data = {
 19                    'men' : {
 20                        'x'     : [2012,   2011,   2010,   2009]
 21                        'y'     : [408184, 408348, 414870, 412156]
 22                        'color' : [0.54, 0, 0]
 23                    },
 24                    'women' : {
 25                        'x'     : [2012,   2011,   2010,   2009]
 26                        'y'     : [388950, 393007, 398449, 395972]
 27                        'color' : [0, 0.50, 0.50]
 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            },
 54        'women' : {
 55                'x'     : [2012,   2011,   2010,   2009],
 56                'y'     : [388950, 393007, 398449, 395972],
 57                'color' : [0, 0.50, 0.50],
 58            }
 59        }
 60        latex_bar_plot = bar_chart(data, caption='My pie chart 1', label='pie1', preamble=True)
 61        ```
 62
 63        Output:
 64        -------
 65
 66        ```latex
 67        \\documentclass[11pt]{article}
 68        \\usepackage{pgfplotstable}
 69        \\pgfplotsset{compat=1.17}
 70        \\usepackage{pgf-pie}
 71        \\usepackage{graphicx}
 72        \\usepackage{xcolor}
 73        \\begin{document}
 74
 75        \\definecolor{color1}{rgb}{0.54,0,0}
 76        \\definecolor{color2}{rgb}{0,0.5,0.5}
 77
 78        \\pgfplotstableread{
 79                x       men     women
 80                0       408184  388950
 81                1       408348  393007
 82                2       414870  398449
 83                3       412156  395972
 84        }\\datatable
 85
 86        \\begin{figure}[!ht]
 87                \\centering
 88                \\resizebox{\\columnwidth}{!}{    
 89                \\begin{tikzpicture}
 90                \\begin{axis}[
 91                ylabel=ylabel,
 92                xlabel=xlabel,
 93                xtick=data,
 94                xticklabels={2012,2011,2010,2009},
 95                legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},
 96                enlarge x limits=0.0005,
 97                ybar interval=0.7,
 98                width=10cm,
 99                height=7cm,
100                ]
101
102                \\addplot[style = {fill=color1}] table [y index=1] {\\datatable};
103                \\addplot[style = {fill=color2}] table [y index=2] {\\datatable};
104
105                \\legend{men,women}
106
107                \\end{axis}
108                \\end{tikzpicture}}
109                \\caption{My bar chart 1}\\label{fig:bar1}
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
128    for i, col in enumerate(data):
129        rgb = data[col]['color']
130        p += "\\definecolor{color"+str(i+1)+"}{rgb}{"+str(rgb[0])+","+str(rgb[1])+","+str(rgb[2])+"}\n"
131    p += "\n"
132
133    # Data in pgf format
134    p += "\\pgfplotstableread{\n"
135    p += "\tx\t" 
136    for col in data: p += f"{col}\t"
137    p += "\n\t"
138
139    for i in range(len(data[col]['x'])):
140        #x = data[col]['x'][i]
141        p += f"{i}\t"
142        for col in data:
143            y = data[col]['y'][i]
144            p += f"{y}\t"
145        p += "\n\t"
146    
147    p += f"{i+1}\t"
148    for col in data:
149        y = data[col]['y'][i]
150        p += f"{y}\t"
151    p += "\n\t"
152
153
154    p = p[:-1]
155    p += "}\\datatable\n\n"
156                 
157
158    # Bar Chart
159    p += "\\begin{figure}[!ht]\n"
160    p += "\t\\centering\n"
161    p += "\t\\resizebox{\columnwidth}{!}{\n"
162    p += "\t\\begin{tikzpicture}\n"
163
164    p += "\t\\begin{axis}[\n"
165    p += "\t  ylabel="+str(y_label)+",\n"
166    p += "\t  xlabel="+str(x_label)+",\n"
167    p += "\t  xtick=data,\n"
168    p += "\t  enlarge x limits=0.0005,\n"
169    p += "\t  xticklabels={"
170
171    for x in data[col]['x']:
172        p += f"{x},"
173    
174    p = p[:-1]
175    p += "},\n"
176    p += "\t  legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},\n"
177    p += "\t  ybar interval=0.7,\n"
178
179    width  = 6 + int(2 * np.sqrt(3*len(data[col]['x'])))
180    height = 3 + int(width*0.2)
181
182    p += "\t  width="+str(width)+"cm,\n"
183    p += "\t  height="+str(height)+"cm,\n\t]\n\n"
184    
185    for i,_ in enumerate(data):
186        p += "\t\\addplot[style = {fill=color"+str(i+1)+"}] table [y index="+str(i+1)+"] {\\datatable};\n"
187
188    p += "\n"
189    p += "\t\\legend{"
190
191    for col in data: p += f"{col},"
192    p = p[:-1]
193    p += "}\n\n"
194    p += "\t\\end{axis}\n"
195
196    p += "\t\\end{tikzpicture}}\n"
197    p += "\t\\caption{"+str(caption)+"}\\label{fig:"+label+"}\n"
198    p += "\\end{figure}\n"
199
200    if preamble:
201        # End document
202        p += "\n\\end{document}\n"
203
204    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) color in rgb format e.g.

        data = {
            'men' : {
                'x'     : [2012,   2011,   2010,   2009]
                'y'     : [408184, 408348, 414870, 412156]
                'color' : [0.54, 0, 0]
            },
            'women' : {
                'x'     : [2012,   2011,   2010,   2009]
                'y'     : [388950, 393007, 398449, 395972]
                'color' : [0, 0.50, 0.50]
            }
        }
    
  • 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],
    },
'women' : {
        'x'     : [2012,   2011,   2010,   2009],
        'y'     : [388950, 393007, 398449, 395972],
        'color' : [0, 0.50, 0.50],
    }
}
latex_bar_plot = bar_chart(data, caption='My pie chart 1', label='pie1', preamble=True)

Output:

\documentclass[11pt]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usepackage{pgf-pie}
\usepackage{graphicx}
\usepackage{xcolor}
\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,
        xticklabels={2012,2011,2010,2009},
        legend style={at={(0.5,-0.2)}, anchor=north,legend columns=-1},
        enlarge x limits=0.0005,
        ybar interval=0.7,
        width=10cm,
        height=7cm,
        ]

        \addplot[style = {fill=color1}] table [y index=1] {\datatable};
        \addplot[style = {fill=color2}] table [y index=2] {\datatable};

        \legend{men,women}

        \end{axis}
        \end{tikzpicture}}
        \caption{My bar chart 1}\label{fig:bar1}
\end{figure}

\end{document}