以对数刻度绘制(Matlab)

编程入门 行业动态 更新时间:2024-10-15 10:21:41
本文介绍了以对数刻度绘制(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一组数据,应该以对数标度标绘. x值始终为正,而y值为正和负.因此,loglog命令将只忽略负y值.但我想绘制它们:我想以对数标度表示y轴,但要同时具有负值和正值.

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.

基本上,如果我们有一组数据(x,y), 我想绘制:(log(x),log(y)),如果y>0,和(log(x),-log(-y)),如果y<0.我尝试使用这些公式,然后使用简单的plot函数,但是使用这种方法时,轴不在对数范围内.再一次,我希望两个轴都为对数刻度.

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.

谢谢

为明确起见,我希望结果与plot(x,y)完全相同,但y轴和x轴均以对数标度

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

推荐答案

有一些用于绘制对数图的函数:沿y,沿x或两者皆有: semilogy , semilogx , loglog .您需要loglog一个:

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')

这基本上在同一图中创建了两个图,一个具有正y值,另一个具有负y值.简而言之:

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')

这确实意味着您需要两个单独的图,以防止x轴也从正数向后移动.

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

更多推荐

以对数刻度绘制(Matlab)

本文发布于:2023-11-30 02:22:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数   刻度   Matlab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!