Skip to content
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ The `plotLaTeX` package is a recent project to make exporting Python data to a L
<img src="images/example_box.png" alt="Fig3" width="1000px">
</p>

**[Barplot](examples/BarPlot.ipynb)**

<p align="center">
<img src="images/example_sbar_plot.png" alt="Fig4" width="1000px">
</p>
<p align="center">
<img src="images/example_mbar_plot.png" alt="Fig5" width="1000px">
</p>
<p align="center">
<img src="images/example_stbar_plot.png" alt="Fig6" width="1000px">
</p>

**TBD**

- 3D scatter


## Installation

Expand Down
524 changes: 524 additions & 0 deletions examples/BarPlot.ipynb

Large diffs are not rendered by default.

138 changes: 131 additions & 7 deletions examples/LaTeX_layout.tex
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ \subsection{Boxplot}
median=128.73043708220285,
upper quartile=134.05952052012063,
lower quartile=123.99094329503548,
upper whisker=103.80254895910255,
lower whisker=148.52278184508938
upper whisker=148.52278184508938,
lower whisker=110.12431085399108
},
] coordinates {};
\addplot[
Expand All @@ -126,8 +126,8 @@ \subsection{Boxplot}
median=81.68214339893669,
upper quartile=90.76340895325481,
lower quartile=63.886789536848525,
upper whisker=41.62457569401917,
lower whisker=134.40338333179238
upper whisker=129.26484224970574,
lower whisker=41.62457569401917
},
] coordinates {};
\addplot[
Expand All @@ -137,8 +137,8 @@ \subsection{Boxplot}
median=122.93087226956357,
upper quartile=141.13312343712607,
lower quartile=100.33669378132858,
upper whisker=22.761979797927822,
lower whisker=235.58194471964163
upper whisker=189.43975700020525,
lower whisker=59.24572240027179
},
] coordinates {};
\end{axis}
Expand All @@ -147,4 +147,128 @@ \subsection{Boxplot}
\label{fig:Caption of the boxplot.}
\end{figure}

\end{document}

\subsection{Bar chart}

\subsubsection{Default}

\begin{figure}[ht]
\centering
\tikzstyle{every node}=[font=\footnotesize]
\begin{tikzpicture}
\begin{axis}[
ylabel=Sales,
xlabel=Fruits,
xticklabels={Apples,Bananas,Cherries},
ybar,
bar width=0.5cm,
xtick=data,
width=7.5cm,
height=3cm,
at={(0cm,0cm)},
scale only axis,
axis background/.style={fill=white},
grid=both,
legend columns = 1,
legend style={at={(1,1.05)}, legend cell align=left, align=left, draw=white!15!black, mark options={draw=none}, anchor=south east},
]

\addplot[fill=black!70!black,opacity=0.7]
coordinates {(1,345) (2,234) (3,512)};

\end{axis}
\end{tikzpicture}
\caption{Caption of the default barchart.}
\label{fig:Caption of the default barchart.}
\end{figure}


\newpage
\subsubsection{Multiple}

\begin{figure}[ht]
\centering
\tikzstyle{every node}=[font=\footnotesize]
\begin{tikzpicture}
\begin{axis}[
ylabel=Sales,
xlabel=Fruits,
xtick={ 0, 1, 2 },
xticklabels={ Apples, Bananas, Cherries },
ybar,
bar width=0.3cm,
xtick=data,
width=7.5cm,
height=3cm,
at={(0cm,0cm)},
scale only axis,
axis background/.style={fill=white},
grid=both,
legend columns = 3,
legend style={at={(1,1.05)}, legend cell align=left, align=left, draw=white!15!black, mark options={draw=none}, anchor=south east},
]
\addplot[fill=black!70!black,opacity=0.7]
coordinates {(1,345) (2,234) (3,512)};
\addlegendentry{2022};

\addplot[fill=blue!70!black,opacity=0.7]
coordinates {(1,420) (2,304) (3,412)};
\addlegendentry{2023};

\addplot[fill=brown!70!black,opacity=0.7]
coordinates {(1,501) (2,324) (3,242)};
\addlegendentry{2024};


\end{axis}
\end{tikzpicture}
\caption{Caption of the barchart.}
\label{fig:Caption of the barchart.}
\end{figure}


\subsubsection{Stacked}

\begin{figure}[ht]
\centering
\tikzstyle{every node}=[font=\footnotesize]
\begin{tikzpicture}
\begin{axis}[
ylabel=Sales,
xlabel=Fruits,
xtick={ 0, 1, 2 },
xticklabels={ Apples, Bananas, Cherries },
ybar stacked,
bar width=0.3cm,
xtick=data,
width=7.5cm,
height=3cm,
at={(0cm,0cm)},
scale only axis,
axis background/.style={fill=white},
grid=both,
legend columns = 3,
legend style={at={(1,1.05)}, legend cell align=left, align=left, draw=white!15!black, mark options={draw=none}, anchor=south east},
]
\addplot[fill=black!70!black,opacity=0.7]
coordinates {(1,345) (2,234) (3,512)};
\addlegendentry{2022};

\addplot[fill=blue!70!black,opacity=0.7]
coordinates {(1,420) (2,304) (3,412)};
\addlegendentry{2023};

\addplot[fill=brown!70!black,opacity=0.7]
coordinates {(1,501) (2,324) (3,242)};
\addlegendentry{2024};


\end{axis}
\end{tikzpicture}
\caption{Caption of the barchart.}
\label{fig:Caption of the barchart.}
\end{figure}



\end{document}
Binary file added images/example_mbar_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/example_sbar_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/example_stbar_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions plotLaTeX/LaTeX_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
latex_colors = [
"black",
"blue",
"brown",
"cyan",
"darkgray",
"gray",
"green",
"lightgray",
"lime",
"magenta",
"olive",
"orange",
"pink",
"purple",
"red",
"teal",
"violet",
"white",
"yellow",
]
3 changes: 2 additions & 1 deletion plotLaTeX/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .line_plot import LinePlot
from .hist_plot import HistPlot
from .box_plot import BoxPlot
from .bar_plot import Barplot, MultipleBars

__all__ = ["LaTeXplot", "HistPlot", "BoxPlot"]
__all__ = ["LaTeXplot", "HistPlot", "BoxPlot", "Barplot", "MultipleBars"]
127 changes: 127 additions & 0 deletions plotLaTeX/bar_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import numpy as np
from .LaTeX_colors import latex_colors


class Barplot:
def __init__(self, dataframe, mode="default"):
self.df = dataframe
self.mode = mode
self.xlabel = "x-label"
self.ylabel = "y-label"
self.x_labels = list(self.df.iloc[:, 0])
self.data_info()

def data_info(self):
print(self.df)

def add_axis_labels(self, xlabel, ylabel):
self.xlabel = xlabel
self.ylabel = ylabel

def set_mode(self, mode):
"""
mode=
- default
- multiple
- stacked
"""
self.bar_mode = mode

def LaTeXcode(self, imports=True, caption="Caption of the default barchart."):
if imports:
print("\tDon´t forget to import the packages:\n")
print(r"\usepackage{graphicx}")
print(r"\usepackage{tikz,pgfplots}")
print(r"\usepgfplotslibrary{statistics}")
print("\n*\t*********\n")

print(r"\begin{figure}[ht]")
print(r" \centering")
print(r" \tikzstyle{every node}=[font=\footnotesize]")
print(r" \begin{tikzpicture}")
print(r" \begin{axis}[")
print(f" ylabel={self.ylabel},")
print(f" xlabel={self.xlabel},")
print(f" xticklabels={{{','.join(self.x_labels)}}},")
print(r" ybar,")
print(r" bar width=0.5cm,")
print(r" xtick=data,")
print(r" width=7.5cm,")
print(r" height=3cm,")
print(r" at={(0cm,0cm)},")
print(r" scale only axis,")
print(r" axis background/.style={fill=white},")
print(r" grid=both,")
print(r" legend columns = 1,")
print(
r" legend style={at={(1,1.05)}, legend cell align=left, align=left, draw=white!15!black, mark options={draw=none}, anchor=south east},"
)
print(r" ]")
print()
print(r" \addplot[fill=black!70!black,opacity=0.7] ")
coordinates = " ".join(
f"({i+1},{row['sales']})" for i, row in self.df.iterrows()
)
print(f" coordinates {{{coordinates}}};")
# print(r" \addlegendentry{2015};")
print()
print(r" \end{axis}")
print(r" \end{tikzpicture}")
print(r" \caption{" + caption + "}")
print(r" \label{fig:" + caption + "}")
print(r"\end{figure}")


class MultipleBars:
def __init__(self, categories: list, bars: dict, mode="multiple"):
self.categories = categories
self.xlabel = "x-label"
self.ylabel = "y-label"
self.xticks = np.arange(len(categories))
self.bars = bars
self.mode = mode

def add_axis_labels(self, xlabel, ylabel):
self.xlabel = xlabel
self.ylabel = ylabel

def LaTeXcode(self, imports=True, caption=f"Caption of the barchart."):
print(r"\begin{figure}[ht]")
print(r"\centering")
print(r"\tikzstyle{every node}=[font=\footnotesize]")
print(r"\begin{tikzpicture}")
print(r" \begin{axis}[")
print(f" ylabel={self.ylabel},")
print(f" xlabel={self.xlabel},")
print(f" xtick={{ {', '.join(map(str, self.xticks))} }},")
print(f" xticklabels={{ {', '.join(self.categories)} }},")
if self.mode == "multiple":
print(r" ybar,")
elif self.mode == "stacked":
print(r" ybar stacked,")
print(r" bar width=0.3cm,")
print(r" xtick=data,")
print(r" width=7.5cm,")
print(r" height=3cm,")
print(r" at={(0cm,0cm)},")
print(r" scale only axis,")
print(r" axis background/.style={fill=white},")
print(r" grid=both,")
print(f" legend columns = {len(self.bars)},")
print(
r" legend style={at={(1,1.05)}, legend cell align=left, align=left, draw=white!15!black, mark options={draw=none}, anchor=south east},"
)
print(r" ]")

for i, (x_idx, values) in enumerate(self.bars.items()):
color = latex_colors[i]
print(f" \\addplot[fill={color}!70!black,opacity=0.7]")
coordinates = " ".join(f"({i+1},{value})" for i, value in enumerate(values))
print(f" coordinates {{{coordinates}}};")
print(f" \\addlegendentry{{{x_idx}}};\n")
print()
print(r" \end{axis}")
print(r"\end{tikzpicture}")
print(r" \caption{" + caption + "}")
print(r" \label{fig:" + caption + "}")
print(r"\end{figure}")
Loading