Posts mit dem Label mathematica werden angezeigt. Alle Posts anzeigen
Posts mit dem Label mathematica werden angezeigt. Alle Posts anzeigen

Montag, 18. Juli 2011

Sharing data among Mathematica notebooks

With time my Mathematica-notebooks increase in size and complexity. That's when I decide to split my notebooks into several files. Also, sometimes I want my results out of the notebooks which possibly change upon each evaluation.

One way is to create a package stored to a notebook (.nb-file) which is convenient for function definitions. Another way is to write Mathematica expressions to an ASCII-file with the suffix ".m" with the command "Save".

Here, I have some expressions:

f[x_] := x Log[x]
a = Table[f'[i], {i, 0.1, 1, 0.1}]
p = ListPlot[a]
ascii = FromCharacterCode[Range[32, 126]]

With this command I can retrieve the names of the expressions in the "Global" scope, or context:

Names["Global`*"]

And finally, this is how I store all expressions which have names starting with "a" to a .m-file:

mfile = NotebookDirectory[]~StringJoin~"results.m"
Save[mfile, #] & /@ Names["Global`a*"]

In another notebook I can load these expressions like this

<<results.m;

The .m-file itself looks this way:

a = {62, 58, 46, 115, 47, 77, 110, 111, 72, 94}
ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

Freitag, 8. Juli 2011

How to create publication style figures with Mathematica

Recently, I've been using Mathematica a lot - both for data processing and analysis as well as for plots. Plotting is easily done and quite selfexplaining, or at least well documented. Publication style figures of plots have little more demands: labled axes, legends, etc. I tinkered around quite a lot with such figures and I'd like to share my output:

As said a good looking plot can be created quite fast:

<< PlotLegends`

l1 = RandomReal[{2900, 3100}, 10]

fig1 = ListPlot[{l1, l2},
  Joined -> True, PlotStyle -> {Red, Green},
  PlotMarkers -> Automatic,
  PlotLegend -> {"list1", "list2"}, LegendShadow -> None,
  LegendPosition -> {1.1, -0.25}
  ]




A figure with two plots and the legend having its own part requires some more code:

pm = {Graphics[{Red, Disk[ImageScaled[{0.5, 0.5}], .025]}],
  Graphics[{Green, Disk[ImageScaled[{0.5, 0.5}], .025]}]}
pm2 = {
  Graphics[{Red, Disk[ImageScaled[{0.5, 0.5}], .25]}],
  Graphics[{Green, Disk[ImageScaled[{0.5, 0.5}], .25]}]
  }

fig = Show[
  GraphicsRow[{
    ListPlot[{l1, l2}, Joined -> True, PlotMarkers -> pm,
     PlotStyle -> {Red, Green}],
    Graphics[
     Legend[Transpose[{pm2, {"list1", "list2"}}],
      LegendShadow -> None]],
    ListPlot[l3, Joined -> True, PlotStyle -> Black,
     PlotMarkers -> Automatic]},
   ImageSize -> Full
   ],
  Graphics[Text[Style["(a)", 14], {0, 20}]],
  Graphics[Text[Style["(b)", 14], {400, 20}]],
  Graphics[Text[Style["(c)", 14], {800, 20}]](*,Frame->True*)
  ]



Unfortunatly, the figures shown are in "PNG" format and they do not look as nice as in "EPS" format. (For some reason I can't upload "EPS" formatted pictures.)