MATLAB Answers

## Change the format of x axis in a Kernel density plot.

Asked by alberto caracciolo

### alberto caracciolo (view profile)

on 16 Jul 2019
Latest activity Answered by Steven Lord

### Steven Lord (view profile)

on 17 Jul 2019
Accepted Answer by the cyclist

### the cyclist (view profile)

Hi all,
I made this plot using a Kernel density function
x = [B];
[f,xi] = ksdensity(x,'bandwidth', bw);
plot(xi,f, 'g');
What i would like to do is to change the x-axis labels into 10 base power. For example, instead of having 0 i would like to have 10^2 and so on. The final result shold be something similar to this figure:
Thank you

#### 0 Comments

Sign in to comment.

## 2 Answers

### the cyclist (view profile)

Answer by the cyclist

### the cyclist (view profile)

on 16 Jul 2019
Accepted Answer

Instead of the plot command, use semilogx.

#### 0 Comments

Sign in to comment.

### Steven Lord (view profile)

Answer by Steven Lord

### Steven Lord (view profile)

on 17 Jul 2019

I suspect when you called semilogx as you said in your answer that probably should have been a comment you plotted on a linear axes first (with plot), turned hold on, then tried plotting a second time with semilogx. If you do that, hold will prevent semilogx from making the axes have a log scaled X axis.
To avoid this, either don't try to plot two graphs on the same axes with hold on but instead plot just the log scale data or change the X axes scale to 'log' directly, without calling semilogx. The hold function will prevent MATLAB from automatically switching the scale, but it won't prevent MATLAB from switching the scale if you explicitly order it to do so.
This example will not switch the X axes scale.
figure
plot(1:10, 1:10)
hold on
semilogx(1:10, 10:-1:1)
This will.
figure
h = plot(1:10, 1:10);
hold on
semilogx(1:10, 10:-1:1);
% Get the axes containing the line
ax = ancestor(h, 'axes');
% Explicitly order MATLAB to change the scale
ax.XScale = 'log';
This will too.
figure
semilogx(1:10, 10:-1:1);

#### 0 Comments

Sign in to comment.