两个坐标之间的填充颜色

两个坐标之间的填充颜色

问题:如何填充 O(0,0) 和 A(1,1) 之间的区域?

这是我目前所做的:

\documentclass[12pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usepgfplotslibrary{fillbetween}
\pagestyle{empty}
\begin{document}
\definecolor{sexdts}{rgb}{0.1803921568627451,0.49019607843137253,0.19607843137254902}
\definecolor{wrwrwr}{rgb}{0.3803921568627451,0.3803921568627451,0.3803921568627451}
\begin{center}
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\begin{axis}[
x=2.5cm,y=2.5cm,
axis lines=middle,
axis line style={stealth-stealth},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
xtick={-2,...,2},
ytick={-2,...,2},]
\draw [samples=50,rotate around={0:(0,0)},xshift=0cm,yshift=0cm,line width=0.8pt,color=wrwrwr,domain=-1.5:1.5,<->,name path=C1] plot (\x,{(\x)^2/2.0/0.5});
\draw [line width=0.8pt,color=sexdts,domain=-2.0:2,<->] plot(\x,{(-0--1*\x)/1});
\begin{scriptsize}
\draw[color=black] (-1.5,2.5) node {$y=x^2$};
\draw[color=black] (-1.5,-2) node {$y=x$};
\draw [fill=black] (0,0) circle (1.8pt);
\draw[color=black] (0.40,-0.25) node [rotate=00]{O(0 , 0)};
\draw [fill=black] (1,1) circle (1.8pt);
\draw[color=black] (1.40,1.00) node [rotate=00]{A(1 , 1)};
\end{scriptsize}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案1

另一种可能性是\tikzfillbetween,但是您的代码是否来自某种自动工具?

因为:

  • 你无用地加载了pgf(它已经被加载了tikz
  • 环境scriptsize内不执行任何操作,请tikzpicture使用选项scale=...
  • 你的直线和抛物线公式太复杂了(我已经修改过了)
  • 你的颜色定义很奇怪。

\documentclass[12pt]{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usepgfplotslibrary{fillbetween}
\pagestyle{empty}
\begin{document}
\definecolor{sexdts}{rgb}{0.1803921568627451,0.49019607843137253,0.19607843137254902}
\definecolor{wrwrwr}{rgb}{0.3803921568627451,0.3803921568627451,0.3803921568627451}
\begin{center}
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45, scale=.9]
\begin{axis}[set layers,
x=2.5cm,y=2.5cm,
axis lines=middle,
axis line style={stealth-stealth},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
xtick={-2,...,2},
ytick={-2,...,2},]
\draw [samples=50,rotate around={0:(0,0)},xshift=0cm,yshift=0cm,line width=0.8pt,color=wrwrwr,domain=-1.5:1.5,<->,name path=C1] plot (\x,{(\x)^2});
\draw [line width=0.8pt,color=sexdts,domain=-2.0:2,<->, name path=retta] plot(\x,{\x});
\draw[color=black] (-1.5,2.5) node {$y=x^2$};
\draw[color=black] (-1.5,-2) node {$y=x$};
\draw [fill=black] (0,0) circle (1.8pt);
\draw[color=black] (0.40,-0.25) node [rotate=00]{O(0 , 0)};
\draw [fill=black] (1,1) circle (1.8pt);
\draw[color=black] (1.40,1.00) node [rotate=00]{A(1 , 1)};
\tikzfillbetween[
    on layer=axis background,
    of=C1 and retta,
    soft clip={domain=0:1}
    ] {red};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案2

使用( )fillbetween库,您可以为两条曲线之间的部分着色。为此,使用将着色限制在特定区域。pgfplots\usepgfplotslibrary{fillbetween}soft clip

该区域可以通过诸如矩形的路径来划定:

\plot [orange] fill between [of=C1 and C2,
        soft clip={(0,0) rectangle(1,1)}
        ];

或者通过函数的定义域的区间:

\plot [orange] fill between [of=C1 and C2,
        soft clip={domain=0:1}
        ];

阴谋

\documentclass[12pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usepgfplotslibrary{fillbetween}
\pagestyle{empty}
\begin{document}
\definecolor{sexdts}{rgb}{0.1803921568627451,0.49019607843137253,0.19607843137254902}
\definecolor{wrwrwr}{rgb}{0.3803921568627451,0.3803921568627451,0.3803921568627451}
\begin{center}
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\begin{axis}[
x=2.5cm,y=2.5cm,
axis lines=middle,
axis line style={stealth-stealth},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
xtick={-2,...,2},
ytick={-2,...,2},]
\draw [samples=50,rotate around={0:(0,0)},xshift=0cm,yshift=0cm,line width=0.8pt,color=wrwrwr,domain=-1.5:1.5,<->,name path=C1] plot (\x,{(\x)^2/2.0/0.5});
\draw [line width=0.8pt,color=sexdts,domain=-2.0:2,<->,name path=C2] plot(\x,{(-0--1*\x)/1});
\plot [orange] fill between [of=C1 and C2,
    soft clip={(0,0) rectangle(1,1)}
    %soft clip={domain=0:1}
    ];
\begin{scriptsize}
\draw[color=black] (-1.5,2.5) node {$y=x^2$};
\draw[color=black] (-1.5,-2) node {$y=x$};
\draw [fill=black] (0,0) circle (1.8pt);
\draw[color=black] (0.40,-0.25) node [rotate=00]{O(0 , 0)};
\draw [fill=black] (1,1) circle (1.8pt);
\draw[color=black] (1.40,1.00) node [rotate=00]{A(1 , 1)};
\end{scriptsize}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

相关内容