pgfplots - 如何为所有图表定义一个循环列表(或尽可能最少的图表)?

pgfplots - 如何为所有图表定义一个循环列表(或尽可能最少的图表)?

由于更改列表中的颜色后需要进行大量维护,我尝试只定义一个循环列表,这样就不必再更新所有列表了。总的来说,这会花费大量令人烦恼的时间,我宁愿把这些时间花在其他事情上。

我已经解决了 MWE,这个问题更像是一个安慰。但如果其他人正在寻找类似的解决方案,这个问题可能会对他们有所帮助。我想它可能有一个缺点:字体也是彩色的。

那么:这是定义一个用于 pgfplots 的单循环列表的正确方法吗?

图片

在此处输入图片描述

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
amsmath,
tikz,
pgfplots,
pgfplotstable,
}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\pgfplotstableread[col sep=comma]{
Process,ValueA,ValueB
1,66,70
2,66,40
3,24,20
4,52,60
5,64,30
6,37,30
}{\tableabcdef}

\pgfplotscreateplotcyclelist{mycolorlisttest}{%
blue!60!black,fill=blue!60!black,every mark/.append style={fill=blue!60!black},mark=*\\%1
red!70!black,fill=red!70!black,every mark/.append style={fill=red!70!black},mark=*\\%2
green!70!black,fill=green!70!black,every mark/.append style={fill=green!70!black},mark=*\\%3
gray,fill=gray,fill=gray,fill=gray,every mark/.append style={fill=gray},mark=*\\%4
orange,fill=orange,every mark/.append style={fill=orange},mark=*\\%5
brown!80!black,fill=brown!80!black,every mark/.append style={fill=brown!80!black},mark=*\\%6
green,fill=green,every mark/.append style={fill=green},mark=*\\%7
violet!80!black,fill=violet!80!black,every mark/.append style={fill=violet!80!black},mark=*\\%8
black,fill=black,every mark/.append style={fill=black},mark=*\\%9
teal,fill=teal,every mark/.append style={fill=teal},mark=*\\%10
magenta!70!black,fill=magenta!70!black,every mark/.append style={fill=magenta!70!black},mark=*\\%11
yellow!90!black,fill=yellow!90!black,every mark/.append style={fill=yellow!90!black},mark=*\\%12
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}[
xlabel={Force in N},
ylabel={Process},
%
xbar,
%
xmin=0,
%
enlarge x limits={rel={0.2}, upper},
%
ytick=data,
nodes near coords,
nodes near coords align={right=3pt},
every node near coord/.append style={fill=white, font=\footnotesize, inner sep=1pt},
%
cycle list name=mycolorlisttest,
no markers
]
\addplot+ table [x=ValueA, y=Process] {\tableabcdef};
\addplot+ table [x=ValueB, y=Process] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

解决方案

\documentclass[
a4paper
]{scrartcl}

\usepackage{
amsmath,
tikz,
pgfplots,
pgfplotstable,
}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\pgfplotstableread[col sep=comma]{
Process,ValueA,ValueB
1,66,70
2,66,40
3,24,20
4,52,60
5,64,30
6,37,30
}{\tableabcdef}

\tikzset{
meinemarkfarben/.style={
mark=*,
draw=#1,
fill=#1,
every mark/.append style={fill=#1}}
}

\pgfplotscreateplotcyclelist{aaaaaaa}{%
meinemarkfarben={yellow!60!black}\\%1
meinemarkfarben={red!70!black}\\%2
meinemarkfarben={green!70!black}\\%3
meinemarkfarben={gray}\\%4
meinemarkfarben={orange}\\%5
meinemarkfarben={brown!80!black}\\%6
meinemarkfarben={green}\\%7
meinemarkfarben={violet!80!black}\\%8
meinemarkfarben={black}\\%9
meinemarkfarben={teal}\\%10
meinemarkfarben={magenta!70!black}\\%11
meinemarkfarben={yellow!90!black}\\%12
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}[
xlabel={Force in N},
ylabel={Process},
%
xbar,
%
xmin=0,
%
enlarge x limits={rel={0.2}, upper},
%
ytick=data,
nodes near coords,
nodes near coords align={right=3pt},
every node near coord/.append style={fill=white, font=\footnotesize, inner sep=1pt},
%
cycle list name=aaaaaaa,
no markers
]
\addplot+ table [x=ValueA, y=Process] {\tableabcdef};
\addplot+ table [x=ValueB, y=Process] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

解决方案图片

在此处输入图片描述

答案1

这应该通过将其包装在自定义样式中来实现。我不太确定你想要什么?

做这个:

\tikzset{
  my mark set/.style={
    draw=#1,
    fill=#1,
    every mark/.append style={fill=#1}
  }
}

然后你可以这样做:

\pgfplotscreateplotcyclelist{mycolorlisttest}{%
  my mark set=blue!60!black,mark=*\\%1
  my mark set=red!70!black,mark=*\\%2
  ...
}

当然,您可以使用带有参数的特殊设置,例如:

\tikzset{
  my mark set/.style 2 args={
    mark=#1,
    draw=#2,
    fill=#2,
    every mark/.append style={fill=#2}
  }
}

然后执行以下操作:

\pgfplotscreateplotcyclelist{mycolorlisttest}{%
  my mark set={*}{blue!60!black}\\%1
  my mark set={*}{red!70!black}\\%2
  ...
}

答案2

毕竟,它可以通过宏来完成,但与使用 /.style 相比,并没有真正的优势

\documentclass[a4paper]{scrartcl}

\usepackage{amsmath,tikz,pgfplots,pgfplotstable}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\pgfplotstableread[col sep=comma]{
Process,ValueA,ValueB
1,66,70
2,66,40
3,24,20
4,52,60
5,64,30
6,37,30
}{\tableabcdef}

\newcommand*{\cycleitem}[1]% #1 = color
{#1,fill=#1,every mark/.append style={fill=#1},mark=*}

\newcommand{\cyclelist}{% list stored as a macro
{\cycleitem{blue!60!black}},%1
{\cycleitem{red!70!black}},%2
{\cycleitem{green!70!black}},%3
{\cycleitem{gray}},%4
{\cycleitem{orange}},%5
{\cycleitem{brown!80!black}},%6
{\cycleitem{green}},%7
{\cycleitem{violet!80!black}},%8
{\cycleitem{black}},%9
{\cycleitem{teal}},%10
{\cycleitem{magenta!70!black}},%11
{\cycleitem{yellow!90!black}}%12
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]

\begin{axis}[
xlabel={Force in N},
ylabel={Process},
%
xbar,
%
xmin=0,
%
enlarge x limits={rel={0.2}, upper},
%
ytick=data,
nodes near coords,
nodes near coords align={right=3pt},
every node near coord/.append style={fill=white, font=\footnotesize, inner sep=1pt},
%
cycle list/.expanded={\cyclelist},% uses macro instead of name
no markers
]
\addplot+ table [x=ValueA, y=Process] {\tableabcdef};
\addplot+ table [x=ValueB, y=Process] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

相关内容