Monday, November 22, 2010

compiler options in QtCreator

Selecting the release build over the default debug build will enable the -O2 compiler option but under Projects settings you can only add make options. To add more compiler options such as -march=core2 you can add a line to your project file:

QMAKE_CXXFLAGS += -march=core2

Wednesday, November 10, 2010

Working with a second y-axis in matplotlib

Recently I had to create a plot with data on a second y-axis in matplotlib (python). This is actually more involved than I expected. After googling around I put this together

First you store the axes object returned from a subplot(111) command


then you plot as usual your data which has to go to the first y-axis. Labels you have to set for the ax1 object:

ax1.set_xlabel(r"Position ($\mu$m)",fontsize=20)
ax1.set_ylabel("Concentration (wt %)",fontsize=20)

The second y-axis is created by creating a twin of your current axes object. The twin is actually not identical but has a mirrored y-axis (i.e. your second y-axis).

ax2.set_label("Other conc (wt%)",fontsize=20)

So far so good. Normally I use the follow function to change the size of text of the ticks

def setTickSize(size=None):
 """Set the tick label size (both x and y axis) to the specified value or 
 default to 'large'
 possible values are 'small', 'medium', 'larger', 'x-large', 'xx-large'
 'x-small', 'xx-small' or integer values
 Use this function only when you are finished adding curves. If curves
 change the axes ranges the ticks get screwed up (sorry)

 if size is None:  #default large
 elif type(size) == str:
  if size not in ['small', 'medium', 'larger', 'x-large', 'xx-large','x-small', 'xx-small']:
   print "Wrong fontsize specifier given, use:"
   print 'small', 'medium', 'larger', 'x-large', 'xx-large', 'x-small', 'xx-small'
   print 'defaulting to large'
 elif type(size) != int:
  print "Wrong fontsize specifier given, use:"
  print 'small', 'medium', 'larger', 'x-large', 'xx-large', 'x-small', 'xx-small'
  print 'or an integer value'
  print 'defaulting to large '
 pylab.xticks(tmp[0],[str(int(x)) for x in tmp[0]], size=size)
 pylab.yticks(tmp[0],[str(int(x)) for x in tmp[0]], size=size)
You have to change the type cast int(x) into something else if you want floating point values on your axes. However, it turns out that it is near impossible to do this for the ticks on the second y-axis. After some more googling I found a much easier alternative. Matplotlib can read settings from a user-preferences file somewhere in the user-home directory. Somehow I can never find this file (I use a lot of different windows/linux/python combinations). But from a script you can also set these preferences! Which of course then only apply to the remainder of the script:

import matplotlib as mpl

The nice part is that the labelsize applies to both your first and second y-axis. You can find more info on the matplotlibrc file on the sourceforge matplotlib site

The only issue I could not solve is to create one legend for the curves on both axes. If you legend() you only get the legend for the current active axes. On windows calling it again just replaced the other legend on linux they were both displayed on top of eachother (which gives you the option to use e.g. legend(loc=2) for the left y-axis and legend(loc=1) for right y-axis).

Sunday, September 26, 2010

Change legend line width in Qwt

In the past I made a small program that basically just plots a number of columns from a text file. This program is written using the Qwt plotting library. The program can simultaneously plot the columns from multiple text files. The different columns in a file are given different colours and for columns from different files a different line style is used (normal, dashed, dotted or dash-dot). A legend showing which line is which is also displayed. Here I had the problem that with the default legend properties the width of the line shown in the legend is so small that you can not distinguish between a normal and a dashed or dashed dotted line. After some searching on the web I found a good description of how to change the width of the line in the legend. If you follow the link you find that they call the line section displayed the legend icon (that is why it took me some time to find that website). In the end I modified their code a little bit. My program was using plain QwtPlotCurve variables. So I made a derived class FractionCurve which implements the updateLegend function. The total code of this very simple custom Curve class is given below. Because my program uses the QwtPlotCurve constructor that takes a QString as an argument I also had to implement that constructor for the derived FractionCurve class.

#include <qwt_legend.h>

#include <qwt_legend_item.h>

class FractionCurve : public QwtPlotCurve



void updateLegend(QwtLegend *legend) const;

FractionCurve(const QString);


FractionCurve::FractionCurve(const QString s) : QwtPlotCurve(s)




void FractionCurve::updateLegend(QwtLegend *legend) const



QwtLegendItem *lgdItem =

dynamic_cast<QwtLegendItem*>(legend->find( this ));

if ( lgdItem )



And an example of a plot with the new legend:

Saturday, August 14, 2010

Nice syntax highlighting in Blogger

There is an easy way to make source code snippets look great on your blog. You have to change your template once and after that you can just surround your code with a simple tag. One thing you must not forget to do is to "escape" your source code so it only contains valid html code. Manually this means replacing any less than sign with appropriate html code.

Everything is nicely explained here:

syntax highlighting tutorial

and here is a direct link to a site to escape your html

quick escape

Sunday, August 8, 2010

Reading a vtk file with multiple scalar fields

Sometimes I come across data files in .vtk format. One of these files stores multiple scalar values in a single file, where the scalar are stored as structured points. For example I received a file containing quantA, quantB and quantC. I would like to read in these files and convert them to something else. So the main goal was to obtain arrays with the 3 scalar fields. The code is not yet complete but the reading is quite involved and it took me a lot of time to patch the required information together from all different examples and the DOxygen online manual of VTK (which I have come to dislike to a great extend). Perhaps the VTK books would have helped me out here but I am not willing to buy them just for this single application of VTK.

Okay here we go:
//First you have to create a reader object:
vtkSmartPointer < vtkstructuredpointsreader> reader = vtkSmartPointer < vtkstructuredpointsreader>::New();
//Now we can get the number of scalars in the file
vtkIdType numScal=reader->GetNumberOfScalarsInFile();
std::cout << "number of scalars in file: " << numScal << std::endl;
//Show which scalars we have
int i;
for(i=0;i< numScalComp;i++){
  std::cout << "Scalar "<< i <<": " << reader->GetScalarsNameInFile(i)<< std::endl;
//To read a certain scalar field we have to tell the reader which one we want
//we do that by name in this example we take the third one
reader->Update();  //I think this actually makes the reader do something
//the rest is what vtk people call setting up the pipeline
//To get to the point data we need a few intermediate steps and objects
vtkStructuredPoints* structuredPoints = reader->GetOutput();
vtkPointData *pd=structuredPoints->GetPointData();
vtkDataArray *scalars ;
//here the same name as selected in the reader must be used!!
//I tried this line with the SetScalarsName call to the reader and
//that doesn't work.
//Now we can access the points for example as
vtkIdType numPoints = structuredPoints->GetNumberOfPoints();
for(i=0;i< numPoints;i++){
   std::cout<< i<< ": " << scalars->GetComponent(i,0)<< std::endl;
The next step is to write a .vtk file from my own data. The mayavi2 and ParaView programs look quite nice for imaging of 3d data.

Sunday, March 28, 2010

Weird experience with Gmail

Today I tried to login to my gmail account using chrome under Ubuntu (I always use https). After trying for a period of 15 minutes during which it kept telling me that the website was unavailable I was about to ask my wife if she alaso had problems reaching her gmail. Then I tried firefox and I could login immediately. I tried Chrome once more and still could not login. Now I can use chrome to login to gmail as well.