Plot of sin(x)/x is shifted/misplaced - latex

I want to plot sin(x)/x but the x-axis is offset by \pi/2.
The mwe is
\documentclass{scrbook}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
% cycle list,
clip=true,
xmin=-4*pi,xmax=4*pi,
%axis lines=left,
%axis x line=middle,
%axis y line=left,
xtick distance = pi/2,
xticklabels={
% foo,
$-4\pi$,
$-\frac{7}{2}\pi$,
$-3\pi$,
$-\frac{5}{2}\pi$,
$-2\pi$,
$-\frac{3}{2}\pi$,
$-\pi$,
$-\frac{1}{2}\pi$,
$0$,
$\frac{1}{2}\pi$,
$\pi$,
$\frac{3}{2}\pi$,
$2\pi$,
$\frac{5}{2}\pi$,
$3\pi$,
$\frac{7}{2}\pi$,
$4\pi$
},
ylabel = {$\sin(x)/x$},
xlabel = {},
]
\addplot[
domain=-4*pi:4*pi,samples=4000,
]
{sin(deg(x))/deg(x)};
\end{axis}
\end{tikzpicture}
\caption{Example of local minima, and global minima and maxima.}
\label{fig:Example of local minima, and local and global maxima.}
\end{figure}
\end{document}
When I uncomment line 16 %foo the x-labels are misplaced, when I leave it there, the label foo is not printed but the alignment of the labels are (seemingly) correct.
I have no idea where the issue is.

Instead of manually adding the labels, you could use https://tex.stackexchange.com/a/396298/36296 (I had to increase the width of the graph, otherwise there wouldn't be enough room to label every pi/2):
\documentclass{scrbook}
\usepackage{tikz,pgfplots}
% Load math library, for \tikzmath
\usetikzlibrary{math}
\pgfplotsset{
% Typeset fractions of pi at regular intervals on x axis
x axis in pi/.style={
% Set tick distance from style argument
xtick distance={pi/#1},
% Set label style: calculate reduced fraction of pi
xticklabel={
\tikzmath{
% Calculate this tick's multiple of pi/#1
int \numorig, \gcd, \num, \denom, \absnum;
\numorig = round(\tick*#1/pi);
% Calculate reduced fraction for \numorig/#1
\gcd = gcd(\numorig,#1);
\num = \numorig / \gcd;
\absnum = abs(\num);
\denom = #1 / \gcd;
% Build label text
if \num < 0 then {
let \sign = -;
} else {
let \sign =;
};
if \absnum == 1 then {
let \numpi = \pi;
} else {
let \numpi = \absnum\pi;
};
if \denom == 1 then {
if \num == 0 then {
{ \strut$0$ };
} else {
{ \strut$\sign\numpi$ };
};
} else {
{ \strut$\sign\frac{\numpi}{\denom}$ };
% Other style with all pi symbols same and aligned:
%{ \strut$\sign\frac{\absnum}{\denom}\pi$ };
};
}
},
},
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
% cycle list,
clip=true,
xmin=-4*pi,xmax=4*pi,
ylabel = {$\sin(x)/x$},
xlabel = {},
x axis in pi=2,
]
\addplot[
domain=-4*pi:4*pi,samples=4000,
]
{sin(deg(x))/deg(x)};
\end{axis}
\end{tikzpicture}
\caption{Example of local minima, and global minima and maxima.}
\label{fig:Example of local minima, and local and global maxima.}
\end{figure}
\end{document}

Related

Big-O Notation's Pgfplots Representation

I am looking for graphs of big-o notation terms below;
O(1) is a constant time complexity
O(n) is a linear time complexity
O(log_2(n)) is a logarithmic time complexity (I mean log basis 2)
O(n log_2(n))
O(n^2) is a quadratic time complexity
O(n^3) cubic time complexity
O(2^n) exponential time complexity
Could you help me to represent these big O notations using pgfplots?
I have tried this code snipped for log_2(n). To tell the truth, I'm not so sure.
\documentclass[border=50pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[trim axis left]
\begin{axis}
[
axis lines = left,
xlabel = \(n\),
ylabel = {\(\log_{2}(n)\)},
]
[domain=0:10,
samples=1000,
enlarge x limits=false,
grid=both,
no markers,
axis equal,
legend pos=outer north east,
legend style={draw=none},]
\addplot +[thick] {ln(x)/ln(2)};
\addlegendentry{$\log_{2}(n)$};
\end{axis}
\end{tikzpicture}
\end{document}
You were almost there ... The only missing parts were other functions and proper axes configuration. Here is my solution. You might need to think about the ranges. I am not entirely sure they are correct but did not bother to check. I changed a way you to make the graph itself centred; I think overlay works better.
\documentclass[margin=50pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis lines = left,
xmin=1, xmax=8.1, ymin=1, ymax=9,
domain=0:8, samples=100, no markers, thick, grid=both,
xlabel = \(n\), ylabel = {\(f(n)\)},
label style = {overlay}, % Has he same effect as
ticklabel style = {overlay}, % trim axis [left|right]
legend entries = {
$1$,
$\log_{5}n$,
$\log_{2}n$,
$n$,
$n\log_{2}n$,
$2^{n}$,
$n^{2}$,
$n^{3}$,
},
every axis/.style = {font=\footnotesize},
label style = {font=\normalsize},
]
\addplot+ {1};
\addplot+ {1 + ln(x)/ln(5)};
\addplot+ {1 + ln(x)/ln(2)};
\addplot+ {x};
\addplot+ {1 + x*ln(x)/ln(2)};
\addplot+ {2^x-1};
\addplot+ {x^2};
\addplot+ {x^3};
\end{axis}
\end{tikzpicture}
\end{document}

Bounding box coordinates for pgfplot

How can I get the bounding box coordinates for the pgfplot below? I need the coordiates in pgfplot space. (current axis.south west) and (current bounding box.south west), for example, do not give what I need. I'm expecting something like SW = (-7,-1.1), NE = (8.2,1.4), which would extend to the right beyond the red rectangle.
\documentclass[border=0pt,multi=my]{standalone}
\usepackage{amssymb,amsmath}
\usepackage[pass,paperheight=\maxdimen]{geometry}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{xcolor}
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{calc,shapes,positioning,fit,decorations,decorations.text,decorations.pathmorphing,backgrounds,patterns,arrows,arrows.meta}
\usepackage{ifthen}
\usepackage{pagecolor}
\tikzset{every picture/.style={line width=0.02cm}}
\begin{document}
\pagestyle{empty}
\begin{my}
\begin{tikzpicture}[tight background,background rectangle/.style={draw=none,fill=red!45},show background rectangle,scale=0.85]
\begin{axis}[
name=foo,
width=360pt,
height=210pt,
xmin=-7,
xmax=8,
ymin=-1.1,
ymax=1.4,
xtick={0},
xticklabels={$0$},
ytick={0},
yticklabels={$0$},
extra x ticks={-6.283185307,-4.71238898,-3.141592654,-1.570796327,0,1.570796327,3.141592654,4.71238898,6.283185307},
extra x tick labels={$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
extra x tick style={
xticklabel style={below right},
},
extra y ticks={-1,1},
extra y tick labels={$-1$,$1$},
extra y tick style={
yticklabel style={right,xshift=5pt},
},
axis x line=center,
axis y line=center]
\addplot[line width=0.0325cm, cyan!60!black, domain=-7:7, samples=200] {sin(deg(x))};
\node at (axis cs:8.1,-0.12) {$x$};
\node[anchor=west] at (axis cs:0.1,1.3) {$y$};
\end{axis}
\end{tikzpicture}
\end{my}
\end{document}
It sounds as if you are looking for \pgfplotspointgetnormalizedcoordinates{}
\documentclass[border=0pt,multi=my]{standalone}
\usepackage{amssymb,amsmath}
\usepackage[pass,paperheight=\maxdimen]{geometry}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{xcolor}
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{calc,shapes,positioning,fit,decorations,decorations.text,decorations.pathmorphing,backgrounds,patterns,arrows,arrows.meta}
\usepackage{ifthen}
\usepackage{pagecolor}
\tikzset{every picture/.style={line width=0.02cm}}
\begin{document}
\pagestyle{empty}
\begin{my}%
\begin{tikzpicture}[tight background,background rectangle/.style={draw=none,fill=red!45},show background rectangle,scale=0.85]
\begin{axis}[
name=foo,
width=360pt,
height=210pt,
xmin=-7,
xmax=8,
ymin=-1.1,
ymax=1.4,
xtick={0},
xticklabels={$0$},
ytick={0},
yticklabels={$0$},
extra x ticks={-6.283185307,-4.71238898,-3.141592654,-1.570796327,0,1.570796327,3.141592654,4.71238898,6.283185307},
extra x tick labels={$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
extra x tick style={
xticklabel style={below right},
},
extra y ticks={-1,1},
extra y tick labels={$-1$,$1$},
extra y tick style={
yticklabel style={right,xshift=5pt},
},
axis x line=center,
axis y line=center]
\addplot[line width=0.0325cm, cyan!60!black, domain=-7:7, samples=200] {sin(deg(x))};
\node (myx) at (axis cs:8.1,-0.12) {$x$};
\node[anchor=west] at (axis cs:0.1,1.3) {$y$};
\node[green] at (200,10) {
\pgfplotspointgetnormalizedcoordinates{(xticklabel cs:0)}
\pgfmathprintnumber{\pgfkeysvalueof{/data point/x}}
\pgfplotspointgetnormalizedcoordinates{(myx.east)}
\pgfmathprintnumber{\pgfkeysvalueof{/data point/x}}
\pgfplotspointgetnormalizedcoordinates{(yticklabel cs:0)}
\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
\pgfplotspointgetnormalizedcoordinates{(yticklabel cs:1)}
\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
};
\draw[green, ultra thick] (axis cs:-7,-1.1) rectangle (axis cs:8.41,1.4);
\end{axis}
\end{tikzpicture}%
\end{my}%
\end{document}

Vertical line in 2d plot using tikzpicture

After plotting on overleaf the following right line
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},
]
%Below the red parabola is defined
\addplot [
domain=-10:10,
samples=100,
color=red,
]
{((6*x - 48)/8)};
\addlegendentry{$6x + 8y = 48$}
I need to add a vertical line for x = 6 on the same plot.
Could you help me with that?
Having a look here, one possible solution is the following:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},
]
%Below the red parabola is defined
\addplot [
domain=-10:10,
samples=100,
color=red,
]
{((6*x - 48)/8)};
%% This is the vertical line
\addplot[thick, samples=50, smooth,domain=0:6,magenta] coordinates {(6,0)(6,-15)};
\addlegendentry{$6x + 8y = 48$}
% Added to the legend
\addlegendentry{$x = 6$}
\end{axis}
\end{tikzpicture}
\end{document}

Multiple horizontal bar-charts in a row in latex

I have three horizontal bar-charts that I created using both the Tikz package and PGF plots package in latex. I am using Overleaf. The three horizontal bar-charts look like the following:
Is there a way to nest these horizontal bar-charts in a row in a latex document? I need this in order to save space for a paper I am working on. So what I am looking for is this (note: this 2nd image is meant to illustrate what I am looking for exactly):
This is the set up currently:
\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{tabularx,booktabs}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{sfmath}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{algorithm, algorithmic}
\usepackage{makeidx}
\usepackage{soul}
\usepackage{color}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{amsthm,amssymb,amsmath}
\begin{document}
\pgfplotsset{width=5cm,compat=1.15}
\begin{center}
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}[title = \textbf{2016 Data-set Results: Parameters for a Successful game},
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
ytick = data,
enlarge y limits = 0.05,
enlarge x limits = 0.02,
width=0.3\textwidth,
bar width=2mm,
symbolic y coords = {User Count, Critic Count, Critic Score, User Score, Platform, Genre, Rating},
nodes near coords,
]
\addplot coordinates { (100,User Count) (93,Critic Count) (57,Critic Score) (20,User Score) (10.5,Platform) (2.3,Genre) (4.52,Rating) };
\end{axis}
\end{tikzpicture}
\caption{Results for the 2016 Dataset on what parameters make a successful game.}
\end{figure}
\end{center}
\pgfplotsset{width=5cm,compat=1.15}
\begin{center}
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}[title = \textbf{2017 Data-set Results: Parameters for a Successful game},
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
ytick = data,
enlarge y limits = 0.05,
enlarge x limits = 0.02,
width=0.3\textwidth,
bar width=2mm,
symbolic y coords = {User Count, Critic Count, Critic Score, User Score, Platform, Genre, Rating},
nodes near coords,
]
\addplot coordinates { (100,User Count) (80,Critic Count) (56,Critic Score) (20,User Score) (8.47,Platform) (2.35,Genre) (3.675,Rating) };
\end{axis}
\end{tikzpicture}
\caption{Results for the 2017 Dataset on what parameters make a successful game.}
\end{figure}
\end{center}
\pgfplotsset{width=5cm,compat=1.15}
\begin{center}
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}[title = \textbf{2019 Data-set Results: Parameters for a Successful game},
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
ytick = data,
enlarge y limits = 0.05,
enlarge x limits = 0.02,
width=0.3\textwidth,
bar width=2mm,
symbolic y coords = {User Count, Critic Count, Critic Score, User Score, Platform, Genre, Rating},
nodes near coords,
]
\addplot coordinates { (99.8,User Count) (73.6,Critic Count) (71.3,Critic Score) (21,User Score) (9.58,Platform) (2.35,Genre) (4,Rating) };
\end{axis}
\end{tikzpicture}
\caption{Results for the 2019 Dataset on what parameters make a successful game.}
\end{figure}
\end{center}
\end{document}

Latex Tikzpictures multiple entries [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I've got a problem with tikzpicture. I've made my cv in Latex but one thing isn't working fine. In a skill graph I got multiple entries. Here's the code.
\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[thick,scale=0.8, every node/.style={scale=0.8},every node/.style={font=\sffamily}]
\begin{axis}[title = \textbf{Sprachen},
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
%enlarge y limits={abs=1cm},
enlarge x limits = 0.02,
symbolic y coords = { Französisch, Englisch, Deutsch},
%nodes near coords,
xmin=0,
xmax=1,
]
\addplot coordinates {
(1,Deutsch)
(0.65,Englisch)
(0.3,Französisch)
};
%\legend{Topics}
\end{axis}
\node[text width=3cm] at (1.5,-0.5)
{Grundkennt.};
\node[text width=3cm] at (5.25,-0.5)
{gut};
\node[text width=3cm] at (7.4,-0.5)
{sehr gut};
\draw (2.5,0.1) -- (2.5, 5.5);
\draw (5,0.1) -- (5, 5.5);
\end{tikzpicture}
\end{center}
\end{document}
Multiple entries
Example
I hope one of you could help me.
Greetz Michi
uncomment the
enlarge y limits={abs=1cm},
and it should work, or do you want to have different distances? Then try this code:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[thick,every node/.style={font=\sffamily}]
\begin{axis}[ title=\textbf{Sprachen},
xbar,
width=12cm, height=3.5cm, enlarge y limits=0.5,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
%xlabel={\#participants},
symbolic y coords={Französisch, Englisch, Deutsch},
ytick=data,
%nodes near coords,
nodes near coords align={horizontal}, ]
\addplot coordinates {(1,Deutsch) (0.65,Englisch) (0.3,Französisch)};
\end{axis}
\node[text width=3cm] at (1.5,-0.5)
{Grundkennt.};
\node[text width=3cm] at (5.25,-0.5)
{gut};
\node[text width=3cm] at (7.4,-0.5)
{sehr gut};
\draw (2.5,0.1) -- (2.5, 1.95);
\draw (5,0.1) -- (5, 1.95);
\end{tikzpicture}
\end{document}
I had to recode it and use an example from this page as template. I still don't know what caused this repeated language labels. But this solution should solve it and have decreased distances.
By the way, if you are looking for a good template for CV's in LaTeX look on Overleaf (I like moderncv).

Resources