pgfplots 图例名称不起作用

pgfplots 图例名称不起作用

我想使用legend to name功能在外部显示图例。我在 tikz 图片中有两个图表,它们来自此问题

以下是尝试修改并使图外的图例起作用的代码。但是,它给出了编译错误I do not know the key '/tikz/myplot',这可能是因为我在图例中使用了某种样式?

有人可以给我一些指点来解决这个问题吗?

\documentclass{article}
\usepackage{mathptmx}
\usepackage{pgf}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes, positioning,shadows,shadows.blur,shapes.geometric,decorations.pathmorphing,patterns}
\usepackage{color,filecontents}
\begin{document}

\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}
\definecolor{predictedCol}{HTML}{266A2E}

\begin{filecontents*}{plot1.csv}
Number      Step1       Step2       Step3           Step4       Step5_6         Step7           Step8       Predicted
0           50          138         2025137         1400        15859           1358            50          11788769
1           50          894         2088724         1898        14662           2035            50          7564508
2           50          1610        3482495         1405        11490           1302            50          5970268
3           50          871         2089859         898         5021            569             50          7864363
4           50          138         3470704         1405        15888           1302            50          11788769
5           50          871         3481357         1909        11110           1324            50          7560008
6           50          871         2089855         2476        16015           885             50          7878218
7           50          1375        4875299         1903        17401           1258            50          11791029
8           50          877         2786201         1405        10704           1358            50          7871713
9           50          894         2733003         898         5027            569             50          7864363
10          50          138         3481371         1400        15882           1302            50          11788769
11          50          894         2088720         1405        18347           1302            50          7566933
\end{filecontents*}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
myplotShadow/.style={blur shadow={shadow blur radius=0.25pt,shadow yshift=-0.5pt, shadow xshift=0.5pt}},
myplot/.style={draw=none,area legend,draw opacity=0,myplotShadow},
   ybar stacked,
width=10cm,height=5cm,
    bar width=8pt,
    enlarge x limits=0.15,
    ylabel={clock cycles},
   xlabel={tile mapping(VLD, IQ/IDCT, CC)},
  ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
    xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
    xtick=data,
   ytick={0,2000000,4000000,6000000,8000000,10000000,12000000},
   xmin=1,
   xmax=10,
   ymin=0,
   ymax=12000000,
axis line style={draw=none},
legend columns=-1,
legend entries={step1,step2, step3, step4, step5+6, step7, step8,predicted},
legend to name=named,
        legend style={draw=none,
       legend cell align=left,
       at={(1.30,1.05)},  },
    ]   

\addplot [myplot,fill=step1Col] table[x=Number,y=Step1] {plot1.csv};
\addplot [myplot,fill=step2Col] table[x=Number,y=Step2] {plot1.csv};
\addplot [myplot,fill=step3Col] table[x=Number,y=Step3] {plot1.csv};
\addplot [myplot,fill=step4Col] table[x=Number,y=Step4] {plot1.csv};
\addplot [myplot,fill=step5_6Col] table[x=Number,y=Step5_6] {plot1.csv};
\addplot [myplot,fill=step7Col] table[x=Number,y=Step7] {plot1.csv};
\addplot [myplot,fill=step8Col,] table[x=Number,y=Step8] {plot1.csv};

%\legend{step1,step2, step3, step4, step5+6, step7, step8}
\end{axis}

\begin{axis}[
width=10cm,
height=5cm,
 xmin=1,
   xmax=10,
   ymin=0,
   ymax=12000000,
    enlarge x limits=0.15,
axis line style={draw=none},
xtick=data,
ytick={},
xticklabels ={},
yticklabels ={},
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
ticks=none,
%legend style={draw=none, at={(1.30,0.00)}}
]   

\addplot [only marks, mark=square*,draw opacity=0,    % for the last plot  
fill=predictedCol,legend image post style={xshift=0.25cm}  %for correct legend position
] table[x=Number,y=Predicted] {plot1.csv};
%\legend{predicted}
\end{axis}
\end{tikzpicture}
\\
\ref{named}
\end{center}
\end{document}

谢谢 !

答案1

在此处输入图片描述

\documentclass{article}
\usepackage{mathptmx}
\usepackage{pgf}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes, positioning,shadows,shadows.blur,shapes.geometric,decorations.pathmorphing,patterns}
\usepackage{color,filecontents}
\begin{document}

\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}
\definecolor{predictedCol}{HTML}{266A2E}

\begin{filecontents*}{plot1.csv}
Number      Step1       Step2       Step3           Step4       Step5_6         Step7           Step8       Predicted
0           50          138         2025137         1400        15859           1358            50          11788769
1           50          894         2088724         1898        14662           2035            50          7564508
2           50          1610        3482495         1405        11490           1302            50          5970268
3           50          871         2089859         898         5021            569             50          7864363
4           50          138         3470704         1405        15888           1302            50          11788769
5           50          871         3481357         1909        11110           1324            50          7560008
6           50          871         2089855         2476        16015           885             50          7878218
7           50          1375        4875299         1903        17401           1258            50          11791029
8           50          877         2786201         1405        10704           1358            50          7871713
9           50          894         2733003         898         5027            569             50          7864363
10          50          138         3481371         1400        15882           1302            50          11788769
11          50          894         2088720         1405        18347           1302            50          7566933
\end{filecontents*}

% #### Put this in \pgfplotsset ####
\pgfplotsset{myplotShadow/.style={blur shadow={shadow blur radius=0.25pt,shadow yshift=-0.5pt, shadow xshift=0.5pt}},
myplot/.style={draw=none,area legend,draw opacity=0,myplotShadow}}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
   ybar stacked,
width=10cm,height=5cm,
    bar width=8pt,
    enlarge x limits=0.15,
    ylabel={clock cycles},
   xlabel={tile mapping(VLD, IQ/IDCT, CC)},
  ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
    xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
    xtick=data,
   ytick={0,2000000,4000000,6000000,8000000,10000000,12000000},
   xmin=1,
   xmax=10,
   ymin=0,
   ymax=12000000,
axis line style={draw=none},
legend columns=-1,
legend entries={step1,step2, step3, step4, step5+6, step7, step8,predicted},
legend to name=named,
        %legend style={draw=none,
       %legend cell align=left,
       %at={(1.30,1.05)},  },
    ]   

\addplot [myplot,fill=step1Col] table[x=Number,y=Step1] {plot1.csv};
\addplot [myplot,fill=step2Col] table[x=Number,y=Step2] {plot1.csv};
\addplot [myplot,fill=step3Col] table[x=Number,y=Step3] {plot1.csv};
\addplot [myplot,fill=step4Col] table[x=Number,y=Step4] {plot1.csv};
\addplot [myplot,fill=step5_6Col] table[x=Number,y=Step5_6] {plot1.csv};
\addplot [myplot,fill=step7Col] table[x=Number,y=Step7] {plot1.csv};
\addplot [myplot,fill=step8Col,] table[x=Number,y=Step8] {plot1.csv};

%\legend{step1,step2, step3, step4, step5+6, step7, step8}
\end{axis}

\begin{axis}[
width=10cm,
height=5cm,
 xmin=1,
   xmax=10,
   ymin=0,
   ymax=12000000,
    enlarge x limits=0.15,
axis line style={draw=none},
xtick=data,
ytick={},
xticklabels ={},
yticklabels ={},
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
ticks=none,
%legend style={draw=none, at={(1.30,0.00)}}
]   

\addplot [only marks, mark=square*,draw opacity=0,    % for the last plot  
fill=predictedCol,legend image post style={xshift=0.25cm}  %for correct legend position
] table[x=Number,y=Predicted] {plot1.csv};
%\legend{predicted}
\end{axis}
\end{tikzpicture}

\ref{named}

\end{center}



\end{document}

相关内容