1

I want create a definite structure. It's a percentage chart.

This is my objective, I want to get this:

enter image description here

I try with this code

enter image description here

\documentclass[a4paper,twoside,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{pgfplotstable}

\definecolor{color1}{rgb}{0.98, 0.81, 0.69}
\definecolor{color2}{rgb}{0.55, 0.71, 0.0}
\definecolor{color3}{rgb}{1.0, 0.6, 0.4}
\definecolor{color4}{rgb}{0.29, 0.59, 0.82}

\pgfplotscreateplotcyclelist{mycolor}{
{fill=color1!80, draw=color1},
{fill=color2!80, draw=color2},
{fill=color3!80, draw=color3},
{fill=color4!80, draw=color4},
}

\pgfplotsset{
discard if/.style 2 args={
    x filter/.code={
        \ifdim\thisrow{#1} pt=#2pt
            \def\pgfmathresult{inf}
        \fi
    }
},
discard if not/.style 2 args={
    x filter/.code={
        \ifdim\thisrow{#1} pt=#2pt
        \else
            \def\pgfmathresult{inf}
        \fi
        }
    }
}


\begin{filecontents*}{coefficienti.dat}
T       K       Q       Kf      n
400     0.0463  32.9        5.78        0.321
500     0.124   24.8        6.30        0.275
600     0.115   24.6        6.30        0.261
700     1.64        24.4        11.9        0.151
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis} [
    ymin=0,
    ybar=0, bar width=20, bar shift=0,
    xtick={data},
    ymajorgrids=true,
    cycle list name=mycolor,
    nodes near coords,
    every node near coord/.append style = {
        /pgf/number format/.cd,
        fixed,
        fixed zerofill,
        precision=3
    }]

% first complete plot to place the x-axis labels
\addplot+ table[x=T,y=K] {coefficienti.dat};
% the bars with different colors
\pgfplotsinvokeforeach{500,600,700}{        
  \addplot+[discard if not={T}{#1}] table[x=T,y=K] {coefficienti.dat};
}
\end{axis}
\end{tikzpicture}

\end{document}

But in that base I can't writte text.

Someone can help me to create my percentage chart.

Any idea?

thank you very much

  • Sorry, but the question is a bit unclear to me. Where do you want to have what? You mean you want to show the nodes near coords with percentage values`? – Stefan Pinnow Aug 23 '18 at 10:01
  • I want to get the graph of the first image [visa, mastercard etc]. Any idea? thank you very much – Vidal Aug 23 '18 at 10:04
  • 1
    Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Stefan Pinnow Aug 29 '18 at 19:10
4

It is simplest if you add the labels to the data file itself. Then you "only" need to change your code "a bit" so you get the desired result.

% used PGFPlots v1.16
    % added `xlables' column
    \begin{filecontents*}{coefficienti.dat}
        T       K       Q       Kf      n       xlables
        400     0.0463  32.9    5.78    0.321   {VISA}
        500     0.124   24.8    6.30    0.275   {MasterCard}
        600     0.115   24.6    6.30    0.261   {American \\ Express}
        700     1.64    24.4    11.9    0.151   {Union Pay \\ (China)}
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \definecolor{color1}{rgb}{0.98, 0.81, 0.69}
    \definecolor{color2}{rgb}{0.55, 0.71, 0.0}
    \definecolor{color3}{rgb}{1.0, 0.6, 0.4}
    \definecolor{color4}{rgb}{0.29, 0.59, 0.82}

    \pgfplotscreateplotcyclelist{mycolor}{
        {fill=color1!80, draw=color1},
        {fill=color2!80, draw=color2},
        {fill=color3!80, draw=color3},
        {fill=color4!80, draw=color4},
    }
    \pgfplotsset{
        % changed definition of this style (so it works with `\coordindex')
        discard if not/.style 2 args={
            % suppress LOG messages about the filtered points
            filter discard warning=false,
            x filter/.expression={
                #1 == #2 ? #1 : NaN
            },
        },
    }
    % load the table (because it is needed over and over again)
    \pgfplotstableread{coefficienti.dat}\loadedtable
    % store number of rows in table
    \pgfplotstablegetrowsof{\loadedtable}
    \pgfmathtruncatemacro{\NoOfRows}{\pgfplotsretval}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ybar=0,
        bar width=20,
        bar shift=0,
        % changing `xtick' to this avoids the need for the dummy `\addplot'
        xtick={0,...,\NoOfRows},
        % then you can use a table column to show the `xticklabels'
        xticklabels from table={\loadedtable}{xlables},
        % added so `xticklabels' allow linebreaks
        x tick label style={
            align=center,
        },
        ymajorgrids=true,
        cycle list name=mycolor,
        % added value part of the key to be able to show the values as percentages
        nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
        every node near coord/.append style={
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision=2,
        },
        % adapt the axis line style
        separate axis lines,
        axis x line*=bottom,
        axis y line=none,
    ]

        % adapted
        \pgfplotsinvokeforeach{0,...,\NoOfRows}{
            \addplot+ [
                discard if not={\coordindex}{#1},
            ] table [
                x expr=\coordindex,
                y=K,
            ] {\loadedtable};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • is possible remove the black line rectangle? Sorry but it produce error in my texmaker :( – Vidal Aug 23 '18 at 12:16
  • Sure. See my edited answer. – Stefan Pinnow Aug 23 '18 at 12:24
  • nice!!! it's perfect thank you so much!! last thing, it possible to convert this graph in a image with standalone?? Now, i can't compilate the code :/ – Vidal Aug 23 '18 at 12:35
  • I would guess so. But I am the wrong person to ask that, because I never do this. If I want to export the images I use PGFPlots external library (see tikz-external). I think you mean that you cannot compile the code anymore, right? What error message to you get? – Stefan Pinnow Aug 23 '18 at 13:27
  • my error is about PGFPlots library. Can't compile the package. I think is better create an other design – Vidal Aug 24 '18 at 17:51
1

You can specify the text on the axis lables with xticklabels={Visa,Mastercard, American Express, Union Pay, JCB}

\documentclass[a4paper,twoside,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{pgfplotstable}

\definecolor{color1}{rgb}{0.98, 0.81, 0.69}
\definecolor{color2}{rgb}{0.55, 0.71, 0.0}
\definecolor{color3}{rgb}{1.0, 0.6, 0.4}
\definecolor{color4}{rgb}{0.29, 0.59, 0.82}

\pgfplotscreateplotcyclelist{mycolor}{
{fill=color1!80, draw=color1},
{fill=color2!80, draw=color2},
{fill=color3!80, draw=color3},
{fill=color4!80, draw=color4},
}

\pgfplotsset{
discard if/.style 2 args={
    x filter/.code={
        \ifdim\thisrow{#1} pt=#2pt
            \def\pgfmathresult{inf}
        \fi
    }
},
discard if not/.style 2 args={
    x filter/.code={
        \ifdim\thisrow{#1} pt=#2pt
        \else
            \def\pgfmathresult{inf}
        \fi
        }
    }
}


\begin{filecontents*}{coefficienti.dat}
T       K       Q       Kf      n
400     0.0463  32.9        5.78        0.321
500     0.124   24.8        6.30        0.275
600     0.115   24.6        6.30        0.261
700     1.64        24.4        11.9        0.151
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis} [
    ymin=0,
    ybar=0, bar width=20, bar shift=0,
    xtick={data},
    xticklabels={Visa,Mastercard, American Express, Union Pay, JCB},
        x tick label style={rotate=30,anchor=east},
    ymajorgrids=true,
    cycle list name=mycolor,
    nodes near coords,
    every node near coord/.append style = {
        /pgf/number format/.cd,
        fixed,
        fixed zerofill,
        precision=3
    }]

% first complete plot to place the x-axis labels
\addplot+ table[x=T,y=K] {coefficienti.dat};
% the bars with different colors
\pgfplotsinvokeforeach{500,600,700}{        
  \addplot+[discard if not={T}{#1}] table[x=T,y=K] {coefficienti.dat};
}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.