Plotting a graph of a function f, like f(x) = e^x, with MetaPost
![](https://secure.gravatar.com/avatar/8a87cf76bc6a6a9bf0d825141593b83f.jpg?s=120&d=mm&r=g)
Dear ConTeXt folks, I finally chose MetaPost/MetaFun [1] because of the native integration with ConTeXt and hopefully easy font handling. I want to use that throughout my document. Of course I hit the first problem. Wanting to draw a “simple” function f like f(x) = e^x, seems hard to accomplish. I guess I should stress that MetaPost does not have a high accuracy so it might not be the best solution [5]. There seem to be quite a lot of solutions (mostly macros) on the Web for this problem so I am wondering if one of them is integrated in MetaFun. Here are some of my findings. 1. Reading about the graph package by John D. Hobby [2] and which is also included in ConTeXt [3]. Especially I liked the automatic coordinate systems. The package seems to only be able to plot graphs with data points saved in files. So one solution would be to manually create data files with data points of the relevant function. 2. There is an example in the Drexel Physics Wiki [4]. 3. Anthony Phan has also created some macros in his `mps` [5 (currently not accessible)]. 4. André Heck uses the macro packages `courbes` and `grille` from Jean-Michel Sarlat to show some nice examples in his tutorial »Learning Metapost by Doing« [6]. So what do you suggest? Thanks, Paul [1] http://www.ntg.nl/pipermail/ntg-context/2011/058527.html [2] http://tug.org/docs/metapost/mpgraph.pdf [3] http://wiki.contextgarden.net/Metapost [4] http://einstein.drexel.edu/liki/index.php/Metapost#Graphing_Functions [5] http://www-math.univ-poitiers.fr/~phan/downloads/metapost/statsman.pdf [6] http://staff.science.uva.nl/~heck/Courses/mptut.pdf
![](https://secure.gravatar.com/avatar/8a87cf76bc6a6a9bf0d825141593b83f.jpg?s=120&d=mm&r=g)
Dear ConTeXt folks, Am Mittwoch, den 12.10.2011, 00:45 +0200 schrieb Paul Menzel:
Dear ConTeXt folks,
I finally chose MetaPost/MetaFun [1] because of the native integration with ConTeXt and hopefully easy font handling. I want to use that throughout my document.
Of course I hit the first problem. Wanting to draw a “simple” function f like f(x) = e^x, seems hard to accomplish. I guess I should stress that MetaPost does not have a high accuracy so it might not be the best solution [5].
There seem to be quite a lot of solutions (mostly macros) on the Web for this problem so I am wondering if one of them is integrated in MetaFun.
Here are some of my findings.
I just want to add that there seems to be work going on in ConTeXt to solve these issues as the talk at this years ConTeXt User Meeting of Alan Braslau suggests [7].
1. Reading about the graph package by John D. Hobby [2] and which is also included in ConTeXt [3]. Especially I liked the automatic coordinate systems. The package seems to only be able to plot graphs with data points saved in files. So one solution would be to manually create data files with data points of the relevant function.
2. There is an example in the Drexel Physics Wiki [4].
3. Anthony Phan has also created some macros in his `mps` [5 (currently not accessible)].
4. André Heck uses the macro packages `courbes` and `grille` from Jean-Michel Sarlat to show some nice examples in his tutorial »Learning Metapost by Doing« [6].
So what do you suggest?
Thanks, Paul
[1] http://www.ntg.nl/pipermail/ntg-context/2011/058527.html [2] http://tug.org/docs/metapost/mpgraph.pdf [3] http://wiki.contextgarden.net/Metapost [4] http://einstein.drexel.edu/liki/index.php/Metapost#Graphing_Functions [5] http://www-math.univ-poitiers.fr/~phan/downloads/metapost/statsman.pdf [6] http://staff.science.uva.nl/~heck/Courses/mptut.pdf [7] http://meeting.contextgarden.net/2011/talks/day1_06_alan_plotting/BassangeBr...
![](https://secure.gravatar.com/avatar/6114858d84b0f40cf715f12237ab67a1.jpg?s=120&d=mm&r=g)
On Wed, Oct 12, 2011 at 00:45, Paul Menzel wrote:
Dear ConTeXt folks,
I finally chose MetaPost/MetaFun [1] because of the native integration with ConTeXt and hopefully easy font handling. I want to use that throughout my document.
Of course I hit the first problem. Wanting to draw a “simple” function f like f(x) = e^x, seems hard to accomplish. I guess I should stress that MetaPost does not have a high accuracy so it might not be the best solution [5].
So what do you suggest? To wait for next metapost beta (or compile the version in trunk by yourself; however you might also need to change ConTeXt a bit, so that it will call metapost with the right parameters).
http://meeting.contextgarden.net/2011/talks/day1_09_taco_metapost/mp2talk.pd... Mojca
![](https://secure.gravatar.com/avatar/ac3fbaf0a287c4cffbc9f18fa931586c.jpg?s=120&d=mm&r=g)
Paul, Metapost can now perform calculations in double precision floating point. At this time, it is in the svn version and does not work yet as such integrated in ConTeXt (through mplib). Taco promises this for soon... The graph package is pretty clever, and I am working on proposals on how to extend/rewrite this taking advantage of the new metapost handling of large and small numbers. To plot a function, you can create a path, as in path p ; p := for i=0 upto 1000: hide (x := i/1000 ;) if (i>0) -- fi (x,exp(x)) endfor ; gplot p ; One proposal will be to make this even easier with new macros. Alan On Wed, Oct 12, 2011 at 12:45:12AM +0200, Paul Menzel wrote:
Dear ConTeXt folks,
I finally chose MetaPost/MetaFun [1] because of the native integration with ConTeXt and hopefully easy font handling. I want to use that throughout my document.
Of course I hit the first problem. Wanting to draw a “simple” function f like f(x) = e^x, seems hard to accomplish. I guess I should stress that MetaPost does not have a high accuracy so it might not be the best solution [5].
There seem to be quite a lot of solutions (mostly macros) on the Web for this problem so I am wondering if one of them is integrated in MetaFun.
Here are some of my findings.
1. Reading about the graph package by John D. Hobby [2] and which is also included in ConTeXt [3]. Especially I liked the automatic coordinate systems. The package seems to only be able to plot graphs with data points saved in files. So one solution would be to manually create data files with data points of the relevant function.
2. There is an example in the Drexel Physics Wiki [4].
3. Anthony Phan has also created some macros in his `mps` [5 (currently not accessible)].
4. André Heck uses the macro packages `courbes` and `grille` from Jean-Michel Sarlat to show some nice examples in his tutorial »Learning Metapost by Doing« [6].
So what do you suggest?
Thanks,
Paul
[1] http://www.ntg.nl/pipermail/ntg-context/2011/058527.html [2] http://tug.org/docs/metapost/mpgraph.pdf [3] http://wiki.contextgarden.net/Metapost [4] http://einstein.drexel.edu/liki/index.php/Metapost#Graphing_Functions [5] http://www-math.univ-poitiers.fr/~phan/downloads/metapost/statsman.pdf [6] http://staff.science.uva.nl/~heck/Courses/mptut.pdf
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
-- Alan Braslau CEA DSM-IRAMIS-SPEC CNRS URA 2464 Orme des Merisiers 91191 Gif-sur-Yvette cedex FRANCE tel: +33 1 69 08 73 15 fax: +33 1 69 08 87 86 mailto:alan.braslau@cea.fr
![](https://secure.gravatar.com/avatar/8a87cf76bc6a6a9bf0d825141593b83f.jpg?s=120&d=mm&r=g)
Dear Alan, Am Mittwoch, den 12.10.2011, 07:36 +0200 schrieb Alan Braslau:
Metapost can now perform calculations in double precision floating point. At this time, it is in the svn version and does not work yet as such integrated in ConTeXt (through mplib). Taco promises this for soon...
great to hear that. Just to clarify, with »now« you mean MetaPost v2, do not you? Taco, I hope you get there without a lot of problems showing up. If you need testers a good option would be to integrate that into ConTeXt Standalone as an option.
The graph package is pretty clever, and I am working on proposals on how to extend/rewrite this taking advantage of the new metapost handling of large and small numbers.
Perfect!
To plot a function, you can create a path, as in path p ; p := for i=0 upto 1000: hide (x := i/1000 ;) if (i>0) -- fi (x,exp(x)) endfor ; gplot p ;
What module do I need for `gplot`? Looking at the MetaFun manual [8] I found out MetaFun even provides a macro `function` which probably does exactly what you do above with the for loop. draw function(2,"x","x**(-0.5)",1,10,1) xyscaled (2cm,2cm) withpen pencircle scaled 5mm withcolor transparent(1,.5,yellow) ;
One proposal will be to make this even easier with new macros.
Another perfect! One thing coming to my mind though that hopefully the wheel is not reimplemented. I guess Gnuplot can do a lot already, but on the other probably not as neat as Metapost when we want to color certain areas. Thanks, Paul [8] http://www.pragma-ade.com/general/manuals/metafun-s.pdf
![](https://secure.gravatar.com/avatar/6114858d84b0f40cf715f12237ab67a1.jpg?s=120&d=mm&r=g)
On Thu, Oct 13, 2011 at 00:13, Paul Menzel wrote:
Am Mittwoch, den 12.10.2011, 07:36 +0200 schrieb Alan Braslau:
Metapost can now perform calculations in double precision floating point. At this time, it is in the svn version and does not work yet as such integrated in ConTeXt (through mplib). Taco promises this for soon...
great to hear that. Just to clarify, with »now« you mean MetaPost v2, do not you?
No, 1.750. The version 2 is not quite ready yet, but the version 1.750 already deals with floating point numbers.
Taco, I hope you get there without a lot of problems showing up. If you need testers a good option would be to integrate that into ConTeXt Standalone as an option.
It will become part of distribution as soon as it gets out, but Taco was busy enough organizing conference and dozens of other things. Just give him some time. I bet that he also wants to update documentation, do final bugfixing etc. instead of just releasing a random snapshot.
One thing coming to my mind though that hopefully the wheel is not reimplemented. I guess Gnuplot can do a lot already, but on the other probably not as neat as Metapost when we want to color certain areas.
Can you please specify your question more precisely? Gnuplot terminal uses metapost output. Gnuplot has some support for transparency, even though I'm not exactly sure if one can specify transparent colors for lines, only for fills. But you can always simply redefine line type colors to be transparent. Gnuplot brings all kinds of other problems like dependency on external tool. If you have enough time to create nice plots in metapost, it is better to avoid external dependency as a general rule. But if you have more questions about it, feel free to ask. I might be able to help you. The ConTeXt terminal was almost included until developers realized that they wanted me to throw away some code for handling units and replace that code with something buggy (like "size 10cm,10" -> (10cm,10cm); "size 10,10cm" -> (0.35cm,10cm) or (25.40cm,10.00cm) depending on weather). Now I need to either rewrite part of my code or rewrite part of core code or convince them why keeping the old code. Mojca
![](https://secure.gravatar.com/avatar/8a87cf76bc6a6a9bf0d825141593b83f.jpg?s=120&d=mm&r=g)
Am Donnerstag, den 13.10.2011, 00:28 +0200 schrieb Mojca Miklavec:
On Thu, Oct 13, 2011 at 00:13, Paul Menzel wrote:
Am Mittwoch, den 12.10.2011, 07:36 +0200 schrieb Alan Braslau:
Metapost can now perform calculations in double precision floating point. At this time, it is in the svn version and does not work yet as such integrated in ConTeXt (through mplib). Taco promises this for soon...
great to hear that. Just to clarify, with »now« you mean MetaPost v2, do not you?
No, 1.750. The version 2 is not quite ready yet, but the version 1.750 already deals with floating point numbers.
Thank you for the clarification/correction.
Taco, I hope you get there without a lot of problems showing up. If you need testers a good option would be to integrate that into ConTeXt Standalone as an option.
It will become part of distribution as soon as it gets out, but Taco was busy enough organizing conference and dozens of other things. Just give him some time. I bet that he also wants to update documentation, do final bugfixing etc. instead of just releasing a random snapshot.
Of course the user would have to make a conscious decision by enabling this “beta” release. If people could test this and with the right announcement I hope that some people would step up to test and contribute by submitting bug reports or writing documentation.
One thing coming to my mind though that hopefully the wheel is not reimplemented. I guess Gnuplot can do a lot already, but on the other probably not as neat as Metapost when we want to color certain areas.
Can you please specify your question more precisely?
I meant coloring part of the area below a graph. For example if you look at the graphic for Riemann sums on page 51 of the document »Learning MetaPost by Doing« [6].
Gnuplot terminal uses metapost output. Gnuplot has some support for transparency, even though I'm not exactly sure if one can specify transparent colors for lines, only for fills. But you can always simply redefine line type colors to be transparent.
Gnuplot brings all kinds of other problems like dependency on external tool. If you have enough time to create nice plots in metapost, it is better to avoid external dependency as a general rule. But if you have more questions about it, feel free to ask. I might be able to help you.
Well my only question would be, why should someone use Gnuplot then? Is it only for people already knowing Gnuplot? Or also because it is a little less work to define the coordinate system and legends? Otherwise MetaPost seems to include all functionality Gnuplot offers and seems to offer more possibilities, does not it?
The ConTeXt terminal was almost included until developers realized that they wanted me to throw away some code for handling units and replace that code with something buggy (like "size 10cm,10" -> (10cm,10cm); "size 10,10cm" -> (0.35cm,10cm) or (25.40cm,10.00cm) depending on weather). Now I need to either rewrite part of my code or rewrite part of core code or convince them why keeping the old code.
I remember a thread about that [9]. Thanks, Paul [6] http://staff.science.uva.nl/~heck/Courses/mptut.pdf [9] http://www.ntg.nl/pipermail/ntg-context/2010/054196.html
![](https://secure.gravatar.com/avatar/6114858d84b0f40cf715f12237ab67a1.jpg?s=120&d=mm&r=g)
On Thu, Oct 13, 2011 at 01:19, Paul Menzel wrote:
Am Donnerstag, den 13.10.2011, 00:28 +0200 schrieb Mojca Miklavec:
On Thu, Oct 13, 2011 at 00:13, Paul Menzel wrote:
Am Mittwoch, den 12.10.2011, 07:36 +0200 schrieb Alan Braslau:
Metapost can now perform calculations in double precision floating point. At this time, it is in the svn version and does not work yet as such integrated in ConTeXt (through mplib). Taco promises this for soon...
great to hear that. Just to clarify, with »now« you mean MetaPost v2, do not you?
No, 1.750. The version 2 is not quite ready yet, but the version 1.750 already deals with floating point numbers.
Thank you for the clarification/correction.
Taco, I hope you get there without a lot of problems showing up. If you need testers a good option would be to integrate that into ConTeXt Standalone as an option.
It will become part of distribution as soon as it gets out, but Taco was busy enough organizing conference and dozens of other things. Just give him some time. I bet that he also wants to update documentation, do final bugfixing etc. instead of just releasing a random snapshot.
Of course the user would have to make a conscious decision by enabling this “beta” release.
And of course the user makes a conscious decision to either compile from trunk if he wants the latest and greatest bugs and new features or to wait patiently until Taco thinks the software is ready and releases a beta. I don't think that one extra week (or two or three or ... for that matter) makes any difference.
If people could test this and with the right announcement I hope that some people would step up to test and contribute by submitting bug reports or writing documentation.
If people could test LaTeX 3 ... they would most certainly send bug reports and write docs. There will be more than enough time for testing before version 2 is out. Those who want to help writing documentation are already free to do so. The code already works.
One thing coming to my mind though that hopefully the wheel is not reimplemented. I guess Gnuplot can do a lot already, but on the other probably not as neat as Metapost when we want to color certain areas.
Can you please specify your question more precisely?
I meant coloring part of the area below a graph. For example if you look at the graphic for Riemann sums on page 51 of the document »Learning MetaPost by Doing« [6].
I'm not sure how to draw discrete function, but you can try out the following: set style fill solid border -1 f(x)=(x-5)*sin(x)-cos(x)+2 plot [1:7] f(x) w filledcurve y1=0,\ '-' using 1:(f($1)) w boxes lt 0, f(x) lt -1 lw 3 1.5 2.5 3.5 4.5 5.5 6.5 e (But of course you need to draw everything else as well.)
Gnuplot terminal uses metapost output. Gnuplot has some support for transparency, even though I'm not exactly sure if one can specify transparent colors for lines, only for fills. But you can always simply redefine line type colors to be transparent.
Gnuplot brings all kinds of other problems like dependency on external tool. If you have enough time to create nice plots in metapost, it is better to avoid external dependency as a general rule. But if you have more questions about it, feel free to ask. I might be able to help you.
Well my only question would be, why should someone use Gnuplot then? Is it only for people already knowing Gnuplot? Or also because it is a little less work to define the coordinate system and legends?
It is much less work to define and draw coordinate system, choose the right units, draw logarithmic plots, parse input data, (past tense: do arithmetics with numbers above 4000), ...
Otherwise MetaPost seems to include all functionality Gnuplot offers and seems to offer more possibilities, does not it?
MetaPost doesn't know erf and other obscure functions, it wasn't able to calculate with big numbers and it is very inconvenient to parse input data. With gnuplot you get fast plotting, data parsing, range guessing, function calculations ... all "for free", but you have slightly less flexibility in drawing. For me the most important part is that I have to use gnuplot for inspecting the data anyway (I would never use metapost to quickly inspect whether my measurement went fine or not) and then I just copy-paste the function call. The trade-off is still in favour of gnuplot, in particular since the quality is acceptable and I don't have any special needs and extra time to do fancy stuff with plots. What I really miss in Gnuplot is 3D support, but that one is not covered in MetaPost either.
I remember a thread about that [9].
That was not the same. The last context-terminal-was-almost-included was something recent (less than a month ago). Mojca
![](https://secure.gravatar.com/avatar/49e63acb01f2ca80efce7eed08310ce8.jpg?s=120&d=mm&r=g)
On 13-10-2011 02:30, Mojca Miklavec wrote:
Of course the user would have to make a conscious decision by enabling this “beta” release.
And of course the user makes a conscious decision to either compile from trunk if he wants the latest and greatest bugs and new features or to wait patiently until Taco thinks the software is ready and releases a beta.
I don't think that one extra week (or two or three or ... for that matter) makes any difference.
Also, it's not a matter of enabling but more making a drastic move... there is a complete infrastructure for switching the underlying handling of numbers and if there are bugs in the common layer it will influence traditional mp methods too. As mplib is a core component of luatex/mkiv it is unlikely that there will be a beta that has not been tested by taco and me on a bunch of documents that we normally use for testing drastic changes. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
![](https://secure.gravatar.com/avatar/ac3fbaf0a287c4cffbc9f18fa931586c.jpg?s=120&d=mm&r=g)
On Thu, Oct 13, 2011 at 02:30:07AM +0200, Mojca Miklavec wrote:
MetaPost doesn't know erf and other obscure functions
Metapost could possibly be extended to provide all of the functions in the standard math library. In fact, it would be nice if there be some hook to `link' to additional functions, written in C or in Fortran, for example. (I regularly use the complex error function.) Maybe this could be possible in luatex, made available to metapost through some communication pipe? Alan
![](https://secure.gravatar.com/avatar/386a116d129cdc597f2bb6210bbaff7a.jpg?s=120&d=mm&r=g)
On Thu, Oct 13, 2011 at 02:30:07AM +0200, Mojca Miklavec wrote:
MetaPost doesn't know erf and other obscure functions
Metapost could possibly be extended to provide all of the functions in the standard math library. In fact, it would be nice if there be some hook to `link' to additional functions, written in C or in Fortran, for example. (I regularly use the complex error function.) Maybe this could be possible in luatex, made available to metapost through some communication pipe?
yes, just build a binding for the library (or use something else like cinvoke) http://www.gust.org.pl/bachotex/2011-en/presentations/Scarso_2_2011
On Thu, Oct 13, 2011 at 9:59 AM, Alan Braslau
![](https://secure.gravatar.com/avatar/386a116d129cdc597f2bb6210bbaff7a.jpg?s=120&d=mm&r=g)
On Wed, Oct 12, 2011 at 12:45 AM, Paul Menzel
Dear ConTeXt folks,
I finally chose MetaPost/MetaFun [1] because of the native integration with ConTeXt and hopefully easy font handling. I want to use that throughout my document.
Of course I hit the first problem. Wanting to draw a “simple” function f like f(x) = e^x, seems hard to accomplish. I guess I should stress that MetaPost does not have a high accuracy so it might not be the best solution [5].
There seem to be quite a lot of solutions (mostly macros) on the Web for this problem so I am wondering if one of them is integrated in MetaFun.
Here are some of my findings.
1. Reading about the graph package by John D. Hobby [2] and which is also included in ConTeXt [3]. Especially I liked the automatic coordinate systems. The package seems to only be able to plot graphs with data points saved in files. So one solution would be to manually create data files with data points of the relevant function.
2. There is an example in the Drexel Physics Wiki [4].
3. Anthony Phan has also created some macros in his `mps` [5 (currently not accessible)].
4. André Heck uses the macro packages `courbes` and `grille` from Jean-Michel Sarlat to show some nice examples in his tutorial »Learning Metapost by Doing« [6].
So what do you suggest? In MKIV you can use Lua to make calculations in floating point, and Metapost to draw the data.
-- luigi
participants (5)
-
Alan Braslau
-
Hans Hagen
-
luigi scarso
-
Mojca Miklavec
-
Paul Menzel