# How to plot with a reciprocal (1/x) scale

40 views (last 30 days)
Maruan Alberto Bracci on 3 May 2021
Hi everybody,
I have some sets of data: [x1] and [y1], [x2] and [y2], ...The x axes have been obtained by calculating (with a complex operation) the reciprocal of the sets [A], [B], ...When I plot x1 vs y1, x2 vs y2, ... the data points are no longer equally spaced in the plot (very crowded at one side of the plot). Something like that:
A = linspace(1,50,100);
B = linspace(10,70,80);
x1 = 1./A; %semplified version of the real calculation
x2 = 1./B;
y1 = rand(100,1);
y2 = rand(80,1);
plot(x1,y1,'-o',x2,y2,'-o') What I need is
1. that the data points are equally spaced
2. that the labels of the horizontal axis correspond to x
3. when a point from the graph is selected, it shows the value of x and not 1/x.
What I did to solve the point 1 is plotting 1./x1 and 1./x2.
plot(1./x1,y1,'-o',1./x2,y2,'-o')
To solve point 2, I have used 'Xtick' and 'XTickLabel' to manually set the labels. I found no solution to point 3. I would like to use something like semilogx, but for reciprocal scales. Does it exist?
Thx for your time

Sambit Supriya Dash on 7 May 2021
Supposing this example from the MATLAB previous codes,
A = [0.5,0.65,0.7,0.66,0.81]; % The plotting function values
B = [5,10,20,100,1000]; % The x-axis levels
plot(A);
xticks(1:length(A));
xticklabels(string(B));
##### 1 CommentShowHide None
Maruan Alberto Bracci on 11 May 2021
thanks for the answer. I had a look at the link you provided, but I was not able to fix my code.
I found a problem in the solution you proposed: if I have 2 sets of data, and the x axes are not coincident, plotting them using plot(set1), hold on, plot(set2), will distort the spectra. Let's say:
x1 = 1./linspace(1,50,100);
x2 = 1./linspace(10,70,80);
y1 = rand(100,1);
y2 = rand(80,1);
plot(y1) %y1 is plotted using as x-axis [1:100]
hold on
plot(y2) %y2 is plotted using as x-axis [1:80]
hold off
The problem is that y1 sould start at some mayor point in the x-axis (i.e. x2(1) < x1(1) ). Also the spacing in the sets x1 and x2 (i.e. dx1 and dx2), is not necessarily coincident. The problem can be solved creating another axis, X, that is the sum of the privious ones. Then find the index positions of x1 in X and x2 in X, and use the indexes to plot y1 and y2.
X = [x1,x2];
X = unique(X); %sorting and removind duplicates
for i = 1:length(x1)
[~,I1(i)] = min(abs(X-x1(i)))
end
for i = 1:length(x2)
[~,I2(i)] = min(abs(X-x2(i)))
end
plot(I1,y1)
hold on
plot(I2,y2)
hold off
n = 20; %reduce number of ticks to be displayed
Xax_labels = round(linspace(X(1),X(end),length(X)/n),3);
Xax_ticks = [linspace(1,length(X),length(X)/n)];
xticks(Xax_ticks);
xticklabels(Xax_labels);
Anothe problem is that when a point is selected, it shows the index (20) and not the X value (ca. 0.1). Moreover, I had some problems in processing bigger sets of data in this way, so I was looking for a cleaner solution. Do you have some idea?

R2020b

### Community Treasure Hunt

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

Start Hunting!