I am collating data from a survey and I want to create a bar graph like the one below:
Can anyone provide me with a template of that chart? I tried to inflect bar chart and stick the two together but it does not work very well.
Thanks!
bar chart
I am collating data from a survey and I want to create a bar graph like the one below:
Can anyone provide me with a template of that chart? I tried to inflect bar chart and stick the two together but it does not work very well.
Thanks!
You can place the descriptions of the bars using nodes near coords
. By default, that will print the meta
value at the end of the bar, which in turn defaults to the dependent variable (the x coordinate in this case).
To also print the prename
value, several steps are required: First, you need to make the content of that column available to PGFPlots. You do this by adding point meta=explicit symbolic
to the \addplot
options (instructing PGFPlots to expect some kind of non-numerical auxiliary data for each data point). In the table
options, you can then set meta=prename
. The content of the prename
column will then be available for each data point by using \pgfkeysvalueof{/pgfplots/meta}
.
Then, you need to find a way to print this at the desired position. Unfortunately, nodes near coords
only prints a single node. Fortunately, you can teach PGFPlots to print extra nodes by "piggybacking" onto this node by using every node near coord/.append style={append after command=node {<some text>}
. This will append node {<some text>}
after the label node.
In order to position this extra node correctly, you'll need to shift it. By default, it is placed at the tip of the bar. To position it at the base of the bar, you can use node [anchor=west] at (axis direction cs:-\pgfkeysvalueof{/data point/x}+\pgfkeysvalueof{/pgfplots/xmin},0)
. Note: The /pgfplots/xmin
is only necessary if you want your bar plot to start from some value other than zero, which is not usually a good idea. As Ethan pointed out in the comments, that's usually misleading. If you want to highlight differences by decreasing the plot range, consider using points instead of bars.
You'll notice that if you do this, the content of prename
column is printed twice: once at the base and once at the tip of the bar. To get the x value back, set nodes near coords=\pgfkeysvalueof{/data point/x}
(instead of just nodes near coords
, which is shorthand for nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}
).
Here's the result of the effort:
... and here's the code:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.9}
\pgfplotstableread[col sep=comma]{
surname,prename,values
Miller,Martin,0.8
Public,John,0.4
Miller,Catherine,0.6
}\testdata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xbar,
ytick = data,
yticklabels from table={\testdata}{surname},
major y tick style=transparent,
point meta=explicit symbolic
]
\addplot +[
nodes near coords=\pgfkeysvalueof{/data point/x},
every node near coord/.append style={
anchor=west,
append after command={
node [anchor=west] at (axis direction cs:-\pgfkeysvalueof{/data point/x}+\pgfkeysvalueof{/pgfplots/xmin},0) {\pgfkeysvalueof{/data point/meta}}}
}
] table [y expr=\coordindex, meta=prename, x=values]{\testdata};
\end{axis}
\end{tikzpicture}
\end{document}
I would really not recommend this plot because basically it's not readable and all data is dominated by Step 3 and predicted ones. First the question,
The plot goes up because you enlarge both limits, you need enlarge x limits=0.15,
I didn't get that error with your code.
You can do that via both declaring a width and a height dimension.
I cleaned up a bit, the code and the result is
\documentclass[tikz,border=5pt]{standalone}
\usepackage[oldstylenums]{kpfonts}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.10}
\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}
\usetikzlibrary{shadows,shadows.blur}
\begin{filecontents*}{plot1.csv}
Number Step1 Step2 Step3 Step4 Step5_6 Step7 Step8 Predicted
0 50 138 2025137 1400 15859 1358 50 11788769
1 50 894 2088724 1898 14662 2035 50 7564508
2 50 1610 3482495 1405 11490 1302 50 5970268
3 50 871 2089859 898 5021 569 50 7864363
4 50 138 3470704 1405 15888 1302 50 11788769
5 50 871 3481357 1909 11110 1324 50 7560008
6 50 871 2089855 2476 16015 885 50 7878218
7 50 1375 4875299 1903 17401 1258 50 11791029
8 50 877 2786201 1405 10704 1358 50 7871713
9 50 894 2733003 898 5027 569 50 7864363
10 50 138 3481371 1400 15882 1302 50 11788769
11 50 894 2088720 1405 18347 1302 50 7566933
\end{filecontents*}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[myplot/.style={ybar,draw=none,area legend},
width=10cm,height=5cm,
bar width=10pt,
enlarge x limits=0.15,
ylabel={clock cycles},
xlabel={tile mapping(VLD, IQ/IDCT, CC)},
ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
xtick=data,
xmin=1,xmax=10,
ymin=1,ymax=1.5e7,
axis line style={draw=none},
legend style={legend cell align=left,at={(1.20,1.00)},anchor=north,
append after command={\pgfextra{\draw[draw=none,blur shadow]
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
}
},
legend image post style={draw opacity=0},
legend entries={Step 1,Step 2,Step 3,Step 4,Step 5+6,Step 7,Step 8,Predicted}
]
\addplot[myplot,fill=step1Col ] table[x=Number,y=Step1] {plot1.csv};
\addplot[myplot,fill=step2Col ] table[x=Number,y=Step2] {plot1.csv};
\addplot[myplot,fill=step3Col ] table[x=Number,y=Step3] {plot1.csv};
\addplot[myplot,fill=step4Col ] table[x=Number,y=Step4] {plot1.csv};
\addplot[myplot,fill=step5_6Col] table[x=Number,y=Step5_6] {plot1.csv};
\addplot[myplot,fill=step7Col ] table[x=Number,y=Step7] {plot1.csv};
\addplot[myplot,fill=step8Col ] table[x=Number,y=Step8] {plot1.csv};
\addplot[only marks,mark=square*,red] table[x=Number,y=Predicted] {plot1.csv};
\end{axis}
\end{tikzpicture}
\end{document}
As you can see, most of your data vanished and you have strange entries in your legend because those datasets are invisible.
Instead I can think of two options,
clean up the legend and mention only step3, step 5+6, and predicted columns with a disclaimer that the remaining step contribution is negligible and comparable
Combine your negligible entries into a sum and plot that but I can't judge whether it would be a good idea here for your application.
Best Answer
This looks similar, except: