I know this is old, but there are some fairly simple answers to the questions so I thought I would have a go. And even though I am a fan of MP, I think that I would agree with @percusse that pgfplots is probably better than input graph
in MP.
I'll get to the list of questions in a minute, but first we can simplify things a bit. The correct form for the MP graph input is like this:
2008 98826 Your comments go here
2009 104925
2010 140153
2011 178414
so I am going to assume that you have changed mydata.d
to this format.
Given this data file we can produce a graph with just a single gdraw
line.
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" withpen pencircle scaled 1/2 withcolor 2/3 red;
endgraph;
endfig;
end.
I added some options to make the line in red with a nice weight. This produces:
And we can immediately see the first problem, the years have all been printed as "2010". Some careful reading of the manual, tells us that this is because the default precision for all numeric labels is 3 significant figures. So all the years have been rounded to the nearest 10.
The command that does this is the implicit autogrid
that is added by the endgraph
command by default. In order to fix this we have to format our labels semi-manually, as explained towards the end of section 2.2 in the manual. Changing our source like this:
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" withpen pencircle scaled 1/2 withcolor 2/3 red;
for x=auto.x: otick.bot(format("%4g", x), x); endfor
for y=auto.y: otick.lft(format("%g", y), y); endfor
endgraph;
endfig;
end.
produces a better result. What we have done here is round the years to 4 significant figures.
The second problem was to put a dot mark at each data point neatly. The correct way to do this is to add the plot
option to the gdraw
command. The OP issue was that using plot btex $\bullet$ etex
puts the dots in the wrong places. The manual does explain this as well, but not very clearly. My recommendation is that you make your own dot marks in MP, like this:
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
picture bullet; bullet = image(drawdot origin withpen pencircle scaled 2);
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" plot bullet withpen pencircle scaled 1/2 withcolor 2/3 red;
for x=auto.x: otick.bot(format("%4g", x), x); endfor
for y=auto.y: otick.lft(format("%g", y), y); endfor
endgraph;
endfig;
end.
to produce this:
The third question was "how to rotate the year labels"? To do this we need
to provide a function to use instead of the format
function. The output should be a picture
object. Here is one way to do it:
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
picture bullet; bullet = image(drawdot origin withpen pencircle scaled 2);
vardef year_label(expr Y) = decimal Y infont "cmr10" rotated 60 enddef;
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" plot bullet withpen pencircle scaled 1/2 withcolor 2/3 red;
for x=auto.x: otick.bot(year_label(x), x); endfor
for y=auto.y: otick.lft(format("%g", y), y); endfor
endgraph;
endfig;
end.
Now the graph look like this. You can play about with the rotation angle, if you really want it at 90°.
The fourth question was about the y-axis labels. The problem here is also about rounding, but we can't just change the precision, as we did for the years, because the absolute values are greater than 4096, so graph
is automatically using MP's string arithmetic, and so the formatting rules are subtly different (and not very good in my opinion). If you really want lots of zeros, then the best that I can suggest is that we put the labels in "by hand":
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
picture bullet; bullet = image(drawdot origin withpen pencircle scaled 2);
vardef year_label(expr Y) = decimal Y infont "cmr10" rotated 60 enddef;
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" plot bullet withpen pencircle scaled 1/2 withcolor 2/3 red;
for x=auto.x: otick.bot(year_label(x), x); endfor
otick.lft("100,000" infont "cmr10", "100000");
otick.lft("140,000" infont "cmr10", "140000");
otick.lft("180,000" infont "cmr10", "180000");
endgraph;
endfig;
end.
to produce this:
but the obvious drawback is that you have to know the data range to put the labels in the right places. So I think my final effort would be to reduce the size of the y values by transforming them, and adding a scale label to the axis:
prologues := 3;
outputtemplate := "%j%c.eps";
input graph
input sarith
picture bullet; bullet = image(drawdot origin withpen pencircle scaled 2);
vardef year_label(expr Y) = decimal Y infont "cmr10" rotated 60 enddef;
beginfig(1);
draw begingraph(160, 100);
gdraw "mydata.d" plot bullet withpen pencircle scaled 1/2 withcolor 2/3 red;
for x=auto.x: otick.bot(year_label(x), x); endfor
for y=auto.y: otick.lft(format("%g", y Sdiv "1000"), y); endfor
glabel.lft("Thousands" infont "cmr8" rotated 90, OUT);
endgraph;
endfig;
end.
Like so:
The answer to the fifth question about putting global comments at the top and bottom is "don't do that". Put your global comments to the right of the first
few columns.
You can put comments after the blank line that finishes each data set, but beware that if you read the file more than once (as the OP did) then your comment might be mistaken for a second data set. So I recommend that you don't do that.