MATLAB Answers


How to create a graph

Asked by John
on 1 Oct 2012


Could somebody advise me on how you would produce a graph like this in matlab

Thank you

  1 Comment

plot a graph from what?



No products are associated with this question.

2 Answers

Answer by per isakson
on 1 Oct 2012
Edited by per isakson
on 31 Oct 2012
 Accepted answer

My approach, which requires some work,

  • put data in a 2D array, C
  • plot with the function, image
  • Image type: Indexed (colormap)
  • Define a colormap, with a special slot, e.g. 1, for "missing data", white in your case. The length of the colormap = 64 is enough.
  • Map C carefully to the colormap, e.g. C(ii,jj)=1 for "missing data"
  • User defined tick-labels on the axes
  • and more

--- in response to comments I try to expand a bit ---


  • cmap is a colormap of size [64x3]. It associates the numbers [1,2,..., 64] to 64 different colors. (64 is an example.)
  • D is your 2D data array
  • C is an array of the same size as D. The values of C are integers in the interval, [1,64].

You create a suitable colormap, cmap, with the colormapeditor. You add some specific colors to signal special values of D, e.g. "missing data", outliers of various kinds etc.

You define a function, which takes D as input and returns C, e.g.

    function C = D2C( D )
        C = zeros( size(D) ); % zeros will cause error if not replaced
        C( isnan(D) )   = 1; % nans will e displayed with color #1
        C( D >= upper ) = 2; % values above upper gets color #2
        C( D <= lower ) = 3; % values below lower gets color #3
        any command is ok as long as the values of C are integers [1,64]
        assert( not( any( C(:)==0 ) ), .... )

and to display the graph

    colormap( cmap )
    image( C )


--- in response to John's comment on 19 Oct 2012 at 16:45 ---

Here is a start to make a graph that resembles

    %% Create some data 
    N = 1000; 
    Speed       = transpose( logspace( 0, 2, N ) );  
    Acc         = randn( N, 1 ) .* ( exp( - sqrt( 10*Speed/N ) ) );  
    Efficiency  = ( Acc .* Speed );
    nAccBins    = 20;   % Set resolution of image
    nSpeedBins  = 30;
    Speed_upper = 100;
    Speed_lower = 0;
    Acc_upper   = 3;
    Acc_lower   = -3;
    ix_Speed    = ceil( nSpeedBins * ( Speed - Speed_lower )        ...
                                   / ( Speed_upper - Speed_lower ) );
    ix_Acc      = ceil( nAccBins * ( Acc - Acc_lower )       ...
                                 / ( Acc_upper - Acc_lower ) );
    M   = accumarray( { ix_Speed, ix_Acc }, Efficiency, [], @mean, nan );
    imh = imagesc( transpose( M ) );
    axh = ancestor ( imh, 'Axes'  );
    set( axh, 'YDir', 'normal' )

Next step would be to map M to a special colormap with something like

    C = D2C( M );

and display with

    colormap( cmap )
    image( C )



  • axis, ticks and ticklabels
  • colorbar


See above

on 19 Oct 2012

Hello Per Isakson,

Thank you for your help with this. Would you mind if I asked you one further question please?

If I was trying to create a graph like this:

So I have 3 sets of data, acceleration, speed and efficiency.

For example Speed =40 and acceleration = 2 has an efficiency of 80%.

Would acceleration and speed be in the 2D array on their own? or would this become a 3D array with acceleration, speed and efficiency?

Thank you


See above

Answer by Image Analyst
on 1 Oct 2012

You would create an image - basically a 2D array. You must have that. If you don't have that, then you have nothing at all to display. Then display it with image() or imshow(), and apply a colormap. Not hard at all, assuming you have the data, temperature vs. current, in a 2D array already.

  1 Comment

on 15 Oct 2012


Thanks for your reply. I'm sorry but I don't understand how to get the colours to appear correctly.

This is what I am getting

but this is what I'm trying to achieve

Also how do you switch the axis?

Thank you for your help

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!