# Plotting a single time series with double y axes, differently labelled

1 visualizzazione (ultimi 30 giorni)
z8080 il 30 Giu 2017
Commentato: Star Strider il 30 Giu 2017
I would like to plot a time series and have the two y axes (left and right) labelled differently (say, in two different languages). I used the yyaxis function but it doesn't produce the expected result: the right axis is created as 0 to 1 as opposed to the range that the left one has, and to fix this one would have to linearly transform that space to match the left one. I've given a simplified example here, but with real scripts, this become very cumbersome.
Is there an easier way to apply labels on the right hand side axis without having to alter its values?
Thanks!
min=0;
max=9;
x=min:max;
y=floor(10 .* rand([max+1 1])); % integers 0 to 9
scatter(x,y)
ylim([min max])
yyaxis left % english labels; min will be 0, max 9
set(gca, 'YTick', min:max)
set(gca, 'YTickLabel', {'zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' })
yyaxis right % italian labels
set(gca, 'YTick', min:max)
set(gca, 'YTickLabel', {'zero' 'uno' 'due' 'tre' 'quattro' 'cinque' 'sei' 'sette' 'otto' 'nove' })
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Star Strider il 30 Giu 2017
You need to plot after each yyaxis call:
yyaxis left % english labels; min will be 0, max 9
scatter(x,y,'b')
set(gca, 'YTick', min:max)
set(gca, 'YTickLabel', {'zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' })
yyaxis right % italian labels
scatter(x,y,'b')
set(gca, 'YTick', min:max)
set(gca, 'YTickLabel', {'zero' 'uno' 'due' 'tre' 'quattro' 'cinque' 'sei' 'sette' 'otto' 'nove' })
This plots the same values (and colors) twice, so the plot itself does not change.
##### 2 CommentiMostra NessunoNascondi Nessuno
z8080 il 30 Giu 2017
Thank you!
Star Strider il 30 Giu 2017
My pleasure!

Accedi per commentare.

### Più risposte (2)

Don't use min and max as variable names, they are functions.
yyaxis is meant for use with two distinct plots, each of which has its own axis. Given that you are plotting nothing for the right axis I would assume you can just set its limits to whatever you want e.g. [0 9] and it will then work fine.
You only set the ticks, but you need to set the limits to be 0 to 9 otherwise the rest of your ticks are just missing off the top.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
z8080 il 30 Giu 2017
Thank you!

Accedi per commentare.

Jan il 30 Giu 2017
Modificato: Jan il 30 Giu 2017
dmin = 0; % Do not shadow the builtin function "min"
dmax = 9;
x = dmin:dmax;
y = floor(10 .* rand([max+1 1])); % integers 0 to 9
AxesH = axes;
scatter(x,y)
ylim([dmin dmax])
yyaxis left % english labels; min will be 0, max 9
set(AxesH, 'YTick', dmin:dmax, ...
'YTickLabel', {'zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' })
yyaxis right % italian labels
AxesH.YAxis(2).Limits = [dmin, dmax];
AxesH.YAxis(2).TickValues = 0:9;
AxesH.YAxis(2).TickLabels = {'zero' 'uno' 'due' 'tre' 'quattro' 'cinque' 'sei' 'sette' 'otto' 'nove'};
Or easier:
yyaxis right % italian labels
set(gca, 'YTick', min:max, ...
'YLim', [min, max], ... % <- Added
'YTickLabel', {'zero' 'uno' 'due' 'tre' 'quattro' 'cinque' ...
'sei' 'sette' 'otto' 'nove' })
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
z8080 il 30 Giu 2017
Thank you!

Accedi per commentare.

### Categorie

Scopri di più su Log Plots in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by