0

I have a set of data, which should be plotted in a loglog scale. x-values are always positive, but y-values are positive and negative. So, loglog command will just omit negative y-values. But I want to plot them: I want to have y-axis in logarithmic scale, but with negative values, as well as positive values.

Basically, if we have set of data (x,y), I want to plot: (log(x),log(y)), if y>0, and (log(x),-log(-y)), if y<0. I tried to to use these formula, and then simple plot function, but with this approach axes are not in logarithmic scale. Once again, I want both axes to be in logarithmic scale.

Thanks

Edit: to clarify, I want the result to be exactly as plot(x,y), but both y-axis and x-axis to be in logarithmic scale

Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47

2 Answers2

4

There's a few functions for plotting logarithmic plots: along y, along x or both: semilogy, semilogx, loglog. You'll need the loglog one:

ypos = y(y>0); % Gets positive values
xpos = x(y>0); % Get corresponding x values
yneg = y(y<0); % Gets negative values
xneg = x(y<0); % Get corresponding x values
figure;
loglog(xpos,ypos)
hold on
loglog(xneg,-yneg, 'r')

This basically creates two plots in the same figure, one with the positive and one with the negative y values. In short:

figure;
loglog(x(y>0),y(y>0))
hold on
loglog(x(y<0),-y(y<0),'r')

Since the logarithm is by definition strict positive, you cannot create a negative scale. What you can do is give the illusion you have one by setting

set(gca, 'xdir','reverse')

This does mean you'd need two separate plots though, to prevent the x-axis from the positive numbers running backwards as well.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Thanks for the respond! But this is not what I want. I tried your code on the set: `x=[0.00001,0.0001,0.001,0.1,1]`, `y=[0.00001,-0.0001,0.001,-0.1,1]`. The y-axis has only positive values, so you code basically treats negative values of `y` as positive. I want y-axis to have both negative and positive values. – Mikhail Genkin Sep 22 '15 at 17:51
  • That's impossible. The logarithm is per definition strict positive. What I suggest it plotting the negative y-values in a different colour and mention that in your figure caption. – Adriaan Sep 22 '15 at 17:55
  • Yes, thanks, I thought about it as well. I asked it, because may be someone knows the better solution – Mikhail Genkin Sep 22 '15 at 17:57
  • added a final attempt to do what you want. – Adriaan Sep 22 '15 at 18:12
  • Thanks for your effort, I accpeted the answer. Do you know the tools, which can be used to develop my own plotter? – Mikhail Genkin Sep 22 '15 at 18:21
  • Thanks for the accept, glad I could do my part. I do not know much about plotting, but try the Imaging Toolbox – Adriaan Sep 22 '15 at 18:23
  • 1
    @MikhailGenkin Ask another question, "your own plotter" is not clear. – IKavanagh Sep 22 '15 at 18:43
2

As Adriaan pointed out you can use loglog() to plot data on a log-log scale. However, going back to the question

I want to plot: (log(x),log(y)), if y>0, and (log(x),-log(-y)), if y<0. I tried to to use these formula, and then simple plot function, but with this approach axes are not in logarithmic scale.

You could take this approach and then set the axes to use a log-log scale with set(gca, 'XScale', 'log') and set(gca, 'YScale', 'log') for the x and y axes respectively.

Community
  • 1
  • 1
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
  • That is almost what I want! But after I'm doing `set(gca, 'YScale', 'log')`, it starts to omit negative values. – Mikhail Genkin Sep 22 '15 at 17:56
  • 2
    Yes, because, as I pointed out, the logarithm is not defined for negative values and you cannot plot that which is not defined. – Adriaan Sep 22 '15 at 17:57