关于pgfplots绘制散点图的一个问题

发布于 2022-03-15 07:36:20

<div class="ls_editor_block" data-type="text"><div class="b"> </div><div class="m"> <div class="c" contenteditable="plaintext-only" data-type="text" placeholder="点击' + '插入内容或直接输入">各位大佬,我现在有下面的一段代码,横轴与纵轴的坐标应该如何设置才能出现图中的效果?</div> </div></div><div class="ls_editor_block" data-type="text"><div class="b"> </div><div class="m"> <div class="c" contenteditable="plaintext-only" data-type="text" placeholder="点击' + '插入内容或直接输入"></div> </div></div><div class="ls_editor_block" data-type="code"><div class="b"> </div><div class="m"> <div class="c" contenteditable="plaintext-only" data-type="code" placeholder="点击' + '插入内容或直接输入">documentclass{article}
usepackage{pgfplots}
usetikzlibrary{arrows.meta}
tikzset{>={Stealth[scale=1.4]}}
pgfplotsset{compat=1.9,
sdt/.style={
ymin=460, ymax=680,
xmin=34, xmax=68,
axis lines =left,
tick align=inside,
tick style={black,major tick length=3},
tick label style={font=footnotesize},
font=small,
scatter/use mapped color={draw=black},
xlabel={$x$},ylabel={$y$},
xlabel style={at={(0.99,0)},below},
ylabel style={at={(0,0.98)},rotate=-90},
every outer x axis line/.append style={->,red},
every outer y axis line/.append style={->,red},
}}
begin{document}

\centering
\begin{tikzpicture}
%\pgfplotsset{ticks=none}
    \begin{axis}[sdt]
        \addplot[scatter,only marks] coordinates {%

(38,500) (40,540) (44,560) (46,570) (48,580) (50,590) (56,600)};

    \end{axis}
\end{tikzpicture}

end{document}</div> </div></div><div class="ls_editor_block" data-type="image"><div class="b"> </div><div class="m"> <div class="c" contenteditable="plaintext-only" data-type="image" placeholder="点击' + '插入内容或直接输入"><img src="/uploads/ls_editor/images/2022/03/15/cfc7df57bfabfbbdbda9367d9894ed09.jpeg"></div> </div></div>

查看更多

关注者
0
被浏览
1.3k
寄存器
寄存器 2022-03-15
这家伙很懒,什么也没写!

对于这种散点图,我觉得使用tkz-base宏包进行绘制也不失为一种方便的方案:

% 引入文档类
\documentclass[margin=5pt]{standalone}
% 导言区
\usepackage{ctex}
\usepackage{tkz-base}

% 正文区
\begin{document}

\begin{tikzpicture}[scale=1.00]
  \tkzInit[xmin=34,xmax=56,xstep=2,ymin=460,ymax=620,ystep=20]
  \tkzDrawX[label={}]
  \tkzDrawY[label={}]
  \tkzLabelX[font=\scriptsize]
  \tkzLabelY[font=\scriptsize]
  \tkzText[draw,
    line width = 1pt,%
    color = blue,%
    fill = orange!20](46,435)%
  {年宣传费/千元}
  \tkzText[draw,
    line width = 1pt,%
    color = red,%
    rotate = 90,%
    fill = orange!20](31,540)%
  {年销售量/$t$}
  \tkzDefSetOfPoints[show]{38/500, 40/540, 44/560, 46/570,
                           48/580, 50/590, 56/600}
  \tkzDrawSetOfPoints[mark=ball,mark size=2pt]
\end{tikzpicture}

\end{document}

image.png

2 个回答
雾月
雾月 2022-03-15
这家伙很懒,什么也没写!
\documentclass{article}
\usepackage[scheme=plain]{ctex}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\tikzset{>={Stealth[scale=1.4]}}
\pgfplotsset{compat=latest, 
  sdt/.style={
  ymin=460, ymax=620,
  ytick distance=20,
  xmin=34, xmax=56,
  xtick={34,36,...,56},
  axis lines =left,
  tick align=inside,
  tick style={black,major tick length=3},
  tick label style={font=\footnotesize},
  enlarge y limits={upper, abs value=6mm},
  enlarge x limits={upper, abs value=10mm},
  font=\small,
  scatter/use mapped color={draw=black},
  xlabel={年宣传费/千元},ylabel={年销售量/t},
  xlabel style={at={(0.5,-0.06)},below,font=\kaishu\small},
  ylabel style={at={(-0.08,0.5)},font=\kaishu\small},
  every outer x axis line/.append style={->,red},
  every outer y axis line/.append style={->,red},
}}
\begin{document}
    \centering
    \begin{tikzpicture}
        \begin{axis}[sdt]
            \addplot[scatter,only marks] coordinates {%
(38,500) (40,540) (44,560) (46,570) (48,580) (50,590) (56,600)};
        \end{axis}
    \end{tikzpicture}

\end{document}

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览