通常我使用普通的 tikz 来绘制曲线。我需要“教科书风格”的坐标系统,网格为 50 毫米。
我尝试使用 tikz 数据可视化库。
\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[]
\datavisualization [
school book axes={unit=0.5},
visualize as smooth line,
x axis={label={$x$},grid,grid={minor steps between steps=1}},
y axis={label={$y$},grid,grid={minor steps between steps=1}},
every major grid/.style = {style={gray, thin}},
every minor grid/.style = {style={gray, very thin}}
]
data [format=function] {
var x : interval [-2:2];
func y = 1/2*(\value x)^2;
}
info' {
\fill[fill=lightgray] (visualization cs: x=1, y=0) -- plot [domain=1:2] (visualization cs: x=\x,y={0.5*(\x)^2}) -- (visualization cs: x=2, y=0) --cycle;
}
;
\end{tikzpicture}
\end{document}
我唯一做错的是给曲线下的区域着色。我在信息块中尝试了以下操作:
\fill[fill=lightgray] (visualization cs: x=1, y=0) -- plot [domain=1:2] (visualization cs: x=\x,y={0.5*(\x)^2}) -- (visualization cs: x=2, y=0) --cycle;
但这会导致 PGF 数学错误。包 PGF 数学错误:无法解析输入“0.5*(1)^2”
我想我需要一种方法来告诉 plot 命令使用可视化 cs。
提前致谢。
干杯
答案1
最好创建一个不同于 的新可视化工具visualize as line
。所以我从 复制了后者的定义tikzlibrarydatavisualization.code.tex
。唯一的不同之处在于
every path/.style={draw},
更改为
every path/.style={draw,fill},
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{datavisualization,datavisualization.formats.functions}
\begin{document}
\makeatletter
\tikzdatavisualizationset{
visualize as pie/.style={
new object={
when=after survey,
store=/tikz/data visualization/visualizers/#1,
class=plot handler visualizer,
arg1=#1,
arg2={\tikz@dv@plot@handler,\tikz@dv@plot@mark@maker}
},
new visualizer={#1}{%
every path/.style={draw,fill},
style={every mark/.append style={color=visualizer color}},
mark size=2pt,
semithick,
color=visualizer color,
mark=none,
/tikz/data visualization/every visualize as line/.try,
}{visualizer in legend=\tikz@dv@legend@entry@as@example},
#1={straight line}
},
visualize as pie/.default=pie,
}
\begin{tikzpicture}
\datavisualization[
school book axes={unit=0.5},
x axis={label={$x$},grid,grid={minor steps between steps=1}},
y axis={label={$y$},grid,grid={minor steps between steps=1}},
every major grid/.style={style={gray,thin}},
every minor grid/.style={style={gray,very thin}},
visualize as pie
]
data point[x=-2, y=0]
data[format=function]{var x :interval [-2:2];func y =1/2*(\value x)^2;}
data point[x=2, y=0];
\end{tikzpicture}
\end{document}
答案2
并非完全使用可视化,而是pgfplots
使用其fillbetween
库。这看起来很容易。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{fillbetween}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
grid=both,
ymin=0,
xmin=-3,xmax=3,
axis on top
]
\addplot[draw=none,name path=A,domain=-2:2,fill=olive!40] {1/2*(x)^2}\closedcycle;
\addplot[solid,thick,red,domain=-2:2] {1/2*(x)^2};
\end{axis}
\end{tikzpicture}
\end{document}
我首先填充了区域,然后绘制了曲线,以避免填充物周围的线条。此外,在这种情况下,axis on top
这有助于将x
轴保持在顶部。
答案3
使用坐标计算(\usetikzlibrary{calc}
)可以进行坐标变换。不幸的是,我只发现了这种混乱的变换,也许有人可以发布一种更简洁的方法来做到这一点。
info' {
\begin{scope}[shift={(visualization cs:x=0, y=0)},
x={($(visualization cs:x=1, y=0)-(visualization cs:x=0, y=0)$)},
y={($(visualization cs:x=0, y=1)-(visualization cs:x=0, y=0)$)}]
\fill[fill=lightgray] (1,0) -- plot [domain=1:2] (\x,{0.5*(\x)^2}) -- (2, 0) --cycle;
\end{scope}
}