我是初学者(这是我在这里写的第四篇文章),我正在努力绘制一些基本的图表。我在这里找到了一些类似的文章,但它们对我没有帮助。 :( 我想绘制图表,其中是:y>= (1/x) (blue) and x<=0 (red)
。我提前感谢你的帮助。
\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:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}
\begin{document}
\begin{figure}[htpb]
\centering
\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.99,xmin=-3,xmax=5.99,
xlabel=$x$, ylabel=$y$,grid=major
]
\addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
{1/x} \drawge;
\addplot[very thick, domain=0.01:6,-] {1/x};
\addplot [draw=none, pattern=north west lines, pattern color=red!40, domain=-3:0]
{-3:0} \drawge;
\end{axis}
\end{tikzpicture}
% \caption{Prípad 1}
\end{figure}
\end{document}
答案1
一开始就运行所有这些事情会让你的生活变得有点复杂。假设你会在某个地方使用它们,我把它们留在里面,并建议
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\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:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}
\begin{document}
\begin{figure}[htpb]
\centering
\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.99,xmin=-3,xmax=5.99,
xlabel=$x$, ylabel=$y$,grid=major,samples=101
]
\addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
{1/x} \drawge;
\addplot[very thick, domain=0.01:6,-] {1/x};
\addplot[draw=none,name path=top] {6};
\addplot[draw=none,name path=bottom] {-6};
\addplot [pattern=north west lines, pattern color=red!40] fill between [of=top and bottom,soft clip={domain=-3:0}];
\end{axis}
\end{tikzpicture}
% \caption{Prípad 1}
\end{figure}
\end{document}
至于额外要求。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\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:5,0) -- (rel axis cs:5,5) -- (rel axis cs:0,5) \closedcycle}
\newcommand{\drawle}{-- (rel axis cs:5,5) -- (rel axis cs:5,0) -- (rel axis cs:0,0) \closedcycle}
\begin{document}
\begin{figure}[htpb]
\centering
\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.99,xmin=-3,xmax=5.99,
xlabel=$x$, ylabel=$y$,grid=major,samples=101
]
% \addplot [draw=none, pattern=north west lines, pattern color=blue!40, domain=0.01:6]
% {1/x};
\addplot[name path=A,very thick, domain=0.01:6,-] {1/x};
\addplot[draw=none,name path=top, domain=-3:6] {6};
\addplot[draw=none,name path=bottom] {-6};
\addplot [pattern=north west lines, pattern color=red!40] fill between [of=top and bottom,soft clip={domain=-3:0}];
\addplot [pattern=north west lines, pattern color=blue!40] fill
between [of=top and A,soft clip={domain=0:6}];
\end{axis}
\end{tikzpicture}
% \caption{Prípad 1}
\end{figure}
\end{document}