11

I have the following code in Latex using gpfplot. The legend in the generated graph always looks like the image attached. There are two boxes instead of one for each entry. Why this is happening? How can I modify the legend so that is only one box for each entry.

(Also, how to make the y axis showing actual value instead of 3*10^6)

It looks like I can not post images. So the legend looks like this: [box1_blue] [box2_blue] entry1 [box1_red] [box2_red] entry2

\begin{tikzpicture}
\begin{axis}[
    ybar,
    tick label style={font=\small},
    tickpos=left,
    xticklabels={double, 20 digits,7 digits, 6 digits, 5 digits}, 
    xtick={1,2,3,4,5},
    ymin=0,
    legend entries={entry1,entry2}
    ]
    \addplot +[bar shift=-.2cm] coordinates {(1,3103533) (2,571651) (3,240729) (4,218595)     (5,207303)};

    \addplot  +[bar shift=.2cm]coordinates {(1,519042) (2,86885) (3,46461) (4,44745) (5,43960)};

\end{axis}
\end{tikzpicture}
Ken Ma
  • 113
  • Welcome to TeX.sx! Please modify your question into a minimal working example (MWE). It is considered a lot better to put in some code that will compile, as it makes it a lot easier for us to copy it into our text editor and work with it, and see exactly what it is you are trying to do. – hpesoj626 Nov 12 '12 at 07:07
  • As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – hpesoj626 Nov 12 '12 at 07:09
  • 1
    I guess the legend entry with two boxes is meant to distinguish box plots from area plots. The latter would get a legend entry with a singular rectangle. – hakaze Nov 12 '12 at 07:37

2 Answers2

14

Another way to change the legend is to use area legend in the \addplot.

To adjust the y tick label style you can use y tick label style as shown below:

enter image description here

If you comment out the fixed setting you get the scientific notation for the y-ticks:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document} \begin{tikzpicture} \begin{axis}[ ybar, tick label style={font=\small}, tickpos=left, xticklabels={double, 20 digits,7 digits, 6 digits, 5 digits}, xtick={1,2,3,4,5}, ymin=0, legend entries={entry1,entry2}, y tick label style={/pgf/number format/.cd,% scaled y ticks = false, set thousands separator={}, fixed }, ] \addplot +[bar shift=-.2cm, area legend] coordinates {(1,3103533) (2,571651) (3,240729) (4,218595) (5,207303)};

\addplot  +[bar shift=.2cm, area legend]coordinates {(1,519042) (2,86885) (3,46461) (4,44745) (5,43960)};

\end{axis} \end{tikzpicture} \end{document}

Peter Grill
  • 223,288
  • Is it possible to add thrid entry? – alper Jun 02 '23 at 11:34
  • 1
    @alper: Should just need to add another addplot and include entry3 in the legened enteries. You will also weak the shift the bar shift' parameters. If you have difficluty, I suggest you post a new question including a MWE that set up the problem. – Peter Grill Jun 03 '23 at 19:29
5

Try this:

\documentclass{standalone}

\usepackage{tikz, pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    tick label style={font=\small},
    tickpos=left,
    xticklabels={double, 20 digits,7 digits, 6 digits, 5 digits}, 
    xtick={1,2,3,4,5},
    ymin=0,
    legend entries={entry1,entry2},
    legend image code/.code={%
      \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
    }   
    ]   
    \addplot +[bar shift=-.2cm] coordinates {(1,3103533) (2,571651) (3,240729)  (4,218595)     (5,207303)};

    \addplot  +[bar shift=.2cm]coordinates {(1,519042) (2,86885) (3,46461) (4,  44745) (5,43960)};

\end{axis}
\end{tikzpicture}

\end{document}

I have added legend image code key here to modify the default key for bar charts. Play around with the dimensions of the rectangle for a desired effect.

enter image description here

Please refer the following questions for changing the x/y tick label formats:

  1. pgfplot with constant decimal places on ticks

  2. How can I change format of number's axis on pgfplots

devendra
  • 2,818
  • This does not show the actual y value. – hpesoj626 Nov 12 '12 at 07:45
  • You don't need the tikz package. – Peter Grill Nov 12 '12 at 07:56
  • @PeterGrill thank you peter. I am getting a strange error: Package pgfkeys Error: I do not know the key '/pgf/number format/at' and I am going to ignore it. Perhaps you misspelled it. when I add the y tick label style to my code. I have used it a hundred times but don't know what went wrong this time. – devendra Nov 12 '12 at 08:01
  • Where is the at in /pgf/number format/at coming from? – Peter Grill Nov 12 '12 at 08:06
  • /pgf/number format/.cd – devendra Nov 12 '12 at 08:08
  • But the error message you quoted is /pgf/number format/at, not /pgf/number format/.cd? check that you don't have blank lines in the options to axis, and you added a comma to the previous option. – Peter Grill Nov 12 '12 at 08:09
  • @PeterGrill You can reproduce the error in your code by commenting out scaled y ticks = false, and set thousands separator={}. – devendra Nov 12 '12 at 08:11
  • Hmmmm.. Interesting. Not sure why but if you want to do that then you need to also remove the .cd,. – Peter Grill Nov 12 '12 at 08:17