I found nice examples of Metapost, http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html.

When I use the example number 270, the pdf only contains a page number. Question: what I am doing wrong. I am a novice. I am using TeXworks, ConTeXt with LuaTeX.

I use the follow code:

    draw begingraph(3cm,2cm)
      gdraw "data1";

I am unsure about the necessary data. I have tried to save data1.txt and data1.d with different content, such as

1 5
2 4
3 2
0.0         0.0
0.2         0.447214
0.4         0.632456
1cm 0cm
2cm 1cm
3cm 2cm  

ConTeXt responds with the follow error message:

.... metapost > initializing instance 'metafun' using format 'metafun' metapost > loading 'metafun' as 'C:/texlive/2015/texmf-dist/metapost/context/base/metafun.mpiv' using method 'default' metapost > initializing number mode 'scaled' metapost > error:

begingraph ! Improper `addto'. withpen draw->...:also(EXPR0)else:doublepath(EXPR0)withpen .currentpen.fi.op ( <> beginfig(270) draw begingraph( 3cm,2cm) gdraw "data1"; endgraph; endfig; ; ! Extra tokens will be flushed. ( <> beginfig(270) draw begingraph( 3cm,2cm) gdraw "data1"; endgraph; endfig; ; endgraph ! Isolated expression. ; <*> ...egingraph(3cm,2cm) gdraw "data1"; endgraph; endfig; ;



1 Answer 1


ConTeXt needs to redefine some of the definitions of graph module. To use these redefinitions, you have to use MP instance graph. To do so,

  • Add \usemodule[graph] in your preamble.
  • If you are using the MPcode environment, use \startMPcode{graph}.
  • If you are using MPpage environment, use \startMPpage[instance=graph].
  • You do not need to add begingraph(..) ... endgraph

Here is a minimal example:

draw begingraph(3in,2in);
    gdraw "data1.d";

draw begingraph(3in,2in);
    gdraw "data1.d";


where data1.d was your first example.

BTW, if you are starting the plotting in ConTeXt, I would highly recommend that you start with pgfplots. It works in ConTeXt with a few quirks.

I am a big advocate of Metapost, how well it is integrated with ConTeXt, and how fast it runs. But, when it comes to plotting, pgfplot is a lot more polished than the metapost graph module.

  • Thank you very much @Aditya. Both diagrams are visible. I will look into pgfplot
    – SGL
    Nov 2, 2015 at 8:00
  • @Aditya Is there a better graph library for Metapost that's comparable to pgfplots, or is anyone working on it?
    – James Qi
    Aug 13, 2020 at 20:48
  • @JamesQi No. There is nothing as feature rich as pgfplots. But you can use pgfplots in context.
    – Aditya
    Aug 14, 2020 at 2:41

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.