I have produced the following spectrogram in MATLAB and I am trying to import it into a LaTeX document after using matlab2tikz. I am fully aware of the issues with maximum memory etc for typesetting, and how to get around them by using LuaLaTeX and to speed everything up by using externalise. The problem is that because the spectrogram that I wish to plot is very detailed, it is far too big even for matlab2tikz to deal with, and I receive a number of errors when trying to save the spectrogram as a tikz file. The underlying matrix of the spectrogram is a 20001x90 complex double.

Is there any way that you can think of to import such a high detail spectrogram into a LaTeX document without the obvious exporting as an EPS - I really don't want to do this as it looks so much worse!

my spectrogram

  • 2
    Check out Section 4.3.7 of the pgfplots manual. I don't have the signal processing toolbox, so I can't test, but you should be able to remove/hide the axis and colormap of the figure, then export the bare graphic as PDF or another vector format (no loss of quality). This can be dropped into an axis/colorbar created with pgfplots. You can use the jet colormap to match the MATLAB color levels. Mar 11, 2016 at 3:06

2 Answers 2


pgfplots has a graphics plot type to handle this scenario. We can export a bare plot (no axis, labels, or anything) as a vector format, then have pgfplots lay an axis atop it.

This way, there is no loss of quality (we used vector export from MATLAB), we have access to all of pgfplots' features, but we've offloaded the heavy-duty computation/memory usage from TeX to MATLAB.

I don't have the signal processing toolbox, but this general method should be adaptable to the MATLAB figure produced by that toolbox.

Here's the MATLAB code. The first section produces a random 3D figure that we can work with for the rest of this answer. The second section removes everything we don't want exported (axis lines, labels, etc.). The third section outputs the vector file. MATLAB does offer a dpdf output option, but it outputs a full-page PDF with margins and no CropBox/TrimBox information. So it's easier to output to EPS (cropped) and convert to PDF externally (here, I call ImageMagick command line tools from within MATLAB to do the conversion). The final section of MATLAB code is optional: I just use this to quickly extract and print all the axis/colorbar limits. These will be needed in a moment when we get to pgfplots.

% These steps are needed for my example; replace with your actual spectrogram.
peaks;        % make a random 3d plot
view(0,90);   % adjust view to match spectrogram
colormap jet; % set colormap (match this in pgfplots)
title('');    % clear title

% Clean plot of any labels/annotations/text
colorbar off; % turn off colorbar (does nothing in this example)
shading flat; % remove any grid (might not be needed)
axis off;     % turn off axes

% Output file
fname = '___mydemo';  % base name; choose anything you like
print('-depsc2',fname); % print to eps (can't go direct to pdf without trimming)
system(['convert ' fname '.eps ' fname '.pdf']); % convert to pdf (ImageMagick)

% Print min/max values for use in pgfplots
ax = gca;
fprintf('xmin: %6.3f\nxmax: %6.3f\n',ax.XLim); % x limits
fprintf('ymin: %6.3f\nymax: %6.3f\n',ax.YLim); % y limits
fprintf('zmin: %6.3f\nzmax: %6.3f\n',ax.CLim); % colorbar limits

In LaTeX, I created a mlspectrogram style that might be useful to keep consistent settings if you are plotting lots of these. I also use siunitx for units in the axis labels.

Then we can use \addplot graphics {fname}; inside our mlspectrogram axis. The set of options immediately following \addplot are used to specify the limits of the color bar, and the set of options immediately following graphics are used to specify the axis limits of the file being plotted.

  unit markings=parenthesis,
  unit code/.code 2 args={\si{#1#2}},
    enlargelimits=false, axis on top, use units,
    xlabel=Time, x unit=\second,
    ylabel=Frequency, y unit=\kilo\hertz,
    colormap/jet, colorbar,
    colorbar style={ylabel=Power/Frequency, y unit=\decibel\per\hertz},

\addplot % min and max values on following 2 lines are lifted from MATLAB output
  [point meta min=-6.5466,point meta max=8.0752] 
  graphics[xmin=-3,xmax=3,ymin=-3,ymax=3] {___mydemo};

enter image description here


I don't have enough reputation to comment. Have you tried export_fig (https://github.com/altmany/export_fig) to export this image to EPS or PDF?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .