Avoid vertical line at the end of data (LaTeX pgfplots) - latex

I'd like to draw a line plot the years on the x-axis with xmax = 2020 in LateX pgfplots. As my data ends in 2019 I get a vertical line at 2019. It's probably quite easy but I didn't find anything about how to avoid that.
Thanks in advance!
Example code:
\documentclass[tikz, border=1mm]{standalone}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
width = 0.6\textwidth,
xticklabel style=
{/pgf/number format/1000 sep=,rotate=90,anchor=east},
xmin = 1995, xmax = 2020,
ymin = 0, ymax = 1.2,
xlabel = Year,
ylabel style ={align=center}, ylabel = Specific energy consumption\\in kWh/pkm,
yticklabels={, 0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2}]
\addplot [] coordinates {
(1995,1.102) (1996,1.067) (1997,1.048) (1998,0.939) (1999,0.905) (2000,0.901) (2001,0.924) (2002,0.944) (2003,0.906) (2004,0.9) (2005,0.901) (2006,0.89) (2007,0.854) (2008,0.829) (2009,0.813) (2010,0.839) (2011,0.846) (2012,0.831) (2013,0.772) (2014,0.768) (2015,0.785) (2016,0.794) (2017,0.765) (2018,0.755) (2019,0.768) } \closedcycle;
\end{axis}
\end{tikzpicture}
\end{figure}

With \closedcycle you explicitly add the line
\documentclass[tikz, border=1mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
width = 0.6\textwidth,
xticklabel style=
{/pgf/number format/1000 sep=,rotate=90,anchor=east},
xmin = 1995, xmax = 2020,
ymin = 0, ymax = 1.2,
xlabel = Year,
ylabel style ={align=center}, ylabel = Specific energy consumption\\in kWh/pkm,
yticklabels={, 0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2}]
\addplot [] coordinates {
(1995,1.102) (1996,1.067) (1997,1.048) (1998,0.939) (1999,0.905) (2000,0.901) (2001,0.924) (2002,0.944) (2003,0.906) (2004,0.9) (2005,0.901) (2006,0.89) (2007,0.854) (2008,0.829) (2009,0.813) (2010,0.839) (2011,0.846) (2012,0.831) (2013,0.772) (2014,0.768) (2015,0.785) (2016,0.794) (2017,0.765) (2018,0.755) (2019,0.768) }
% \closedcycle
;
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

Related

Fill between function does not account for areas of other curves

In the below graph, the fill below function works great for filling in between, path B and path C. But it is not so good when I do not want to fill anything below path A (that is I do not want the area under blue line shaded, I do not how to get rid of that shaded area). Is there a way to do this?
\usepackage{tikz}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = $ $,
ymin = 0,
ymax = 6,
xmin = 0,
xmax = 4,
samples = 100,
xticklabels={},
yticklabels={},
]
\addplot[color=blue, domain=0:4,name path =A] {4-x};
\addplot[color=red, domain=0:4, name path =C] {x};
\addplot[color=purple, domain=0:4, name path =B] {1.5*x};
\addlegendentry{$f(x)= a-bx$}
\addlegendentry{$g(x)= \tilde{d}x$}
\addlegendentry{$g(x)= dx$}
\addplot fill between[
of = C and B,
soft clip={domain=1.6:2},
];
\addplot fill between[
of = C and A,
soft clip={domain=1.6:2},
];
\end{axis}
\end{tikzpicture}

adjust labels on tikz bar chart

How can I adjust the labels on the y-axis?
In the current version, the labels are not aligned to the bars.
The example is from:
https://texample.net/tikz/examples/bar-chart/
I add a few more data fields and removed the second category.
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title = Contributions per category
at LaTeX-Community.org,
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
enlarge y limits = 0.2,
enlarge x limits = 0.02,
symbolic y coords = {test1,test2,test3,test4,LaTeX, Tools, Distributions, Editors},
nodes near coords,
]
\addplot coordinates { (57727,LaTeX) (5672,Tools)
(2193,Distributions) (11106,Editors)
(11106,test1)
(11106,test2)
(11106,test3)
(11106,test4)
};
\legend{Topics}
\end{axis}
\end{tikzpicture}
\end{document}
With the ytick=data option:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title = Contributions per category at LaTeX-Community.org,
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
enlarge y limits = 0.2,
enlarge x limits = 0.02,
symbolic y coords = {test1,test2,test3,test4,LaTeX,Tools,Distributions,Editors},
ytick=data,
nodes near coords,
nodes near coords align={horizontal},
]
\addplot coordinates {
(57727,LaTeX)
(5672,Tools)
(2193,Distributions)
(11106,Editors)
(11106,test1)
(11106,test2)
(11106,test3)
(11106,test4)
};
\legend{Topics}
\end{axis}
\end{tikzpicture}
\end{document}

Arrow between two nodes in two different tikzpictures

I have a tikzpicture where I use two def blocks to define two unique figures. I want to draw an arrow between two nodes and each of them locate in each def block. There are IDs assigned to each node, but the arrow I get is not what I want.
\usetikzlibrary{shapes}
\usetikzlibrary{math}
\def\clusterone{%
\begin{tikzpicture}[scale=1.0]
\foreach \radius [count=\angleCount from 0] in {0.2, 0.6, 0.4, 0.5}
{\node[draw=green, circle, inner sep=0pt, minimum size=2pt, fill=green] (c1-\angleCount) at ({\radius * cos(90 * \angleCount)}, {\radius * sin(90 * \angleCount)}) {};}
\end{tikzpicture}%
}
\def\clustertwo{%
\begin{tikzpicture}[scale=1.0]
\foreach \radius [count=\angleCount from 0] in {0.6, 0.1, 0.4, 0.6}
{\node[draw=green, circle, inner sep=0pt, minimum size=2pt, fill=green] (c2-\angleCount) at ({\radius * cos(90 * \angleCount)}, {\radius * sin(90 * \angleCount)}) {};}
\end{tikzpicture}%
}
\scalebox{1}{
\begin{tikzpicture}
\node[rotate=30] (node1) at (0, 1.5) {\clusterone};
\node[] at (0, 1.5) {1};
\node[rotate=30] (node2) at (0, 0) {\clustertwo};
\node[] at (0, 0) {2};
\draw[->] (c1-0) -- (c2-3);
\end{tikzpicture}
}
Expected
Current Result
Is there a way to address IDs of two nodes that are located inside two different tikzpictures? Or is there a way to draw an arrow between nodes in this situation?
Don't nest tikz pictures!
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{math}
\begin{document}
\def\clusterone{%
\foreach \radius [count=\angleCount from 0] in {0.2, 0.6, 0.4, 0.5}
{\node[draw=green, circle, inner sep=0pt, minimum size=2pt, fill=green] (c1-\angleCount) at ({\radius * cos(90 * \angleCount)}, {\radius * sin(90 * \angleCount)}) {};}
}
\def\clustertwo{%
\foreach \radius [count=\angleCount from 0] in {0.6, 0.1, 0.4, 0.6}
{\node[draw=green, circle, inner sep=0pt, minimum size=2pt, fill=green] (c2-\angleCount) at ({\radius * cos(90 * \angleCount)}, {\radius * sin(90 * \angleCount)}) {};}
}
\begin{tikzpicture}
\begin{scope}[yshift=1.5cm]
\clusterone
\end{scope}
\node[] at (0, 1.5) {1};
\begin{scope}
\clustertwo
\end{scope}
\node[] at (0, 0) {2};
\draw[->] (c1-0) -- (c2-3);
\end{tikzpicture}
\end{document}

Remove white space on right side of pgfplot

Currently I have the following figures (plots):
I have placed the pgfplots inside two subfigures such that I can add the captions. The problem however is that some white space is added on the right side of Figure 3b. I am using the \hfill option but still this white space is note removed. Does someone know how to get rid of this white space such that the righthandside of Figure 3b alligns with the borders of my paper (represented by text text text ...)
My code is as follows:
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{subcaption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{font=\footnotesize}
\begin{document}
\begin{figure}[H]
\centering
\begin{subfigure}{0.49\textwidth}
\begin{tikzpicture}
\begin{axis}[
title={},
xlabel={Epochs},
ylabel={Rewards},
ylabel near ticks,
legend pos=south east,
ymajorgrids=true,
grid style=densely dotted,
width=7cm,
height=6cm,
legend cell align={left},
legend style={font=\footnotesize}
]
\addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
(1, 1.5)(2, 1.3)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
\legend{Plot1,}
\end{axis}
\begin{axis}[
ylabel={Percentage (\%)},
axis y line*=right,
ylabel near ticks,
ylabel style={rotate=-180},
hide x axis,
width=7cm,
height=6cm,
legend style={font=\footnotesize}
]
\addplot[color = black, mark = *, mark size = 1] coordinates {
(1, 0.5)(2, 0.6)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {0.53}; % Constant line
\legend{Plot2,}
\end{axis}
\end{tikzpicture}
\caption{Performance}
\label{Plot:}
\end{subfigure}\hfill
\begin{subfigure}{0.49\textwidth}
\begin{tikzpicture}
\begin{axis}[
title={},
xlabel={Epochs},
ylabel={Rewards},
ylabel near ticks,
legend pos=south east,
ymajorgrids=true,
grid style=densely dotted,
width=7cm,
height=6cm,
legend cell align={left},
legend style={font=\footnotesize}
]
\addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
(1, 1.5)(2, 1.3)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
\legend{Plot1,}
\end{axis}
\end{tikzpicture}
\caption{Action distribution}
\label{Plot:}
\end{subfigure}
\caption{Results}
\label{Plot:}
\end{figure}
\end{document}
The problem is that your plots are about 7 cm wide and you place them in a subfigure of .49\textwidth, so the \hfill you place between the subfigures only sees the objects of .49\textwidth and add fill space according to this. If you want to move the plot towards the right boarder, you could
a) place the \hfill into the subfigure (the caption won't be centred anymore)
b) adjust the .49\textwidth to match the actual width of the plot (something around 7 cm)
(the \fbox{...} in the code below is just to visualize the size of the plots)
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[margin=2.5cm,showframe]{geometry}
\usepackage{subcaption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{font=\footnotesize}
\begin{document}
\begin{figure}[H]
\centering
\begin{subfigure}{0.49\textwidth}
\fbox{\begin{tikzpicture}
\begin{axis}[
title={},
xlabel={Epochs},
ylabel={Rewards},
ylabel near ticks,
legend pos=south east,
ymajorgrids=true,
grid style=densely dotted,
width=7cm,
height=6cm,
legend cell align={left},
legend style={font=\footnotesize}
]
\addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
(1, 1.5)(2, 1.3)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
\legend{Plot1,}
\end{axis}
\begin{axis}[
ylabel={Percentage (\%)},
axis y line*=right,
ylabel near ticks,
ylabel style={rotate=-180},
hide x axis,
width=7cm,
height=6cm,
legend style={font=\footnotesize}
]
\addplot[color = black, mark = *, mark size = 1] coordinates {
(1, 0.5)(2, 0.6)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {0.53}; % Constant line
\legend{Plot2,}
\end{axis}
\end{tikzpicture}}
\caption{Performance}
\label{Plot:}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
\hfill%
\fbox{\begin{tikzpicture}
\begin{axis}[
title={},
xlabel={Epochs},
ylabel={Rewards},
ylabel near ticks,
legend pos=south east,
ymajorgrids=true,
grid style=densely dotted,
width=7cm,
height=6cm,
legend cell align={left},
legend style={font=\footnotesize}
]
\addplot[color = black, line width = 0.25mm, mark = *, mark size = 1] coordinates {
(1, 1.5)(2, 1.3)
};
\addplot[color = black, line width = 0.25mm, mark = none, samples = 2, dashed] {1.4}; % Constant line
\legend{Plot1,}
\end{axis}
\end{tikzpicture}}%
\caption{Action distribution}
\label{Plot:}
\end{subfigure}
\caption{Results}
\label{Plot:}
\end{figure}
\end{document}

How to flip the orientation of beamsplitter in pst-optexp package in latex

I was trying to plot an interferometer in latex using pst-optexp package. Everything works fine the beamsplitter is in the wrong orientation and need to be fliped 90 degree. However, the command\beamsplitter[bsstyle=plate, compname=BS,label = 0.8 -45](A)(BS)(PD){BS} does not work. Does anyone know how to rotate the beamsplitter in that package?
The code I used is
\documentclass[margin=36pt]{standalone}
\usepackage{pst-optexp}
\usepackage{amsmath}
\begin{document}
\begin{pspicture}[showgrid](6,6)
\pnodes(0,3){S}(1,3){LS}(2,3){A}(3,3){BS}(5,3){M1}(3,5){M2}(3,1){PD}
\optbox[abspos = 0.5, optboxsize=1 0.5, labeloffset = 0](S)(LS){Laser}
\lens[compname=L0, lensradius=0.5 0.5, lensheight = 0.5, abspos = 0.2, n=2,thicklens = false](LS)(A){obj}
\pinhole[compname = PH, labeloffset = -0.7](LS)(A){Pinhole}
\lens[compname=L1, abspos = 1, n=2, thicklens = false](LS)(A){Lens1}
\psset{mirrortype=extended, mirrordepth=0.2}
\beamsplitter[bsstyle=plate, compname=BS,label = 0.8 -45](A)(BS)(PD){BS}
\optbox[abspos = 1.75, optboxsize=0.5 0.5, label = 0.7 180](BS)(M1){Sample}
\mirror[compname=M1,labeloffset = 1](BS)(M1)(BS){Test Arm}
\mirror[compname=M2, mirrortype=piezo](BS)(M2)(BS){Ref Arm}
\lens[compname=L2, label = 1 180](BS)(PD){Lens2}
\optdetector[compname=Det, dettype=round, label = 0.5 90,abspos = 2.5](BS)(PD){Camera}
\addtopsstyle{Beam}{beamwidth=0.2, fillstyle=solid, linecolor = red, fillcolor=red, opacity = 0.2}
\drawwidebeam(LS){L0}{L1}{BS}{M1}{BS}{M2}{BS}{L2}{Det}
\end{pspicture}
\end{document}
The resulting picture is the orientation of beam splitter is wrong. The example given in pst-optexp manual is wrong in the same way.

Resources