[英]how to plot a line with a slope in matplotlib using plt.loglog
Here I need to plot a frequency and a simple line with a slope of -5/3.这里我需要 plot 一个频率和一条斜率为 -5/3 的简单线。
The problem is that the main plot is using plt.loglog()
and when I want to show the line it gives me nothing or something strange.问题是主要的 plot 正在使用plt.loglog()
,当我想显示该行时,它没有给我任何东西或奇怪的东西。
Here are the pictures of it.这是它的照片。 I have plotted the right one and desired is the left one.我已经绘制了右边的,想要的是左边的。
I already used np.linspace
and some other things, but I was not able to solve the problem.我已经使用np.linspace
和其他一些东西,但我无法解决问题。 Also, it is not clear at which points I have the first and the end of the frequency plot.此外,尚不清楚我在哪个点有频率 plot 的第一个和最后一个。 That's another reason why I can not use 'np.linspace'.这是我不能使用“np.linspace”的另一个原因。 Can anyone help me?谁能帮我?
Thanks a lot for your attention.非常感谢您的关注。 I tried your code but I found out maybe there are better ways to do it with kind of my dataset.我尝试了你的代码,但我发现也许有更好的方法来使用我的数据集。 So I did this:所以我这样做了:
np.array()
and had np.log()
function on it:将数据集的 class 更改为np.array()
并在其上有np.log()
function:x = ... # type(x) = list
y = ... # type(y) = list
.
.
.
x = np.log(np.array(x))
y = np.log(np.array(y))
In this case I did not have to use plt.loglog()
or np.log()
and np.exp()
in calculations anymore.在这种情况下,我不必再在计算中使用plt.loglog()
或np.log()
和np.exp()
了。
ymin, ymax = ([y.min(), y.max()])
ymid = (ymin + ymax) / 2
xmin, xmax = ([x.min(), x.max()])
xmid = (xmin + xmax) / 2
np.linspace()
for rest:对 rest 使用np.linspace()
:slope = - 5 / 3
x1 = np.linspace(xmin, xmax)
y1 = slope * (x1 - xmid) + ymid
ax.plot(x1, y1, 'r')
And got the result I wanted.并得到了我想要的结果。
the result结果
plt.loglog()
kind of plots in frequency spectrums, I edited these things:
因此,最好在频谱中使用plt.loglog()
类型的图,我编辑了这些东西:
np.array()
将 x 和 y 改回正常np.array()
x = array(...) y = array(...)
np.exp()
:找到 x 和 y 的中点作为直线的中心,并使用简单的直线方程,然后使用np.exp()
绘制直线: ymin, ymax = log([y.min(), y.max()]) ymid = (ymin + ymax) / 2 xmin, xmax = log([x.min(), x.max()]) xmid = (xmin + xmax) / 2 slope = - 5 / 3 y1 = slope * (xmin - xmid) + ymid y2 = slope * (xmax - xmid) + ymid ax.plot(exp([xmin, xmax]), exp([y1, y2]), 'r') plt.loglog()
the result结果
As you see now we have the plot in log scale.正如您现在看到的,我们有对数刻度的 plot。
Here is a possible approach.这是一种可能的方法。 All calculations happen in logspace.所有计算都发生在日志空间中。 The points are transformed back to linear space to be plotted.这些点被转换回要绘制的线性空间。 As matplotlib plots a line given two points always as straight, independent to the tranformation of the axes, only two points are needed.由于 matplotlib 在给定两个点的情况下绘制一条直线,因此与轴的变换无关,因此只需要两个点。
Steps:脚步:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(8, 8))
# first create some toy data roughly resembling the example plot
x = np.linspace(10, 2000, 1000)
y = np.random.normal(2000 / x ** np.linspace(.7, .55, x.size), 100 / x ** .7)
ax.plot(x, y)
y0, y1 = np.log([y.min(), y.max()])
# mid_x, mid_y = np.log([x[x.size // 2], y[y.size // 2]])
mid_x, mid_y = (np.log(x.min()) + np.log(x.max())) / 2, (y0 + y1) / 2
slope = -5 / 3
x0 = mid_x + slope * (y0 - mid_y)
x1 = mid_x + slope * (y1 - mid_y)
ax.plot(np.exp([x0, x1]), np.exp([y0, y1]), color='crimson')
plt.loglog()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.