Trinh @ Bath

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
it_matlab_figures [2025/02/07 16:06]
trinh
it_matlab_figures [2025/05/02 18:40] (current)
trinh
Line 3: Line 3:
 Here are some brief details and tips for developing a good workflow for generating publication-quality images.  Here are some brief details and tips for developing a good workflow for generating publication-quality images. 
  
-===== A workflow for saving images =====+===== 1. A workflow for saving images =====
  
 As you become a more experienced researcher, you will learn the value of developing a workflow where images can be easily re-generated. This means that you can easily replot the images when you are finalising the presentation.  As you become a more experienced researcher, you will learn the value of developing a workflow where images can be easily re-generated. This means that you can easily replot the images when you are finalising the presentation. 
Line 11: Line 11:
   - **Tip: save your figures as .fig.** Similar to above, you may want to save ''%%.fig%%'' files of the pictures. Again, it allows fine-tuning. (In fact, you can extract raw data from ''%%.fig%%'' files via  scripts if you look into it). To save such a file, simply click File > Save-As on the figure window.   - **Tip: save your figures as .fig.** Similar to above, you may want to save ''%%.fig%%'' files of the pictures. Again, it allows fine-tuning. (In fact, you can extract raw data from ''%%.fig%%'' files via  scripts if you look into it). To save such a file, simply click File > Save-As on the figure window.
  
-===== Exporting figures =====+===== 2. Exporting figures =====
  
 Traditionally (meaning literally in the last 20 years), Matlab has had very poor ability to generate publication-quality images. Problems include: poor bounding boxes, weird rasterisation effects, weird layering issues, etc.  Traditionally (meaning literally in the last 20 years), Matlab has had very poor ability to generate publication-quality images. Problems include: poor bounding boxes, weird rasterisation effects, weird layering issues, etc. 
Line 29: Line 29:
 in order to produce a PDF of the currently focused figure. in order to produce a PDF of the currently focused figure.
  
-===== Annotations =====+===== 3. Annotations =====
  
 In order to add annotations to figures, you have a few options: In order to add annotations to figures, you have a few options:
Line 38: Line 38:
  
  
-===== Advanced =====+===== 4. Advanced image generation using TikZ =====
  
 More 'advanced' figure generation can involve things like [[https://en.wikipedia.org/wiki/PGF/TikZ|TikZ]], which generates figures natively within LaTeX. The advantage of this approach is consistency in annotation.   More 'advanced' figure generation can involve things like [[https://en.wikipedia.org/wiki/PGF/TikZ|TikZ]], which generates figures natively within LaTeX. The advantage of this approach is consistency in annotation.  
  
-Post-2010 there have been very powerful scripts that allow Matlab figures to be automatically converted to TiKZ, such as [[https://uk.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz|matlab2tikz]].+There are now very powerful scripts that allow Matlab figures to be automatically converted to TiKZ, such as [[https://uk.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz|matlab2tikz]]. Once your figure has been generated, you can run:  
 + 
 +<code matlab> 
 +matlab2tikz 
 +</code> 
 + 
 +Save the file, either as a .tex or as a .tikz. If you save it as a .tex, you can compile the file to generate the images.
  
 If you are an undergrad student or under pressure to write a report, I don't recommend trying to learn this! If you are an undergrad student or under pressure to write a report, I don't recommend trying to learn this!
  
 +===== 5. Colormaps =====
 +
 +The default colormaps are not so good. Use [[https://uk.mathworks.com/matlabcentral/fileexchange/45208-colorbrewer-attractive-and-distinctive-colormaps|ColorBrewer]] to generate a beautiful colormap. Add the package folder to your [[it_matlab_startup|Matlab startup]] functions folder. Then in my `startup.m` file, I add the following: 
 +
 +<code matlab>
 +set(0,'DefaultFigureColormap', brewermap([],'*RdYlBu'));
 +</code>
 +
 +You can view all the colormaps with 
 +
 +<code matlab>
 +brewermap('demo'
 +</code>
 +
 +Open via `open brewermap` to view the documentation and examples.