我正在努力绘制我的第一张 Tikz 图表。我想在坐标 (20.48,0.6358) 处绘制一个圆,该圆对应于最小值。但是,由于轴具有不同的刻度 ( [x=0.15cm,y=6cm]
),圆会按比例扭曲。有什么方法可以避免这种情况并显示一个圆角圆(最好带有字母“A”或其他东西)?
*奖励:是否可以将 y 轴放在两条线上或沿 y 轴垂直放置?这个\\
技巧不起作用。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[x=0.15cm,y=6cm]
\def\xmin{0}
\def\xmax{57}
\def\ymin{0}
\def\ymax{1.12}
% grid
\draw[style=help lines, ystep=0.1, xstep=5, color=lightgray] (\xmin,\ymin) grid
(\xmax,\ymax);
% axes
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {\textit{Var2 (years)}};
\draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {\textit{Var 1 ideally in two \\ lines or vertically }};
% xticks and yticks
\foreach \x in {0,5,10,...,55}
\node at (\x, \ymin) [below] {\x};
\foreach \y in {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1}
\node at (\xmin,\y) [left] {\y};
% generate and plot another a curve y = 0.1 x^2 + 2.5
% this generates the files figure.parabola.gnuplot and figure.parabola.table
\draw[color=red, domain=\xmin:\xmax, line width=0.09cm] plot[id=parabola]
function{0.7532*(1+(0.00034*x**2 - 0.01393*x))} node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (0,0.6358) -- (20.48,0.6358) -- (20.48,0);
\draw [fill=black, black] (20.48,0.6358) circle [radius=0.08];
\end{tikzpicture}
\end{document}
答案1
不要对整体进行更改,x
而是在范围内进行更改以保持其局部效果;通过范围内的值更改执行所有应该受到影响的事情;在范围外画一个圆圈:y
tikzpicture
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\def\xmin{0}
\def\xmax{57}
\def\ymin{0}
\def\ymax{1.12}
\begin{scope}[x=0.15cm,y=6cm]
% grid
\draw[style=help lines, ystep=0.1, xstep=5, color=lightgray] (\xmin,\ymin) grid
(\xmax,\ymax);
% axes
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {\textit{Var2 (years)}};
\draw[->] (\xmin,\ymin) -- (\xmin,\ymax)
node[midway,rotate=90,yshift=28pt] {\textit{Var 1 ideally in two lines or vertically }}
node[align=center,anchor=south] {\itshape Var 1 ideally in two \\ lines or vertically };
% xticks and yticks
\foreach \x in {0,5,10,...,55}
\node at (\x, \ymin) [below] {\x};
\foreach \y in {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1}
\node at (\xmin,\y) [left] {\y};
% generate and plot another a curve y = 0.1 x^2 + 2.5
% this generates the files figure.parabola.gnuplot and figure.parabola.table
\draw[color=red, domain=\xmin:\xmax, line width=0.09cm] plot[id=parabola]
function{0.7532*(1+(0.00034*x**2 - 0.01393*x))} node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (0,0.6358) -- (20.48,0.6358) -- (20.48,0);
\coordinate (aux) at (20.48,0.6358);
\end{scope}
\draw [fill=black, black,] (aux) circle [radius=0.08];
\end{tikzpicture}
\end{document}
奖励积分:
如果要旋转节点标签,可以使用rotate=<angle>
key 来旋转文本。如果要多行标签,可以使用text width=<length>
and/或align=<option>
and 替换\textit
for \itshape
(前者不允许换行)。
不过,我建议你改用pgfplots
这是专门为绘图而设计的;现在你的代码更短了,并且你拥有开箱即用的功能来定制你的绘图:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
xmin=0,
xmax=57,
ymin=0,
ymax=1.05,
xtick={0,5,...,55},
ytick={0.1,0.2,...,1},
xlabel={Var2 (years)},
ylabel={Var 1 ideally in two lines or vertically},
grid=both,
width=10cm,
height=7.5cm,
clip=false
]
\addplot[red, domain=0:57,line width=3pt] {0.7532*(1+(0.00034*x^2 - 0.01393*x))}
node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (axis cs:0,0.6358) -- (axis cs:20.48,0.6358) -- (axis cs:20.48,0);
\draw [fill=black, black,] (axis cs:20.48,0.6358) circle [radius=2pt];
\end{axis}
\end{tikzpicture}
\end{document}
答案2
从给定的 MWE 我无法重现所讨论的图像,因此我更改了绘图功能的代码行:
\draw[domain=\xmin:\xmax, line width=0.9mm, red]
plot[id=parabola] (\x,{0.7532*(0.00034*\x*\x - 0.01393*\x + 1)})
node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
其中,我没有看到最小位置有任何失真。MWE 为:
\documentclass[border=3mm,
tikz,
]{standalone}
\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}[x=0.15cm,y=6cm]
\def\xmin{0}
\def\xmax{57}
\def\ymin{0}
\def\ymax{1.12}
% grid
\draw[style=help lines, ystep=0.1, xstep=5, color=lightgray]
(\xmin,\ymin) grid (\xmax,\ymax);
% axes
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {\textit{Var2 (years)}};
\draw[->] (\xmin,\ymin) -- node[font=\itshape,above=7mm,align=center,sloped]
{Var 1 ideally in two\\
lines or vertically} (\xmin,\ymax);
% xticks and yticks
\foreach \x in {0,5,10,...,55}
\node at (\x, \ymin) [below] {\x};
\foreach \y in {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1}
\node at (\xmin,\y) [left] {\y};
% generate and plot another a curve y = 0.1 x^2 + 2.5
% this generates the files figure.parabola.gnuplot and figure.parabola.table
\draw[domain=\xmin:\xmax, line width=0.9mm, red]
plot[id=parabola] (\x,{0.7532*(0.00034*\x*\x - 0.01393*\x + 1)})
node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (0,0.6358) -| (20.48,0);
\draw [fill=black] (20.48,0.6358) circle (0.8mm);
\end{tikzpicture}
\end{document}
答案3
解决这个问题的一种方法是将其绘制为椭圆形,具有不同的x radius
和y radius
。乘以另一个单位向量会得到一个圆。为了允许换行,请将align=<alignment>
键添加到节点。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[x=0.15cm,y=6cm]
\def\xmin{0}
\def\xmax{57}
\def\ymin{0}
\def\ymax{1.12}
% grid
\draw[style=help lines, ystep=0.1, xstep=5, color=lightgray] (\xmin,\ymin) grid
(\xmax,\ymax);
% axes
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {\textit{Var2 (years)}};
\draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above,align=center] {\textit{Var 1 ideally in two} \\ \textit{lines or vertically }};
% xticks and yticks
\foreach \x in {0,5,10,...,55}
\node at (\x, \ymin) [below] {\x};
\foreach \y in {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1}
\node at (\xmin,\y) [left] {\y};
% generate and plot another a curve y = 0.1 x^2 + 2.5
% this generates the files figure.parabola.gnuplot and figure.parabola.table
\draw[color=red, domain=\xmin:\xmax, line width=0.09cm] plot[id=parabola]
function{0.7532*(1+(0.00034*x**2 - 0.01393*x))} node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (0,0.6358) -- (20.48,0.6358) -- (20.48,0);
\draw [fill=black, black] (20.48,0.6358) circle [x radius=0.08*6,y radius=0.08*0.15];
\end{tikzpicture}
\end{document}
话虽如此,你考虑过吗pgfplots
?
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{axis}[
ylabel={var 1 vertically},
xlabel={var 2},
every axis label/.append style={font=\itshape},
grid,
domain=0:57,
ymin=0,ymax=1.2,
axis lines=left,
ytick={0,0.1,...,1.2},
xtick={0,5,...,55},
clip=false
]
\addplot [red] {0.7532*(1+(0.00034*x^2 - 0.01393*x))} node [right] {$y=0.00034\,x^2 + 0.01393\,x$};
\draw [thick, dashed] (0,0.6358) -- (20.48,0.6358) coordinate(minimum) -- (20.48,0);
\end{axis}
\draw [fill=black, black] (minimum) circle [radius=0.08];
\end{tikzpicture}
\end{document}