0

I would like to draw this histogram (see the attached image) on a ruled graph sheet. So far, I have the MWE as follows:

\documentclass[tikz,border=2mm]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width = 1.00*\textwidth,
height = 8cm,
ybar=2*\pgflinewidth,
bar width=1pt,
ymajorgrids = true,
ybar interval=1.0,
xlabel = {Months},
ylabel = {Amount of Rainfall (cm)},
symbolic x coords={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec},
ymin=0,
]
\addplot[style={blue,fill=green}] 
coordinates {
(Jan,0.6) (Feb,1)
(Mar,2) (Apr,3.2) 
(May,4) (Jun,4) 
(Jul,4.5) (Aug,3) 
(Sep,5) (Oct,5) 
(Nov,0.7) (Dec,0)
};
\end{axis}
\end{tikzpicture}
\end {document}

Histogram on a Ruled Graph Sheet

Please, I need the following help:

  1. The last value for December (Dec) is not displaying. I want this to display.
  2. I need the plot to have minor gridlines as in the attached image.
  3. I will like the bars to be a bit transparent so that the gridlines can still be seen through.
  4. If possible, to remove the bounding box of the axis plot in the MWE from displaying. In fact, I need the graph sheet to resemble that in the attached image
  • In this pgfplots package is your friend. So, see the first example on the page 88 of pgfplots manual. This example can be relative easy adapt to what you like to have. In the first attempt don't bother with image background, this we can help you add when you will have your histogram. So far your question is "do.it-instead-me" ... what is liked here. – Zarko Oct 24 at 14:36
  • Okay, let me try it again. Though I have tried it before and got no success in making it look like the above. Regarding the example on page 88, the nodes on the x axis are intervals, but this one is symbolic coordinates – Chika Odiliobi Oct 24 at 14:55
  • symbolic coordinates -- page 386 -- just do ctrl-F to search the pdf doc – js bibra Oct 24 at 15:31
  • I've tried my best. See the MWE that I attached. Please help me. – Chika Odiliobi Oct 24 at 18:19
2

1- For Dec, add a dummy value with zero to the symbolic x coords, say a.

2- For a minor grid lines, use grid style and major grid style options in axis environment, like:

grid=major,
grid=both,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt,draw=gray!60},
minor tick num=5

Also add

xmin=Jan,
xmax=a,
ymin=0,
ymax=5

3- For tranparency, use opacity option in addplot.

MWE:

\documentclass[tikz,border=2mm]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width = 1.00*\textwidth,
height = 8cm,
ybar=2*\pgflinewidth,
bar width=1pt,
%ymajorgrids = true,
ybar interval=1.0,
xlabel = {Months},
ylabel = {Amount of Rainfall (cm)},
symbolic x coords={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,a}, % Add a zero valued extra coord. for Dec.
grid=major,
grid=both,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt,draw=gray!60},
minor tick num=5,
xmin=Jan,
xmax=a,
ymin=0,
ymax=5,
]
\addplot[style={blue,fill=green!30,opacity=0.5}] 
coordinates {
(Jan,0.6) (Feb,1)
(Mar,2) (Apr,3.2) 
(May,4) (Jun,4) 
(Jul,4.5) (Aug,3) 
(Sep,5) (Oct,5) 
(Nov,0.7) (Dec,0) (a,0)
};
\end{axis}
\end{tikzpicture}
\end {document}

enter image description here

| improve this answer | |
  • Thank you greatly.👍 – Chika Odiliobi Oct 25 at 0:27
  • 1
    You only need one grid option. If you use both, then major is not needed because it's included in the former along with grid=minor. – Alenanno Oct 25 at 2:42

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.