1

I want to make my plot look like the one in the screenshot. Same colors, same style and position of the legends and the dotted grid in the background!

enter image description here

One more bar will be added, though

Ideally it would look something like this(3 bars only):

enter image description here

 \documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\definecolor{curlyblue}{RGB}{39,26,115}
\definecolor{curlygreen}{RGB}{48,172,140}   
\pgfplotsset{width=9cm,compat=1.8}
\pgfplotsset{
    /pgfplots/bar cycle list/.style={/pgfplots/cycle list={
    {black,fill=curlyblue,mark=none},
{black,fill=curlygreen,mark=none},
{black,fill=yellow,mark=none},
}, }}

\begin{document}
\begin{tikzpicture}[font=]
\begin{axis}[
    ybar,
    enlargelimits=0.09,
    legend style={at={(0.75,0.95)},
      anchor=north,legend columns=1},
    ylabel={RMSE},
    symbolic x coords={50,100,150,200,250},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    ]


\addplot coordinates {(50,0.9359) (100,0.9351) (150,0.9361) (200,0.9376) (250,0.9386) };
\addplot coordinates {(50,0.9324) (100,0.9294) (150,0.9291) (200,0.9290) (250,0.9289) };
\addplot coordinates {(50,0.9297) (100,0.9281) (150,0.9280) (200,0.9280) (250,0.9280) };
\legend{Cosine,Pearson,PearsonBL}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

I also tried this but it does not work either:

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}

\begin{filecontents}{data.csv}
    A,  B,  C,  D
    50,  0.9359, 0.9324, 0.9297
    100, 0.9351, 0.9294, 0.9281
    150, 0.9361, 0.9291, 0.9280
    200, 0.9376, 0.9290, 0.9280
    250, 0.9386, 0.9289, 0.9280
%   300, 0.9359, 0.9359, 0.9359
%   350, 0.9359, 0.9359, 0.9359
%   350, 0.9359, 0.9359, 0.9359 
\end{filecontents}
\pgfplotstableread[col sep=comma,]{data.csv}\datatable

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[width=11cm,
    ybar,
    bar width=7pt,
    xlabel={},
    xtick=data,
    xticklabels from table={\datatable}{A},
    ymajorgrids,
    legend pos=north west
    ]
    \addplot table [x expr=\coordindex, y=B]{\datatable};
    \addplot table [x expr=\coordindex, y=C]{\datatable};
    \addplot table [x expr=\coordindex, y=D]{\datatable};
    \legend{Cosine, Pearson, PearsonBL}
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

HERE ARE THE TRUE COORDINATES:

\addplot[black,fill=blue1] coordinates {
(50,0.9359) (100,0.9351) (150,0.9361) (200,0.9376) (250,0.9386) (300,0.9391) (350,0.9393) (400,0.9395)
  };
   \addlegendentry{Bla0}
\addplot[black,fill=red1] coordinates {


(50,0.9324) (100,0.9294) (150,0.9291) (200,0.9290) (250,0.9289) (300,0.9289) (350,0.9289) (400,0.9289)};
\addlegendentry{Bla1}

\addplot[black,fill=gray1] coordinates {
                    (50,0.9297) (100,0.9281) (150,0.9280) (200,0.928) (250,0.928) (300,0.928) (350,0.928) (400,0.9280)
                };
    \addlegendentry{Bla2}
4
  • Could you please add some clarifications to your question about what exactly your problem is: Is it the position and style of the legend, is ist the colors, is it the numbers that appear above the bars, is it the y-axis labels, is it the dotted grid in the background...? – leandriis Oct 3 '19 at 8:55
  • I want to make my plot look like the one in the screenshot. Same colors, same style and position of the legends and the dooted grid in the background! – NaveganTeX Oct 3 '19 at 8:59
  • 1
    So, basically, everything, I mentioned. Since your screenshot of the desired output only shows two sets of bars, (=two colors), what's the desired color of the third set? – leandriis Oct 3 '19 at 9:00
  • A gray good be awesome! Or whatever one you consider that matches the triplet – NaveganTeX Oct 3 '19 at 9:01
2

Probably the following is closer to what you wanted to achieve:

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

%% Define the colors used for the bars %%
\definecolor{myblue}{RGB}{0,0,143}
\definecolor{myred}{RGB}{128,0,0}   
\definecolor{mygray}{RGB}{128,128,128}

\pgfplotsset{width=9cm,compat=1.8}


%% Set up for the bar chart, mostly unchanged despite of colors %%
\pgfplotsset{/pgfplots/bar cycle list/.style={
                 /pgfplots/cycle list={
                     {black,fill=myblue,mark=none},
                     {black,fill=myred,mark=none},
                     {black,fill=mygray,mark=none},
             }, }}


%% Adding the dotted grid %%
\pgfplotsset{grid style = {
                 dash pattern = on 0.05mm off 1mm,
                 line cap = round,
                 black,
                 line width = 0.5pt
             }}



\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    %% related to legend, draw=none removes the border, anchor= north east places legend in upper right corner%%
    legend style={draw=none,at={(1,1)},anchor=north east,legend columns=1},
    legend cell align={left},
    %% style of y axis labels, precision=3 adds a third decimal place, fixed zerofill adds otherwise missing zeros (e.g. 0.93 vs. 0.930)%%
    y tick label style={/pgf/number format/.cd,precision=3,fixed zerofill},
    ylabel={RMSE},
    %% ymax makes sure that the legend does not overlap with the blue 250 bar
    ymax=0.941, 
    %% adds sme horizontal white space between the left- and right-most bar and hte corresponding y axis%%
    enlarge x limits=0.15,
    %% adds the grid that we defined earlier%%
    grid,
    %% makes sure that the ticks of the upper x axis stay inside the plot
    xtick align=inside
    ]


\addplot coordinates {(50,0.9359) (100,0.9351) (150,0.9361) (200,0.9376) (250,0.9386) };
\addplot coordinates {(50,0.9324) (100,0.9294) (150,0.9291) (200,0.9290) (250,0.9289) };
\addplot coordinates {(50,0.9297) (100,0.9281) (150,0.9280) (200,0.9280) (250,0.9280) };
\legend{Cosine,Pearson,PearsonBL}
\end{axis}
\end{tikzpicture}

\end{document}

Changes I have done in comparison to the original code:

  • Color of the bars: In order to add the desired colors to the bars, I have changed the origonal \definecolor commands to match the colors in the screenshot. I have also added a third, gray color for the third set of bars.
  • Dotted grid: The code for the dotted grid is borrowed from Jake's answer to Change grid line dash pattern such that grid intersections look uniform
  • Legend style: To remove the box around the legend, I have used the draw=noneoption. To place the legend in the upper right corner, I have used anchor=north east and to left align the legend entries, I have used legend cell align={left}. (Inspiration for these options came from Harry49's answer to How to align the content of the legend to the left instead of default centre? and from Matthew Leingang's answer to Removing legend box (border) in pgfplots). I have also added y max in order to prevent the legend from overlapping with the large blue bar of the rightmost set.
  • Number format of y axis: To change the number format of the y axis to include an additional decimal place, I have used y tick label style={/pgf/number format/.cd,precision=3,fixed zerofill}, that was inspired by Jake's answer to pgfplots - number format on axis. If you prefer 0.930 being displayed without the trailing 0(as in 0.93), simply remove fixed zerofill.
  • Horizontal white space: To add some horizontal white space between the leftmost and the rigtmost column and theis respective y axis, I have used enlarge x limits=0.15, as suggested by Jake in their answer to Insert space between axis and plot in pgfplots.
  • Position of upper x axis ticks: In order to place the ticks of the upper x axis below the axis (inside of the plot) instead of above, I have added xtick align=inside as shown by Jake in the answer to Pgfplots ybar option draws tickmarks outside the plot area.
  • Numbers above bars: Lastly, I have removed nodes near coords to get rid of the numbers that were displayes above the corresponding bars.

Here is the adjsted MWE for your actual data. I have adjusted the color names to match the definitions, changed the values of ymax and enlarge x limits and added added bar width=5pt, to prevent the individual sets of bars from overlapping:

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

%% Define the colors used for the bars %%
\definecolor{myblue}{RGB}{0,0,143}
\definecolor{myred}{RGB}{128,0,0}   
\definecolor{mygray}{RGB}{128,128,128}

\pgfplotsset{width=9cm,compat=1.8}


%% Adding the dotted grid %%
\pgfplotsset{grid style = {
                 dash pattern = on 0.05mm off 1mm,
                 line cap = round,
                 black,
                 line width = 0.5pt
             }}



\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=5pt,
    %% related to legend, draw=none removes the border, anchor= north east places legend in upper right corner%%
    legend style={draw=none,at={(1,1)},anchor=north east,legend columns=1},
    legend cell align={left},
    %% style of y axis labels, precision=3 adds a third decimal place, fixed zerofill adds otherwise missing zeros (e.g. 0.93 vs. 0.930)%%
    y tick label style={/pgf/number format/.cd,precision=3,fixed zerofill},
    ylabel={RMSE},
    %% ymax makes sure that the legend does not overlap with the blue 250 bar
    ymax=0.943, 
    %% adds sme horizontal white space between the left- and right-most bar and hte corresponding y axis%%
    enlarge x limits=0.1,
    %% adds the grid that we defined earlier%%
    grid,
    %% makes sure that the ticks of the upper x axis stay inside the plot
    xtick align=inside
    ]


\addplot[black,fill=myblue] coordinates {
        (50,0.9359) (100,0.9351) (150,0.9361) (200,0.9376) (250,0.9386) (300,0.9391) (350,0.9393) (400,0.9395)};
   \addlegendentry{Bla0}

\addplot[black,fill=myred] coordinates {
        (50,0.9324) (100,0.9294) (150,0.9291) (200,0.9290) (250,0.9289) (300,0.9289) (350,0.9289) (400,0.9289)};
\addlegendentry{Bla1}

\addplot[black,fill=mygray] coordinates {
        (50,0.9297) (100,0.9281) (150,0.9280) (200,0.928) (250,0.928) (300,0.928) (350,0.928) (400,0.9280)};
\addlegendentry{Bla2}
\end{axis}
\end{tikzpicture}

\end{document}
4
  • Just in case , for the labels: ` \pgfplotsset{legend image code/.code={\draw [#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);},}` – NaveganTeX Oct 3 '19 at 9:34
  • 1
    @NaveganTeX: I have edited my answer to include an annotated, slightly changed MWE as well as some additional explanation. I hope this helps if you want to do some further adjustments. – leandriis Oct 3 '19 at 10:06
  • Outstanding. Thank you very much!!! – NaveganTeX Oct 3 '19 at 10:09
  • @NaveganTeX: You're welcome. For fun, I have added another, slightly edited MWE that includes the real coordinates (that I did not see before) from your question. – leandriis Oct 3 '19 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.