TransWikia.com

Problem with legend image in ybar combined chart

TeX - LaTeX Asked by pepe on August 6, 2021

i have problem with legend image in ybar combined chart. The last entry is not dashed line. Is there any options for addlegendimage to solve this?

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}

enter image description here

One Answer

A quick note about your example first. While it's good that you add an example, it is always best if it can be tested without any modifications. In your one needs to add a preamble, figure out that the units library is required, remove the use of the barvyBaru cycle list (because you didn't supply the definition of it, but it's also irrelevant to the problem), and finally remove all the plots that uses the data file (because that isn't supplied either, and those plots are not relevant to the problem). More about making such a minimal working example (MWE) can be found in I've just been asked to write a minimal working example (MWE), what is that?


You don't need the addlegendimage when you use addplot to draw the line, so you can in fact remove that altogether. To fix the legend, add line legend to the addplot options. Personally I'd suggest using sharp plot instead of smooth, as that seems the more appropriate way of changing from a bar plot to a line plot:

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)};

enter image description here

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}

Correct answer by Torbjørn T. on August 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP