Friday 9 August 2024

Matrix shading in Mathematica

Not exactly plotting, but at least data visualisation ... 

I have a matrix which looks as follows, generated here in Mathematica from a 4x4 identity matrix and a tensor product of Pauli matrices:


H := 5.907  IdentityMatrix[4] + .2183  KroneckerProduct[
    PauliMatrix[3], IdentityMatrix[2]] -
  6.125  KroneckerProduct[IdentityMatrix[2], PauliMatrix[3]] -
  2.143  (KroneckerProduct[PauliMatrix[1], PauliMatrix[1]] +
     KroneckerProduct[PauliMatrix[2], PauliMatrix[2]]);
 

H // MatrixForm

what I'd like to do is highlight the block diagonal bits - i.e. elements (1,1), (4,4) and the 2x2 block in the middle. From some searching of the internet (particularly this useful page) and the Mathematica documentation, I see that the Background attribute can be applied to a Grid view of the matrix:

bg = {None,
   None, {{1, 1} -> LightGreen, {4, 4} ->
     LightGreen, {{2, 3}, {2, 3}} -> LightGreen}};

Grid[H, Background -> bg]

which has indeed highlighted the elements that I wanted it to, but at the expense of no longer using the brackets of MatrixForm.  I you ask for this to be printed in MatrixForm, the background colouring disappears.  As far as I have been able to work out, the Background attribute cannot be combined with MatrixForm.  Oh well, the blocking is still as I wanted.

The syntax for Background is given in the hyperlink above.

Thursday 4 January 2024

Gnuplot 6.0.0

 I noticed that Gnuplot 6.0.0 is out.  Details of new features can be seen in the release notes.  I'm excited about the possibility of sector plots, in particular, it can make "polar equivalent to sparse-matrix heatmaps."  This is a kind of plot I do actually need to make for my research, and have outsourced the work to knowledgeable postdocs, or made somewhat ugly ones with matplotlib.  I'm keen to see what I can do with Gnuplot (while acknowledging that it is me, rather than matplotlib per se that stop me from making the plots I want to with mpl)

Monday 22 June 2020

Separate y-axis on the right

If you want to have two independent y-axis – one on the left, and one on the right – you can unset the mirroring of the "main" left-hand y-axis marks with set ytics nomirror, and use axis x1y2 in the plot command to use the right-hand y-axis.  See the following example for a basic unembellished example

gnuplot> set y2tics
gnuplot> set ytics nomirror
gnuplot> set ylabel "Energy [MeV]"
gnuplot> set y2label "{/Symbol b}_2"
gnuplot> set termopt enh
gnuplot> set xlabel "index"
gnuplot> plot 'ensurf.all' u 0:13 w l t "Binding Energy", '' u 0:4 axis x1y2 w l t "deformation ({/Symbol b}_2)"


and the result is shown in the screengrab below


Tuesday 3 July 2018

Dash it all: Custom line styles

Lines in gnuplot can come in different styles;  solid lines, dashed lines, or dotted lines, for example.  Probably the easiest way to set these is to use the dashtype pattern specifier, in gnuplot 5 and above.  The gnuplot help guide says:

       dashtype "pattern"  # string containing a combination of the characters
                           # dot (.) hyphen (-) underscore(_) and space.

and some examples are shown below:


set term pdfcairo font "Chalkduster,15"
set out 'dashes.pdf'
set key samplen 12 opaque box
PI = acos(-1.)
set xrange [0:2*PI]
plot sin(x) dashtype '-' lw 3 t 'Sin(x)', \
  sin(2*x) dashtype '.' lw 3 t 'Sin(2x)', \
  sin(3*x) dashtype '. -   _' lw 3 t 'Sin(3x)', \
0 not

Here we set up a pdf output, with a somewhat silly choice of font -- and one that I checked existed on my system.  I set up the key to include a very long length line in the legend, because I am making up an example with a complicated dash type.

The plot line plots the functions sin(nx), n=1,2,3 and assigns each a different dash type:  '-', '.' and '. -  _'for dashes, dots, and a more complicated pattern.  The results are shown below:


Friday 19 February 2016

A pm3d surface plot

Here is a quick example of visualising a function of two variables using the pm3d "palette-mapped" 3d plot.  It comes from a module I teach on computational techniques and is the end result of a solution of the Laplace equation for the electrical potential, in a square region in which the potential along the upper boundary of the square is held at +100V and is 0V along the other boundaries.  The data, in the file pot.dat, is here.
# set up the terminal to be the interactive wxt display, 
# but with equal width and height
set term wxt size 400,400

# we will be plotting a function of two variables, but we 
# don't want the default surface plot
unset surface

# view from above
set view 0,90

# set the coloured "pm3d" plot (rather than the surface, 
# which we unset above)
set pm3d

# viewing from above, we don't want to have the z-axis
# tick marks shown all bunched up
unset ztics

# now plot, with a title
splot 'pot.dat' title "Potential"
The result, loaded into gnuplot with a load './pot.gpl' command is shown below:


It's not perfect.  With a bit of tweaking with placement, the title could be centrally above the square of the plot and the whitespace more even in the frame, but it visualises the potential quite well.

Tuesday 12 January 2016

Quicker and dirtier one-line printing

In a previous post, I wrote about how to use a for loop and the system command to plot several data sets from different files in one go.  Taking that idea a bit further, one can make use of the dumb terminal type and a Unix pipe (assuming one is using a variety of Unix, such as OS X or Linux) to have a completely self-contained one-liner from a Unix shell.  This can be particularly useful for a quick way to check results running on a distant server that you want to connect to remotely via ssh and don't have the bandwidth to comfortably bring up graphical windows via X.

An example, building on the previous one, along with the result, is shown below:

% echo "set term dumb ; plot for [file in system('ls -1 */quadrupoles.res')] file u 1:4 w l t file" | gnuplot



  1200 ++---------+-----------+----------+----------+-----------+---------++
       *          +           +          +    66mev/quadrupoles.res ****** +
       *                                      69mev/quadrupoles.res ###### |
  1000 +*                                     70mev/quadrupoles.res $$$$$$++
       |*                                     71mev/quadrupoles.res %%%%%% |
       |#*                                                                 |
   800 ++*                                                                ++
       | *                                                                 |
       |  *                                                                |
   600 ++ #*                                                              ++
       |   *                              %                                |
       |   %*                         %%%%$$$$$$$$$$$$$$$$$$$$             |
       |    *                   %$############               $$$$$$$$$     |
   400 ++   #*               %##*******       ####                    $$$$++
       |     **          %******       ****      ##**                     $$
       |      **       ****                ***   **                        |
   200 ++      **    ***                      ***                         ++
       |        ** %**                                                     |
       +         ***          +          +          +           +          +
     0 ++---------+-----------+----------+----------+-----------+---------++
       0         100         200        300        400         500        600


Now, along with the plot for [file in ... construct, we have an extra gnuplot command set term dumb to make the output be character-based in the terminal window.  The Unix echo command prints the whole set of gnuplot instructions, and those are then piped to the gnuplot program using the Unix pipe symbol (|).

The dumb terminal by default uses a 'display' which is 79x24 characters, which is suitable for most default situations.  If you are running from a Terminal window in a windowing environment (the most common situation at the time of writing) then you can always drag your terminal to be much bigger and then request a more detailed plot.  The result is shown as a graphic below (unlike the above literal text result) so that it can be squashed down to fit in the blog post.  When used in anger, the font is just the same as the example above.  The difference in the command leading up to this figure is in writing set term dumb 300 75 to give 300 characters in the x direction and 75 in the y direction.

gnuplot textual plot


Tuesday 21 July 2015

A quick one-liner for plotting multiple files

I have a series of directories, each with a file of the same name, containing numerical data to do with calculations of nuclear fusion.  Each data set differs by the starting condition, and I'd like to be able to quickly show all the different calculations together, and see a visual update of them all, while my code is generating the files.

A quick one-liner, which does this is


plot for [file in system("ls -1 */qmom")] file u 0:1 w l t file


In this case, the file in each directory is called qmom.  The for loop syntax for [a in list] allows an arbitrary list of text strings to be looped over and assigned to a variable.  This allows for a really quick way to produce a plot comparing data from several files.

Of course, extra commands to prettify the plot to one's taste would probably be needed for a production plot.  The attached plot has had a bit of such stuff done (as well as adding labels etc.)