pgfplots:图表框的圆角

pgfplots:图表框的圆角

pgfplots我们考虑使用外部网格为矩形的小代码,就像用这个 MWE 创建的图像一样:

在此处输入图片描述

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}

但是这个矩形网格是固定的,还是可以获得像这样的图像,其边缘是圆角的并且可以着色,并且您可以在圆角矩形内绘制不同的图形?

在此处输入图片描述

答案1

这使用带有intersections库的纯 TikZ 来获取交点。请注意,scope环境可以嵌套,\clip可以使用多个 s 来填充。如果您想要具有更多圆角的矩形,则只需[rounded corners=3mm]在 的定义中使用 即可\bb

我相信,pgfplots只要在环境中有合适的选项,也可以轻松绘制此图axis。不过,纯 TikZ 让我更舒服。

在此处输入图片描述

\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[scale=2]
\def\bb{[rounded corners] (-1,-1) rectangle (2,1.5)}
\def\curveA{plot[domain=-1:2,smooth,samples=100]  (\x,{\x/(sqrt(1+\x*\x))})}
\def\curveB{plot[domain=-1:2,smooth,samples=100](\x,{pow(\x,4)-\x})}
\begin{scope} \clip \bb;
\begin{scope} 
\clip \curveA|-cycle;
\clip \curveB--cycle;
\fill[cyan!20] \bb;
\end{scope}
\draw (-1,0)--(2,0) (0,-1)--(0,1.5);
\draw[magenta,thick,name path=A] \curveA;
\draw[cyan,thick,name path=B] \curveB;
\path[name intersections={of=A and B}] (intersection-2) node[cyan,rectangle,minimum size=2mm,draw,thick]{};
\end{scope}
\draw[cyan] \bb;
\foreach \i in {-.5,0,...,1.5} \draw (\i,.05)--(\i,-.05);
\foreach \j in {-.5,0,...,1} \draw (.05,\j)--(-.05,\j);
\path
(-1,0) node[left]{$-1$}
(2,0) node[right]{$2$}
(0,-1) node[below]{$-1$}
(0,1.5) node[above]{$1.5$}
(1.2,-.5) node{$y=x^4-x$}
(.6,1.2) node (N) {$y=\dfrac{x}{\sqrt{x^2+1}}$};
\draw[-stealth] (N)--+(-60:.6);
\end{tikzpicture}
\end{document}

答案2

所有这些都可以用简单的工具完成pgfplots

  1. 圆形框架非常简单axis background/.style={rounded corners=4mm,draw=blue}
  2. 去掉箭头很简单every inner x axis line/.append style={-},every inner y axis line/.append style={-}
  3. 可以使用 进行阴影处理fillbetween
  4. 公式是简单的标签/大头针。
  5. 可以根据 等值自动附加最大值和xmin最小值after end axis/.code

为了您的方便,我以一种称为的风格收集了其中大部分内容Sebastiano

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{Sebastiano/.style={xticklabel=\empty,yticklabel=\empty,
    axis lines = center,
    every inner x axis line/.append style={-},
    every inner y axis line/.append style={-},
    axis background/.style={rounded corners=4mm,draw=blue},
    before end axis/.code={\path (0,0) coordinate (O);},
    after end axis/.code={
    \path
     (current axis.east|-O) node[right]
      {\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmax}}}
     (current axis.west|-O) node[left]
      {\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmin}}}
     (current axis.north-|O) node[above]
      {\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymax}}}
     (current axis.south-|O) node[below]
      {\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymin}}};
    }
}}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
%
\begin{axis}[Sebastiano,width =12cm,
    xmin = -1,xmax = 2,
    ymin = -1,ymax = 1.5,
    domain=-1:2,smooth]
    \addplot[name path=A,color=magenta,thick]  {x/sqrt(1+x*x)}
    coordinate[pos=0.7,pin={[black,pin edge={stealth-,thick}]100:{$\displaystyle y=\frac{x}{\sqrt{1+x^2}}$}}](pA);
    \addplot[name path=B,color=cyan,thick]  {x^4-x}
    coordinate[pos=0.19,label={[black]below right:{$\displaystyle y=x^4-x$}}](pB);
    \addplot fill between [of=A and B,
        split,
        every segment no 0/.style={fill=none},
        every segment no 1/.style={cyan,opacity=50},
        every segment no 2/.style={fill=none},
        ];
\end{axis}
%
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

作为起点,我使用了 Manuel Kuehner 博士的答案(因为它使用了更简单的图表)。借助fitTikZ 库:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{fit}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[name=PLOT,
    width = 80mm,
    height= 60mm,
    xmin = -1.2,
    xmax =  2.2,
    ymin = -1.3,
    ymax =  4.3,
    axis lines = center,
    scale only axis
    ]
    \addplot[
        domain = 0:2,
        red,
        line width = 1pt
        ]
        {x^2};
\coordinate (O) at (0,0);
\end{axis}
%
\node (f) [draw=blue, thick, rounded corners = 5mm, 
           inner sep=0pt, fit=(PLOT)] {};
\path   (O-|f.west)  node[lbl, left]  {$-1$}  (O-|f.east)  node[lbl,right] {$2$}
        (O|-f.north) node[lbl,above]  {$1.5$} (O|-f.south) node[lbl,below] {$-1$};
    \end{tikzpicture}
\end{document}

f编辑:在节点外部添加坐标标签(它们是\Largered,您可以轻松看到,它们在图片上,您可以轻松更改这些设置)

在此处输入图片描述

答案4

  • 只是一个想法/开始(现在去睡觉)。
  • 将两个图块叠放在一起。
  • 它们具有相同的尺寸,但轴选项不同。
  • scale only axis很重要。

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
%
\begin{axis}[
    width = 80mm,
    height = 60mm,
    xmin = -1.2,
    xmax = 2.2,
    ymin = -1.3,
    ymax = 4.3,
    axis x line = center,
    axis y line = center,
    %axis line style = {rounded corners = 4mm},
    scale only axis
    ]
    \addplot[
        domain = 0:2, 
        red, 
        line width = 1pt
        ] 
        {x^2};
\end{axis}
%
\begin{axis}[
    width = 80mm,
    height = 60mm,
    xmin = -1.2,
    xmax = 2.2,
    ymin = -1.3,
    ymax = 4.3,
    axis line style = {rounded corners = 5mm, blue},
    scale only axis,
    ticks = none
    ]
\end{axis}
%
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容