我对在 Latex 中绘图还很陌生,正在尝试拼凑一个带有图例和标记轴的气泡柱图。到目前为止,我有一个工作图表,但只能得到图例或者带标签的 x 轴。
我想我可能用了比较难的方法。我使用 dummy\addplot
强制使用图例,但这样会删除轴刻度标签。有办法解决这个问题吗,或者只是有更好的方法吗?
(取消注释这两行以获取不带图例的 x 轴刻度标签)
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}
\newif\iflegend
%\legendtrue
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
1 25 1 1
1 39 0.989010989 2
2 40 0.673629243 3
2 13 0.499013807 4
4 37 0.575 5
5 98 0.994505495 2
5 108 0.64229765 3
5 6 0.583333333 5
6 1 0.96 1
6 6 0.972527473 2
6 14 0.454308094 3
7 15 0.58382643 4
7 6 0.583333333 5
\end{filecontents*}
\begin{document}
\begin{tikzpicture}[y=.5cm]
\pgfplotstableread{data.dat}\table
\pgfplotstablegetrowsof{\table}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\table}
\pgfmathsetmacro{\N}{\pgfplotsretval-1}
\definecolor{color0}{RGB}{0,0,0}
\definecolor{color1}{RGB}{255,0,0}
\definecolor{color2}{RGB}{0,255,0}
\definecolor{color3}{RGB}{150,255,200}
\definecolor{color4}{RGB}{200,200,90}
\definecolor{color5}{RGB}{100,0,200}
\definecolor{color6}{RGB}{190,0,190}
\begin{axis}[
scatter,
axis lines=left,
ylabel=Training Time,
every axis/.append style={font=\tiny},
x axis line style=-,
y axis line style=-,
xtick=data,
xticklabels={,,Label 1,Label 2,Label 3,Label 4,Label 5},
xticklabel style={align=left},
width=8.6cm,
height=9.2cm,
xmin=0, xmax=5,
ymin=0, ymax=110,
legend style={
at={(0,0)},
anchor=south west,
at={(axis description cs:0.15,-0.35)},
draw=none % no border
},
% legend entries={baseline,legend1,legend3,legend4,legend5,legend6},
scatter/@pre marker code/.code={%
\pgfplotstransformcoordinatex{\pgfplotspointmeta}%
\scope[mark size=\pgfplotsunitxlength*\pgfmathresult]
}
]
\iflegend
\addplot +[bar shift, area legend, color=color0] coordinates {(6,0.001)};
\addplot +[bar shift, area legend, color=color1, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color2, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color3, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color4, fill, opacity=.5] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color5, fill, opacity=.5, solid] coordinates {(0,.01)};
\addplot +[bar shift, area legend, color=color6, fill, opacity=.5, solid] coordinates {(0,.01)};
\fi
\end{axis}
\foreach \row in {0,...,\M}{
\foreach \col in {0,...,\N}{
\pgfplotstablegetelem{\row}{[index]\col}\of\table
\ifnum\col=3 % video
\xdef\x{\pgfplotsretval}
\fi
\ifnum\col=1 % time
\xdef\y{\pgfplotsretval}
\fi
\ifnum\col=2 % accuracy
\xdef\radius{\pgfplotsretval}
\fi
\ifnum\col=0 % method
\xdef\category{\pgfplotsretval}
\fi
}
\ifnum\category=1
\definecolor{mycolor}{RGB}{0,0,0}
\else\ifnum\category=2
\definecolor{mycolor}{RGB}{255,0,0}
\else \ifnum\category=3
\definecolor{mycolor}{RGB}{0,255,0}
\else\ifnum\category=4
\definecolor{mycolor}{RGB}{150,255,200}
\else\ifnum\category=5
\definecolor{mycolor}{RGB}{200,200,90}
\else\ifnum\category=6
\definecolor{mycolor}{RGB}{100,0,200}
\else\ifnum\category=7
\definecolor{mycolor}{RGB}{190,0,190}
\else \definecolor{mycolor}{RGB}{128,128,128}
\fi
\fi
\fi
\fi
\fi
\fi
\fi
\ifnum\category=1
\draw[mycolor] (\x*1.41,1+\y/8)circle(\radius*5mm);
\else
\fill[mycolor,opacity=.5] (\x*1.41,1+\y/8)circle(\radius*5mm);
\fi
}
\end{tikzpicture}
\end{document}
答案1
我同意你做得有点太过努力了。pgfplots 已经提供了一些非常接近的替代机制。
\documentclass[border=4mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}
\begin{filecontents*}{data.csv}
met time acc video
1 25 1 1
1 39 0.989010989 2
2 40 0.673629243 3
2 13 0.499013807 4
4 37 0.575 5
5 98 0.994505495 2
5 108 0.64229765 3
5 6 0.583333333 5
6 1 0.96 1
6 6 0.972527473 2
6 14 0.454308094 3
7 15 0.58382643 4
7 6 0.583333333 5
\end{filecontents*}
\definecolor{color3}{RGB}{150,255,200}
\definecolor{color4}{RGB}{200,200,90}
\definecolor{color5}{RGB}{100,0,200}
\definecolor{color6}{RGB}{190,0,190}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
ylabel=Training Time,
every axis/.append style={font=\tiny},
x axis line style=-,
y axis line style=-,
xtick=data,
xticklabels={Label 1,Label 2,Label 3,Label 4,Label 5},
xticklabel style={align=left},
width=8.6cm,
height=9.2cm,
xmin=0, xmax=5,
ymin=0, ymax=110,
legend style={
at={(0,0)},
anchor=south west,
at={(axis description cs:0.15,-0.35)},
draw=none % no border
},
legend entries={legend1,legend2,legend3,legend4,legend5},
only marks,
%legend image post style={only marks},
scatter/classes={
1={draw=black,fill=white},
2={fill=red},
3={green},% etc. etc.
4={color3},
5={color4},
6={color5},
7={color6}
},
visualization depends on= 10*\thisrow{acc} \as\myrad,
scatter/@pre marker code/.append style={/tikz/mark size=\myrad}
]
\addplot[scatter,scatter src=explicit,mark=*] table[x =video,y=time,meta=met] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}