如何绘制多项式阶的一维网格?

如何绘制多项式阶的一维网格?

我想画出近似解和网格(包括多项式阶)。我想到的例子如下:(来自http://dx.doi.org/10.1201/97814200116921

我的尝试产生了以下 pgfplots 代码:

\documentclass[border={2pt}]{standalone}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{patchplots}


\begin{document}

\newcommand{\plotwidth}{10cm}
\newcommand{\plotheight}{7cm}
% this is the data of the solution that will be provided with a file
\pgfplotstableread{
    X       My_solution
   0.0       0.000
  0.2       0.180
  0.5       0.159
  0.7       0.107
  1.0        0.000
}\solution

% this is the data of the mesh that will also be provided with a file
\pgfplotstableread{
x y  order
%
0    0  1
0.2  0  1
0.2  1  1
0 1     1
%
0.2 0  2
0.5 0  2
0.5 1  2
0.2 1  2
%
0.5 0  3
0.7 0  3
0.7 1  3
0.5 1  3
%
0.7  0  5
1    0  5
1    1  5
0.7  1  5
%
}\mesh


\begin{tikzpicture}
\begin{axis}[name=master,width=\plotwidth,height=\plotheight,]
% draw the solution in the master axis
\addplot[solid, line width=1pt] table[x=X, y=My_solution] {\solution};
\end{axis}

% this axis contains the mesh (with orders)
\begin{axis}[
colorbar right,
  colorbar style={
    at={(master.right of north east)},
    height=\plotheight/1.5,
} ,
width=\plotwidth,
%place the colorbar with repesct to the master axis
at={(master.above north west)},
enlargelimits=false,
enlarge x limits=true,
axis lines=none,
height=2cm,
ticks=none,
 ]
 % draw the patches that will represent the mesh
    \addplot [patch, patch type=rectangle, point meta=explicit,line width=1pt,faceted color=black]  table[meta=order] {\mesh};

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

2

结果与预期相差不大,但仍存在未解决的问题:

  1. 我使用补丁来绘制网格,但我不确定这是否是最好的方法。此外,我希望该轴的高度与默认宽度相同,而不是 colobar 宽度:\pgfkeysvalueof{/pgfplots/colorbar/width},但编译器抱怨它提供的图太小。这是一个小问题。
  2. 给出颜色图例的颜色条很难正确处理:

    • 我需要它是离散的,并且标签位于颜色单元的中间。为此,我尝试了colormap access=directcolorbar sampled以及,colorbar style={ytick=data,y tick label as interval=true,}但它们都没有提供正确的输出。

    • 我需要颜色条的高度与主轴的大小相关,最好是相同的大小。以下命令不起作用\pgfkeysvalueof{/pgfplots/master axis height}

欢迎任何帮助。

谢谢。

答案1

这是否更接近您的要求?我只是从您的示例中修改了一些参数,但作为注释,这太长了。最重要的修改与仅应用于绘图区域的scale only axis修改有关,这使得s 的大小调整更容易。with={}height={}colorbar

\documentclass[border={2pt}]{standalone}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{patchplots}


\begin{document}

\newcommand{\plotwidth}{10cm}
\newcommand{\plotheight}{7cm}
% this is the data of the solution that will be provided with a file
\pgfplotstableread{
    X       My_solution
   0.0       0.000
  0.2       0.180
  0.5       0.159
  0.7       0.107
  1.0        0.000
}\solution

% this is the data of the mesh that will also be provided with a file
\pgfplotstableread{
x y  order
%
0    0  1
0.2  0  1
0.2  1  1
0 1     1
%
0.2 0  2
0.5 0  2
0.5 1  2
0.2 1  2
%
0.5 0  3
0.7 0  3
0.7 1  3
0.5 1  3
%
0.7  0  5
1    0  5
1    1  5
0.7  1  5
%
}\mesh


\begin{tikzpicture}
\begin{axis}[
enlarge x limits=false,
scale only axis,name=master,width=\plotwidth,height=\plotheight]
% draw the solution in the master axis
\addplot[solid, line width=1pt] table[x=X, y=My_solution] {\solution};
\end{axis}

% this axis contains the mesh (with orders)
\begin{axis}[
scale only axis,
colorbar right,
  colorbar style={
    at={(master.right of north east)},
    height=\plotheight,
    ylabel={Colorbar Label},
    ylabel style={yshift=6ex},
} ,
width=\plotwidth,
%place the colorbar with repesct to the master axis
at={(master.north west)},
anchor=south west,
%enlargelimits=false,
enlarge x limits=false,
axis lines=none,
height=5mm,
ticks=none,
]
 % draw the patches that will represent the mesh
    \addplot [patch, patch type=rectangle, point meta=explicit,faceted color=black]  table[meta=order] {\mesh};

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

在此处输入图片描述

编辑:略有改善

\documentclass[border={2pt}]{standalone}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{patchplots}


\begin{document}

\newcommand{\plotwidth}{10cm}
\newcommand{\plotheight}{7cm}
% this is the data of the solution that will be provided with a file
\pgfplotstableread{
    X       My_solution
   0.0       0.000
  0.2       0.180
  0.5       0.159
  0.7       0.107
  1.0        0.000
}\solution

% this is the data of the mesh that will also be provided with a file
\pgfplotstableread{
x y  order
%
0    0  1
0.2  0  1
0.2  1  1
0 1     1
%
0.2 0  2
0.5 0  2
0.5 1  2
0.2 1  2
%
0.5 0  3
0.7 0  3
0.7 1  3
0.5 1  3
%
0.7  0  5
1    0  5
1    1  5
0.7  1  5
%
}\mesh


\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=false,
    scale only axis,
    name=master,
    width=\plotwidth,
    height=\plotheight
]
% draw the solution in the master axis
\addplot[solid, line width=1pt] table[x=X, y=My_solution] {\solution};
\end{axis}

% this axis contains the mesh (with orders)
\begin{axis}[
    scale only axis,
    colormap={example}{samples of colormap={6,sample for=const}},
    colormap access = const,
    colorbar right,
    colorbar style={
        samples = 6,
        at={(master.right of north east)},
        height=\plotheight,
        ylabel={Colorbar Label},
        ylabel style={yshift=6ex},
        ytick={1,2,3,4,5},
        yticklabel style = {yshift=-5ex},
        point meta min = 0,
        point meta max = 5,
    },
    colorbar sampled,
    width=\plotwidth,
    %place the colorbar with repesct to the master axis
    at={(master.north west)},
    anchor=south west,
    enlarge x limits=false,
    axis lines=none,
    height=5mm,
    ticks=none,
]
% draw the patches that will represent the mesh
    \addplot [patch, patch type=rectangle, point meta=explicit,
              faceted color=black,
              colormap={example}{samples of colormap={6,sample for=const}},
              colormap access = const,
              ]  table[meta=order] {\mesh};

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

在此处输入图片描述

相关内容