The beauty of Mayavi

Four charges with different magnitude plotted in 3D using Mayavi

In one of my earlier posts about Mayavi, I wrote about how you could visualize 2D field line plots using the flow function. At the end of that post I added that Mayavi is actually best at 3D plotting, and to follow up on that I’ll show you some of these plots with a few example Python scripts you might try out on your own.

First of all, you might want to know how to install Mayavi. For those lucky ones of you who have freed yourself and jumped on the Linux bandwagon, installing Mayavi should be quite easy. If you are using Ubuntu in particular, you may just install the package mayavi2 using either Synaptic or apt-get. If you are on Windows or Mac, you may either install Enthought’s own Python distribution (EPD) or give a shot at compiling on your own. Just note that EPD is quite expensive, even though all its components are open source, but if you are a student or academic user you could go ahead and download the academic version for free. It is basically the same as the commercial one, but with an academic license. (Kudos to Enthought for both making Mayavi open source, building an business model around it and still providing a great solution for students!)

Now, Enter 3D!

The way you do your plots in Mayavi depends on what you want to express. Most likely, you would prefer to show some simple plots giving just the necessary amount of information to tell you how the electric field behaves around your charges. A simple example of this is shown below:

Continue reading

Using Mayavi in Fys1120 oblig 1

3D plot for exercise 2d

There are only two days left before the first mandatory exercise in Fys1120 is due. Last time I posted about Mayavi I wrote in a comment that I would give you some hints on how to use it with the oblig. Actually, there are not so many useful applications for Mayavi in this particular oblig, since most plots are 2D and are better created using scitools.

In fact, the 3D plots in this oblig might also be plotted using scitools, but I decided to use Mayavi to test it out for this particular purpose.

Continue reading

Using Mayavi to visualize electric fields

Mayavi renders great field line plots.

While searching for a good Python module to visualize electric fields, I found Mayavi. Developed by Enthought, Mayavi is a very good module for visualizing a huge range of different scientific data sets. Everything from surfaces, flows and streamlines to bar charts, 3D plots and contour surfs are beautifully drawn on screen and exported to several file formats, such as PDF, PNG, EPS and more.

What I needed it for, however, was to visualize electric field lines in the course FYS1120 at the University of Oslo. We were told to use Matlab with the streamline and quiver functions, but even so, I wanted to use Python and decided to do a search and see if something similar was possible with Python. It took me some time to figure out how to use the scitools package to do streamline plots, but eventually I made it. However, these were a bit tedious to get working correctly and looked only about as good as the Matlab plots.

Continue reading

Using Python in the first MAT1120 oblig

The first “oblig” (mandatory exercise) in the subject MAT1120 is now available. I am trying to do as much work as possible in Python instead of Matlab, but as always this creates some extra effort when the subject is oriented around the latter.

Already in the first exercise there is a minor challenge, since the data file is not stored as a simple array, but as Matlab code. This means we need to rewrite this file to Python code or run it in Matlab and export it as data instead. As I am currently using a computer without Matlab installed and being to lazy to connect to a server with Matlab via remote desktop, I decided to do the latter. (I might add that I also wanted to see if I could do this without Matlab at all).

First of all, I figured the data was stored in the following manner:

Continue reading

WordPress in LaTeX glory

Speaking of LaTeX, if you, like us, want to write LaTeX math code in your blog, you should have a look at the LaTeX WP plugin.

The output will become something like this: (1 + (\frac{y}{a})^2)^{\frac{1}{2}} = 1 + \frac{1}{2}(\frac{y}{a})^2 + \ldots

Or maybe like this: f(x) = \sum_{n=0}^\infty \frac{f^{(n)}(0)}{n!}x^n = 1 + nx +  \frac{n(n-1)}{2}x^2 + \ldots

These are not as pretty as real \LaTeX output, but they sure are prettier than writing math the hard way:

f(x) = \sum_{n=0}^\infty \frac{f^{(n)}(0)}{n!}x^n = ...

I’m looking forward to be serving you with more math-stuff in the future!

LaTeX It! (and a new blog)

Together with my good friend, Mikael, I’ve started a blog about physics, chemistry, maths and technology over at Mindseye.no. Some of my postings over there will be dual posted here. Especially those about technical notes and tutorials. This is the first one, about LaTeX equations in Thunderbird.

If you are using Thunderbird for e-mail and want to send mathematical formulas to your contacts, you should consider the LaTeX It! plugin or the Equations plugin. The former requires you to have LaTeX and ImageMagick installed, while Equations uses an external server to generate your images.

There are a few pros and cons to both approaches. If you are using the Equations plugin, your images won’t be integrated in your e-mail as files, but will simply be linked to. You will see the images, of course, but if the server should crash or no longer be operative, your LaTeX formulas will become invisible.

This will probably not happen in the nearest future, but if you want to see your formulas forever, it might be a good idea to use LaTeX It! instead.

The problem with LaTeX It! is that it requires your computer to have the right set of packages installed. In Ubuntu this is no problem – you just install the packages texlive-latex-base, texlive-binaries and imagemagick using either Synaptic or by issuing the following command in a Terminal:

sudo apt-get install texlive-latex-base texlive-binaries imagemagick

In Windows you need to install LaTeX and ImageMagick by using the automatic installers, and you will also need GhostScript. On a Mac, however, I’ve not yet got the installation to work. You could try to see if everything is installable from MacPorts, but I have been unlucky with the converting, and would suggest you use the Equations plugin if everything else fails.

LaTeX It!

This is going to my first post in this blog, and along with some other more technical posts, I will dual post it in my own blog over at dragly.org.

If you are using Thunderbird for e-mail and want to send mathematical formulas to your contacts, you should consider the LaTeX It! plugin or the Equations plugin. The former requires you to have LaTeX and ImageMagick installed, while Equations uses an external server to generate your images.

Continue reading