我从以下代码获得:https://tex.stackexchange.com/a/76014/10898
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={
\pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque,
grid=none}\pgfplotsdrawaxis}}}
\newcommand{\drawge}{-- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:1,1) -- (rel axis cs:1,0) -- (rel axis cs:0,0) \closedcycle}
\begin{document}
\begin{figure}[htpb]
\centering
\label{fig:p3:c1}
\begin{tikzpicture}
\begin{axis}[only axis on top,
axis line style=very thick,
axis x line=middle,
axis y line=middle,
ymin=-1,ymax=5.99,xmin=-1,xmax=5.99,
xlabel=$x$, ylabel=$y$,grid=major
]
\addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=-10:12]
{3-3/2*x} \drawge;
\addplot[very thick, domain=-1:2.5,<->] {3-3/2*x};
\end{axis}
\end{tikzpicture}
\caption{Problem 4, Part A}
\end{figure}
\end{document}
这里的问题是如何让它与以下设置一起工作:
\documentclass[dvipsnames]{article}
\usepackage{amsmath,pgfplots,tikz}
\usetikzlibrary{arrows,calc}
\begin{document}
\begin{tikzpicture}[transform shape,scale=0.6]
\draw[step=1cm,gray,very thin] (-4,-3) grid (4,5);
\draw [-latex,thick](-4,0) -- (4,0) node[right] {$x$} coordinate(x axis);
\foreach \x in {-3,-2,-1,1,2,3}
\draw (\x,0.1) -- (\x,-0.1) node [fill=white,below] {$\x$};
\foreach \y in {-2,-1,1,2,3,4}
\draw (0.1,\y) -- (-0.1,\y) node [fill=white,left] {$\y$};
\draw [-latex,thick](0,-3) -- (0,5) node[above] {$y$} coordinate(y axis);
\node at (-0.25,-0.3375) {0};
\draw[color=Cerulean,latex-latex,domain=-1.3:4] plot (\x,-3/2*\x+3);
\filldraw[OrangeRed] (0,3) circle (1.5pt) node[inner sep=2pt,right,fill=white,shift={(0.25,0)}] {$(0,3)$};
\filldraw[OrangeRed] (2,0) circle (1.5pt) node[inner sep=2pt,right,fill=white,shift={(0.25,0.35)}] {$(2,0)$};
\end{tikzpicture}
\end{document}
也就是说,我想用与pgfplots
上面最后一个例子类似的方式表示不等式的解集。这可能吗?
答案1
\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={
\pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style={thick,opaque},
grid=none}\pgfplotsdrawaxis}}}
\begin{document}
\begin{figure}[htpb]
\centering
\label{fig:p3:c1}
\begin{tikzpicture}
\begin{axis}[only axis on top,
axis line style=very thick,
axis x line=middle,
axis y line=middle,
ymin=-3,ymax=5,xmin=-4,xmax=4,
%xlabel=$x$, ylabel=$y$,
xtick={-4,-3,-2,-1,0,1,2,3,4},
ytick={-3,-2,-1,0,1,2,3,4,5},
xticklabels={,-3,-2,-1,0,1,2,3},
yticklabels={,-2,-1,0,1,2,3,4},
grid=both,clip=false
]
\draw[draw=none,pattern=north west lines, pattern color=blue!40](axis cs: -1.3,5)--(axis cs:
4,-3)--(axis cs: 4,5)--cycle;
\addplot[Cerulean,very thick,latex-latex,domain=-1.3:4] plot (\x,-3/2*\x+3);
\node[font=\small] at (axis cs: 4.25,0) {$x$};
\node[font=\small] at (axis cs: 0,5.25) {$y$};
\filldraw[OrangeRed] (axis cs: 0,3) circle (2pt) node[inner
sep=2pt,right=3mm,fill=white]
{$(0,3)$};
\filldraw[OrangeRed] (axis cs:2,0) circle (2pt) node[inner
sep=2pt,fill=white,anchor=south west] at (axis cs: 2.1,0.1) {$(2,0)$};
\end{axis}
\end{tikzpicture}
\caption{Problem 4, Part A}
\end{figure}
\end{document}
由于您希望x
和y
标签突出(我必须手动放置),我不得不使用clip=false
这反过来又阻止使用\drawge
命令。因此我选择像 Jesse 一样手动填充图案。我还调整了ticks
、ticklabels
和网格。
答案2
这是一个可行的解决方案。添加一行带有和draw
的命令,如下所示。需要来自 tikzlibrary。pattern
pattern color
patterns
如果要将坐标(0,3)、(2,0)隐藏在图案线后面,请将下面的绘制线移到代码的最后。
\draw[draw=none,pattern=north west lines, pattern color=blue!40](-1.3,5)--(4,-3)--(4,5)--cycle;
代码
\documentclass[dvipsnames]{article}
\usepackage[paper size={8cm, 8cm}]{geometry}
\usepackage{amsmath,pgfplots,tikz}
\usetikzlibrary{arrows,calc}
\usetikzlibrary{decorations.pathmorphing,patterns}
\begin{document}
\begin{tikzpicture}[transform shape,scale=0.6]
\draw[step=1cm,gray,very thin] (-4,-3) grid (4,5);
\draw [-latex,thick](-4,0) -- (4,0) node[right] {$x$} coordinate(x axis);
\foreach \x in {-3,-2,-1,1,2,3}
\draw (\x,0.1) -- (\x,-0.1) node [fill=white,below] {$\x$};
\foreach \y in {-2,-1,1,2,3,4}
\draw (0.1,\y) -- (-0.1,\y) node [fill=white,left] {$\y$};
\draw [-latex,thick](0,-3) -- (0,5) node[above] {$y$} coordinate(y axis);
\node at (-0.25,-0.3375) {0};
\draw[draw=none,pattern=north west lines, pattern color=blue!40](-1.3,5)--(4,-3)--(4,5)--cycle;
\draw[color=Cerulean,latex-latex,domain=-1.3:4] plot (\x,-3/2*\x+3);
\filldraw[OrangeRed] (0,3) circle (1.5pt) node[inner sep=2pt,right,fill=white,shift={(0.25,0)}] {$(0,3)$};
\filldraw[OrangeRed] (2,0) circle (1.5pt) node[inner sep=2pt,right,fill=white,shift={(0.25,0.35)}] {$(2,0)$};
\end{tikzpicture}
\end{document}