
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
這使用普通的 TikZ 和intersections
函式庫作為交點。注意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
。
- 圓形框架很簡單
axis background/.style={rounded corners=4mm,draw=blue}
, - 擺脫箭頭就像這樣簡單
every inner x axis line/.append style={-},every inner y axis line/.append style={-}
, - 陰影可以用 來完成
fillbetween
。 - 這些公式是簡單的標籤/引腳。
- 可以根據 等值自動附加最大值和
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 博士的答案(因為它使用更簡單的圖表)。在 TikZ 庫的幫助下fit
:
\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
編輯:在節點外部添加坐標標籤(它們是\Large
和red
,您很容易看到,它們在圖片上,您可以輕鬆更改此設定)
答案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}