我正在尝试使用“tikz”和“pgfplots”包重现下图左侧的图片。但正如您所见,我得到的最好的结果是下图右侧。在我的尝试中,我修改了 tex 代码这里并根据下面的图片进行了如下修改。
我的问题如下。
如何平移“x”轴和“y”轴使得这些轴在点 (0,0) 垂直相交?
如何在图表上定位 (x,y) 坐标?如何在图表上定位红色圆圈?
如何控制轴图例的大小?轴的大小或宽度如何更改?
\documentclass{独立} \usepackage{tikz} \使用包{pgfplots} \pgfplotsset{兼容 = 最新} \开始{文档} \开始{tikzpicture} \开始{轴}[ x最小= -6,x最大= +8, y 最小值 = -2.0,y 最大值 = +11.0, xtick 距离 = 1.0, ytick 距离 = 1.0, 网格 = 两者, 次要刻度数 = 1, 主要网格样式 = {lightgray!25}, 次要网格样式 = {lightgray!25}, 宽度 = 1.0\文本宽度, 高度 = 1.0\文本宽度, x标签 = {$x$}, y标签 = {$y$}, 图例单元格对齐 = {左}, ] \添加图[ 域 = -4:3, 样本 = 200, 光滑的, 蓝色的, 厚的, ]%{exp(ln(x+1)/ln(2))}; {2^(x)+1}; \添加图[ 域 = -4:3, 样本 = 200, 光滑的, 红色的, 虚线 ]{2^(x)}; %\legend{根据表达式绘图,根据文件绘图} \end{轴} \结束{tikzpicture} \结束{文档}
答案1
axis lines=center
有多种方法可以做到这一点,例如,您可以使用 TikZ 代码,例如
\node [circle, fill=red, minimum size=5pt, inner sep=0pt, label={[red]below right:$(0,1)$}] at (0,1) {};
。但是,我认为我宁愿使用的功能pgfplots
,特别是nodes near coords
。请参阅下面的代码作为示例,如果有任何不清楚的地方请询问。例如
legend style={font=\small}
减小图例的字体大小。这一点的其余部分您已经在代码中回答了(width
和height
键)
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.18} % using an explicit version is recommended by the package author
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center, % axis lines through origin instead of box
xmin = -6, xmax = +8,
ymin = -2.0, ymax = +11.0,
xtick distance = 1.0,
ytick distance = 1.0,
grid = both,
minor tick num = 1,
major grid style = {lightgray!25},
minor grid style = {lightgray!25},
width = 1.0\textwidth,
height = 1.0\textwidth,
xlabel = {$x$},
ylabel = {$y$},
legend cell align = {left},
visualization depends on=x\as\tmpX, % make the x coordinate available for use in nodes near coords below
visualization depends on=y\as\tmpY, % same for y
legend style={
font=\small, % change font size of legend
at={(1,1)},
anchor=north east
}
]
\addplot[
domain = -4:3,
samples = 200,
smooth,
blue,
thick,
] %{exp(ln(x+1)/ln(2))};
{2^(x)+1};
\addplot[
domain = -4:3,
samples = 200,
smooth,
red,
dashed
] {2^(x)};
\addplot [
blue,
mark=*,
only marks,
samples at={0,1,2,3}, % specify x values to plot function at
% now add a node next to each plotted point
% we use the macros defined with visualization depends on here
% to make the coordinate pair
nodes near coords={$(\pgfmathprintnumber{\tmpX}, \pgfmathprintnumber{\tmpY})$},
% finally define were the nodes are placed relative to the plotted points
nodes near coords align=above left
] {2^(x)+1};
% a more manual way of adding dots with coordinate label
%\node [circle, fill=red, minimum size=5pt, inner sep=0pt, label={[red]below right:$(0,1)$}] at (0,1) {};
\legend{Plot from expression, Plot from file}
\end{axis}
\end{tikzpicture}
\end{document}