How to Create Bar Charts using Latex PGFPlots
Bar plots using Latex PGFPlot
Previously, I used to include plots as a figure generated using python matplotlib.
However, recently, while working on a paper, my supervisor wanted us to include plots in the paper using latex pgfplot. It is convenient to use these plots if we need to change conference/journal templates without worrying about image quality/configurations. In this post, I have added latex codes to create bar plots using latex pgfplot. Regardless to say, codes have been included for note purpose and collected from our paper and online resources.
Find outputs in this Overleaf Document.
A simple Bar Plot
Here, we plot a simple bar chart having data from a CSV file.
Prerequisite Configuration
The CSV file had 100 entries. But, I included first 20 rows. For that I needed to include an extra rule as follows:
\pgfplotsset{select coords between index/.style 2 args={
x filter/.code={
\ifnum\coordindex<#1\def\pgfmathresult{}\fi
\ifnum\coordindex>#2\def\pgfmathresult{}\fi
}
}}
Bar Plot Code
\begin{figure}
\centering
\pgfplotstableread[col sep=comma,]{100SalesRecords.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=.1cm,
width=\textwidth,
height=10cm,
ymin = 0,
xtick=data,
xticklabels from table={\datatable}{Country},
x tick label style={font=\normalsize, rotate=90, anchor=east},
ylabel={Units Sold in Number}]
\addplot [select coords between index={1}{20}] table [x expr=\coordindex, y={Units Sold}]{\datatable};
\end{axis}
\end{tikzpicture}
\caption{Units sold by Different countries}
\label{fig:my_label}
\end{figure}
Group Plotting
Prerequisites
The following configuration has been collected from our recent paper.
\pgfplotsset{
SmallBarPlot/.style={
font=\footnotesize,
ybar,
width=\linewidth,
ymin=0,
xtick=data,
xticklabel style={text width=1.5cm, rotate=90, align=center}
},
BlueBars/.style={
fill=blue!20, bar width=0.25
},
RedBars/.style={
fill=red!20, bar width=0.25
}
}
\DeclareRobustCommand\legendbox[1]{(\textcolor{#1}{#1}~\begin{tikzpicture}[x=0.2cm, y=0.2cm] \draw [color=black, fill=#1!20] (0,0) -- (0,1) -- (0.6,1) -- (0.6,0) -- (0, 0); \end{tikzpicture})}
Simple Group Bar Plotting
\begin{figure}[ht!]
\pgfplotstableread[col sep = comma]{100SalesRecords.csv}\table
\centering
\begin{tikzpicture}
\begin{axis}[
SmallBarPlot,
xticklabels from table={\table}{Country},
ylabel=Value in Number,
xlabel=Countries
]
\addplot [BlueBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Units Sold] {\table};
\addlegendentry{First}
\addplot [RedBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Unit Price] {\table};
\addlegendentry{Second}
\legend{}
\end{axis}
\end{tikzpicture}
\caption{The first legend \legendbox{blue} and the second one \legendbox{red}}
\label{fig:multiBar}
\end{figure}
Subplots with Group Plotting
Prerequisite
The previous configurations.
Subplotting with group bars
The second subplot includes logarithmic scale.
\begin{figure}[ht!]
\pgfplotstableread[col sep = comma]{100SalesRecords.csv}\table
\centering
\begin{subfigure}[b]{.5\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[
SmallBarPlot,
xticklabels from table={\table}{Country},
ylabel=Value in Number,
xlabel=Countries
]
\addplot [BlueBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Units Sold] {\table};
\addlegendentry{First}
\addplot [RedBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Unit Price] {\table};
\addlegendentry{Second}
\legend{}
\end{axis}
\end{tikzpicture}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[
SmallBarPlot,
xticklabels from table={\table}{Country},
ylabel= ylabel=Value in Number,
xlabel=Countries,
ymode=log,
log basis y={10}
]
\addplot [BlueBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Total Revenue] {\table};
\addlegendentry{First}
\addplot [RedBars] [select coords between index={1}{5}] table [x expr=\coordindex, y=Total Cost] {\table};
\addlegendentry{Second}
\legend{}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\caption{The first legend \legendbox{blue} and the second one \legendbox{red}.}
\label{fig:subBarPlot}
\end{figure}
Happy Documentation and writing papers.
Related Posts
You can find a comprehensive list of Latex resources in the following post:
If you are a new Latex user, check out this post: 20 Most Common Mistakes Made by New Latex Users
You can find all Latex oriented posts of mine in: https://shantoroy.com/categories/#latex
- How to write an algorithm in Latex
- How to add subfigure in Latex
- Add Copyright Notice and Conference Name in IEEE Conference Template
- Preparing Tables for Publication and Documentation in Latex
- How to Write Matrix with Row/Column Labels in Latex
- How to Collaboratively Write a Paper using Overleaf Latex Platform
- Itemize, Enumerate, and To-do-list in Latex
- Latex Table for Survey in IEEE two-column format
- Line Plotting using Latex PGFPlots
- How to add Codes in Latex:
listingspackage for code documentation - Bibliography management with Bibtex in Latex
- Creating Multiple Line plots from CSV file using Latex Tikz and PGFPlot
- How to Draw a Literature Survey Taxonomy Tree in Latex
- How to Convert Python Matplotlib Plots to Latex Plots (Easiest Way) for Academic Papers
Promotions and Referrals (US Residents Only)
- Chime: Open a Checking account at Chime using my referral link and get $100 after your employer deposit paycheck of minimum $200 within the first 45 days.
- Rakuten: Get $30 after you spend $30 at Rakuten select stores after you use my referral link to open an account.
-
Chase Freedom Credit Card: Earn $200 cash back with Chase Freedom Unlimited or Chase Freedom Flex credit card. I can be rewarded if you apply using my referral link and are approved for the card.
- Chase Checking Account: Get $200 when you open a checking account using my referral link after your first salary is deposited.
- Discover: Earn $50 cash back with Discover when you apply using my referral link and are approved for the card.
- Amex Blue Cash Preferred: Earn $250 as statement credit when you spend $3000 in first six months. Apply using my referral link.
Leave a comment