我对 ybar 组合图中的图例图像有问题。最后一个条目不是虚线。有什么方法可以addlegendimage
解决这个问题吗?
\begin{tikzpicture}
\begin{axis}[
ybar = 0.0cm, %Vzdalenost mezi bary
bar width = 0.28cm, %Sirka baru
width=\linewidth, % Scale the plot to \linewidth
cycle list name=barvyBaru, % Barevne schema baru
xlabel=Koncentrace, % Set the labels
ylabel=Rozdíl hmot. úbytků,
y unit=\%, % Procenta na ose Y
xtick = {0,1,2,3},
xticklabels = {0.1, 0.5, 1.0, 3.0},
x unit=\%, % Stupne celsia na ose X
legend style={at={(0.95,0.95)},anchor=north east, legend columns = 3}, %Legenda pod grafem
%nodes near coords, %Zobrazi hodnoty primo nad sloupcema
ymin=94, ymax=102, xmin=-0.5, xmax=3.5
]
\addplot table[x expr=\coordindex,y=0mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{0 měsíců}
\addplot table[x expr=\coordindex,y=3mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{3 měsíce}
\addplot table[x expr=\coordindex,y=6mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{6 měsíců}
\addplot table[x expr=\coordindex,y=9mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{9 měsíců}
\addplot table[x expr=\coordindex,y=12mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{12 měsíců}
\addplot table[x expr=\coordindex,y=15mes,col sep=comma] {grafy/55_PET_voda_mnozstvi.csv};
\addlegendentry{15 měsíců}
\addlegendimage{dashed,line width=1pt, smooth} % Upravi obrazek v legende na caru misto baru
\addplot[dashed,line width=1pt, smooth] coordinates {(-0.5,95.561) (3.5,95.561)};\label{necol}
\addlegendentry{půda (bez MP)}
\end{axis}
\end{tikzpicture}
答案1
首先简要说明一下您的示例。虽然添加示例很好,但如果可以在不进行任何修改的情况下进行测试,那总是最好的。在您的示例中,需要添加一个前言,找出units
需要的库,删除对barvyBaru
(因为您没有提供它的定义,但它也与问题无关),最后删除所有使用数据文件的图(因为也没有提供,并且这些图与问题无关)。有关制作这种最小工作示例 (MWE) 的更多信息,请参阅我刚刚被要求写一个最小工作示例(MWE),那是什么?
\addlegendimage
使用 绘制线条时不需要\addplot
,因此实际上可以将其完全删除。要修复图例,请添加line legend
到\addplot
选项中。我个人建议使用sharp plot
而不是smooth
,因为这似乎是从条形图更改为线图的更合适的方法:
\addplot[
dashed,
line width=1pt,
sharp plot, % <-- changes the plot type from a bar plot to a line plot
line legend % <-- sets the type of legend entry to use
] coordinates {(-0.5,95.561) (3.5,95.561)};
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar = 0.0cm, %Vzdalenost mezi bary
ymin=94, ymax=102, xmin=-0.5, xmax=3.5
]
\addplot[
dashed,
line width=1pt,
sharp plot, % <-- changes the plot type from a bar plot to a line plot
line legend % <-- sets the type of legend entry to use
] coordinates {(-0.5,95.561) (3.5,95.561)};\label{necol}
\addlegendentry{půda (bez MP)}
\end{axis}
\end{tikzpicture}
\end{document}