# Plot bar graph using table data

## I am trying to plot this kind of graph using pgfplots here is my code:

it is showing error pls help me

\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Year,
ylabel=No. of Ques]
\addplot[smooth,mark=*,blue] plot coordinates {
(CD,0)
(CN,0)
(CO,0)
(DA,0)
(DBMS,0)
(DLD,0)
(DM,0)
(EM,1)
(GEN,0)
(OS,0)
(PROG,1)
(TOC,0)
};
2015}
\end{axis}
\end{tikzpicture}
\end{document}


You just need to define your coordinates before using them.

Replace

\begin{axis}[
xlabel=Year,
ylabel=No. of Ques]


with

    \begin{axis}[
xlabel=Year,
ylabel=No. of Ques,
symbolic x coords={CD, CN, CO, DA, DBMS, DLD, DM, EM, GEN, OS, PROG, TOC}]


to get

To see all of the x-coordinates, they must have enough space to fit. You will have to enlarge the width of the plot. Solution to this was provided as answer to the question of How to increase/fit the width of x-axis to fit all points in pgfplots?

Now replace

\begin{axis}[
xlabel=Year,
ylabel=No. of Ques,
symbolic x coords={CD, CN, CO, DA, DBMS, DLD, DM, EM, GEN, OS, PROG, TOC}]


with

\begin{axis}[
xlabel=Year,
ylabel=No. of Ques,
symbolic x coords={CD, CN, CO, DA, DBMS, DLD, DM, EM, GEN, OS, PROG, TOC},
enlarge x limits=-1,
width=13cm] % ADJUST WIDTH TO YOUR LIKING


to get

Here is your code with above modifications and a document class declaration (which was absent) in case you run into problems:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Year,
ylabel=No. of Ques,
symbolic x coords={CD, CN, CO, DA, DBMS, DLD, DM, EM, GEN, OS, PROG, TOC},
enlarge x limits=-1,
width=13cm] % ADJUST WIDTH TO YOUR LIKING
\addplot[smooth,mark=*,blue] plot coordinates {
(CD,0)
(CN,0)
(CO,0)
(DA,0)
(DBMS,0)
(DLD,0)
(DM,0)
(EM,1)
(GEN,0)
(OS,0)
(PROG,1)
(TOC,0)
};
2015}
\end{axis}
\end{tikzpicture}
\end{document}


If you want the exact plot as showed in the picture of your question, you'll have to let pgfplots know that you need a bar plot. You'll also have to provide same values as shown in your picture.

• Why not just add ybar to the axis options (and remove all the options for the \addplot)? Sep 4, 2017 at 20:14