如何使用存储在数组中的值通过 pgfplots 进行绘图?

如何使用存储在数组中的值通过 pgfplots 进行绘图?

是否可以根据存储在数组中的值进行绘图。

\documentclass{article}
\usepackage{xparse,fp,xfp}
\usepackage{pgfplots}
\ExplSyntaxOn

\NewDocumentCommand{\newarray}{mO{100}}
 {
  \fparray_new:cn { g_sandu_#1_fparray } { #2 }
  \cs_new:cpn { #1 } ##1
   {
    \fparray_item:cn { g_sandu_#1_fparray } { ##1 }
   }
 }

\NewDocumentCommand{\readarray}{mm}
 {
  \seq_set_split:Nnn \l__sandu_temp_seq { & } { #2 }
  \int_step_inline:nn { \seq_count:N \l__sandu_temp_seq }
   {
    \fparray_gset:cne { g_sandu_#1_fparray } { ##1 }
     { \seq_item:Nn \l__sandu_temp_seq { ##1 } }
   }
 }

\NewDocumentCommand{\setarrayitem}{mmm}
 {
  \fparray_gset:cne { g_sandu_#1_fparray } { #2 } { #3 }
 }

\cs_generate_variant:Nn \fparray_new:Nn { c }
\cs_generate_variant:Nn \fparray_item:Nn { c }
\cs_generate_variant:Nn \fparray_gset:Nnn { cnn, cne }

\ExplSyntaxOff

\FPset\rowN{3}

\newarray{xx}
\readarray{xx}{1&2&3&4&5}

\newarray{yy}
\readarray{yy}{6&7&8&9&10}

\begin{document}

\begin{tabular}{ccc}
No & xx & yy   \\ \hline
1 & \xx{1} & \yy{1}  \\
2 & \xx{2} & \yy{2}  \\
3 & \xx{3} & \yy{3}  \\
4 & \xx{4} & \yy{4}  \\
5 & \xx{5} & \yy{5}  \\ \hline
\end{tabular}

\bigskip

\begin{tikzpicture}
\begin{axis}[
width=0.8\linewidth, % Scale the plot to \linewidth
grid=major, % Display a grid
grid style={dashed,orange!30}, % Set the style
y axis line style=blue!75!black,
xlabel= xx, % Set the labels
ylabel= yy,
]

\foreach \k in {0, ..., \rowN} 
{
\addplot[smooth,orange] coordinates {
({\xx{\k}},{\yy{\k}})
};
}
\addplot[smooth,color=red,mark=x,thick] coordinates {
(1,6)
(2,7)
(3,8)
(4,9)
(5,10)
};
\end{axis}

\end{tikzpicture}
\end{document}

答案1

众多可能性之一。

\documentclass{article}
\usepackage{xparse,fp,xfp}
\usepackage{pgfplots}
\ExplSyntaxOn

\NewDocumentCommand{\newarray}{mO{100}}
 {
  \fparray_new:cn { g_sandu_#1_fparray } { #2 }
  \cs_new:cpn { #1 } ##1
   {
    \fparray_item:cn { g_sandu_#1_fparray } { ##1 }
   }
 }

\NewDocumentCommand{\readarray}{mm}
 {
  \seq_set_split:Nnn \l__sandu_temp_seq { & } { #2 }
  \int_step_inline:nn { \seq_count:N \l__sandu_temp_seq }
   {
    \fparray_gset:cne { g_sandu_#1_fparray } { ##1 }
     { \seq_item:Nn \l__sandu_temp_seq { ##1 } }
   }
 }

\NewDocumentCommand{\setarrayitem}{mmm}
 {
  \fparray_gset:cne { g_sandu_#1_fparray } { #2 } { #3 }
 }

\cs_generate_variant:Nn \fparray_new:Nn { c }
\cs_generate_variant:Nn \fparray_item:Nn { c }
\cs_generate_variant:Nn \fparray_gset:Nnn { cnn, cne }

\ExplSyntaxOff

\FPset\rowN{3}

\newarray{xx}
\readarray{xx}{1&2&3&4&5}

\newarray{yy}
\readarray{yy}{6&7&8&9&10}

\begin{document}

\begin{tabular}{ccc}
No & xx & yy   \\ \hline
1 & \xx{1} & \yy{1}  \\
2 & \xx{2} & \yy{2}  \\
3 & \xx{3} & \yy{3}  \\
4 & \xx{4} & \yy{4}  \\
5 & \xx{5} & \yy{5}  \\ \hline
\end{tabular}

\bigskip

\begin{tikzpicture}
\foreach \k in {1, ..., \rowN} 
{\ifnum\k=1
\xdef\myLst{({\xx{\k}},{\yy{\k}})}
\else
\xdef\myLst{\myLst ({\xx{\k}},{\yy{\k}})}
\fi}
\begin{axis}[
width=0.8\linewidth, % Scale the plot to \linewidth
grid=major, % Display a grid
grid style={dashed,orange!30}, % Set the style
y axis line style=blue!75!black,
xlabel= xx, % Set the labels
ylabel= yy,
]
\addplot[red,mark=x,thick] coordinates {\myLst};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容