用matplotlib在半对数图上绘制直线。

时间:2022-11-01 20:22:30

用matplotlib在半对数图上绘制直线。

This isn't, strictly speaking, a semilogy plot. I used this code to get a logarithmic y-axis:

严格地说,这不是一个符号学的情节。我用这个代码得到一个对数y轴:

pyplot.gca().set_yscale('log')

edit: Okay maybe I'm dumbing it down too much. I need to plot straight lines, from the x-axis at a 45 degree angle. Similar to the line in the image, but actually straight and at a 45 degree angle. I also need to shift any x-value based solely on its y-value.

编辑:好吧,也许我太笨了。我需要画直线,从x轴到45度角。类似于图像中的直线,但实际上是直的,并且是45度角。我还需要改变任何基于y值的x值。

What is the formula for plotting a straight line for known y-values and unknown x-values at a 45 degree angle? (Perhaps a math forum would be more appropriate?)

对于已知的y值和未知的x值,以45度角绘制直线的公式是什么?(或许一个数学论坛更合适?)

My education is at a pretty low level, so for instance I had to teach myself what logarithms are the other day because I had never learned in school. So I'm not able to work out a way to plot straight lines on my own.

我的教育水平很低,所以我不得不自学对数,因为我从来没有在学校里学过。所以我不能自己画直线。

1 个解决方案

#1


2  

Okay, just gonna answer the question with what I've figured out so far.

好了,我想用我已经算出的答案来回答这个问题。

In order to plot a straight line on a semilog, there are two main methods. If you have a list of x values and want to get the corresponding y values which will plot a straight line, then you just call numpy.exp() on each x value.

为了在半对数上画一条直线,有两种主要的方法。如果你有一个x值的列表,并且想要得到对应的y值,它会画一条直线,那么你只需在每一个x值上调用numpy.exp()。

import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_yscale('log')

x = np.arange(0, 51, 10)
y = np.exp(x)

plt.plot(x, y, 'k-')
plt.show()

Here's some proof.

这里有一些证据。

用matplotlib在半对数图上绘制直线。

If you want to plot a straight line with known y values and unknown x values, just do the opposite.

如果你想用已知的y值和未知的x值画一条直线,就做相反的事情。

import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_yscale('log')

y = np.arange(0, 1001, 100)
x = np.log(y)

plt.plot(x, y, 'k-')
plt.show()

Here's more proof.

这是更多的证据。

用matplotlib在半对数图上绘制直线。

Now, in the context of a skew-t, there's more work to do. When generating a skew-t, you'll need to work both from a known y-value and a known x-value. Here's an example function which takes a temperature (x value) and a level (y value) and returns the appropriately skewed x value.

现在,在一个斜体的背景下,有更多的工作要做。当生成一个skew-t时,您需要从已知的y值和已知的x值中工作。这里有一个示例函数,它使用一个温度(x值)和一个level (y值),并返回适当倾斜的x值。

def get_skewed_x(level, temp):
    base_log = (-30 * np.log(1000))
    top_log = (-30 * np.log(level))
    diff = base_log - temp
    x = top_log - diff
    return x

The function accepts a level the data should be plotted on, and a temperature value.

该函数接受一个级别的数据应该被绘制和一个温度值。

This function seems pretty complex, but that's because when you create a straight line on a semilog by calling np.log() on a y-value, the x-value will be way off from where it needs to be. So you need to find the difference between where the value actually is, and where it should be. No matter what level you're plotting the data at, you know where it should be plotted at the lowest level so you have to find the difference at the lowest level before applying it at the upper level.

这个函数看起来相当复杂,但这是因为当您在一个y值上调用np.log()时,在半日志上创建一条直线,x值将会偏离它需要的位置。所以你需要找出实际值和应该在哪里的差值。无论你在什么层次上绘制数据,你都知道它应该在什么位置绘制在最低的水平上,所以你必须在最低层上找出差异,然后再将其应用到上层。

Once you know the "offset" you just compensate for that difference on all skewed x values. The reason these values are multiplied by -30 is application specific. This number will need to change based on the y limit and x limit of the plot.

一旦你知道了“偏移量”,你只需要补偿所有倾斜的x值上的差值。这些值乘以-30的原因是特定于应用程序的。这个数字需要根据图的y极限和x极限来改变。

The "1000" in the np.log(1000) may need to change as well depending on the plot. This should be the lowest level on the skew-t plot (highest y value).

np.log(1000)中的“1000”可能需要根据不同的情节进行更改。这应该是斜线图(最高的y值)的最低水平。

The point is, if you know what temperature the data should be plotted on, and what level you want to plot the data, this function will properly skew the value (when the -30 is adjusted for your specific plot of course).

关键是,如果你知道数据应该被绘制在什么温度上,以及你想要绘制数据的水平,这个函数会正确地扭曲这个值(当-30被调整到你的特定的位置)。

To see the function in action, here's what it looks like to plot a data point with a temperature of 10 at level 500.

要想看到函数的作用,这里有一个数据点,它的温度是10,在500级。

get_skewed_x(500, 10)

The blue dot shows where the datapoint would be plotted.

蓝点表示数据点的绘制位置。

There's probably a more elegant solution, but this is the best I have right now.

也许有一个更优雅的解决方案,但这是我现在所拥有的最好的解决方案。

用matplotlib在半对数图上绘制直线。

#1


2  

Okay, just gonna answer the question with what I've figured out so far.

好了,我想用我已经算出的答案来回答这个问题。

In order to plot a straight line on a semilog, there are two main methods. If you have a list of x values and want to get the corresponding y values which will plot a straight line, then you just call numpy.exp() on each x value.

为了在半对数上画一条直线,有两种主要的方法。如果你有一个x值的列表,并且想要得到对应的y值,它会画一条直线,那么你只需在每一个x值上调用numpy.exp()。

import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_yscale('log')

x = np.arange(0, 51, 10)
y = np.exp(x)

plt.plot(x, y, 'k-')
plt.show()

Here's some proof.

这里有一些证据。

用matplotlib在半对数图上绘制直线。

If you want to plot a straight line with known y values and unknown x values, just do the opposite.

如果你想用已知的y值和未知的x值画一条直线,就做相反的事情。

import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_yscale('log')

y = np.arange(0, 1001, 100)
x = np.log(y)

plt.plot(x, y, 'k-')
plt.show()

Here's more proof.

这是更多的证据。

用matplotlib在半对数图上绘制直线。

Now, in the context of a skew-t, there's more work to do. When generating a skew-t, you'll need to work both from a known y-value and a known x-value. Here's an example function which takes a temperature (x value) and a level (y value) and returns the appropriately skewed x value.

现在,在一个斜体的背景下,有更多的工作要做。当生成一个skew-t时,您需要从已知的y值和已知的x值中工作。这里有一个示例函数,它使用一个温度(x值)和一个level (y值),并返回适当倾斜的x值。

def get_skewed_x(level, temp):
    base_log = (-30 * np.log(1000))
    top_log = (-30 * np.log(level))
    diff = base_log - temp
    x = top_log - diff
    return x

The function accepts a level the data should be plotted on, and a temperature value.

该函数接受一个级别的数据应该被绘制和一个温度值。

This function seems pretty complex, but that's because when you create a straight line on a semilog by calling np.log() on a y-value, the x-value will be way off from where it needs to be. So you need to find the difference between where the value actually is, and where it should be. No matter what level you're plotting the data at, you know where it should be plotted at the lowest level so you have to find the difference at the lowest level before applying it at the upper level.

这个函数看起来相当复杂,但这是因为当您在一个y值上调用np.log()时,在半日志上创建一条直线,x值将会偏离它需要的位置。所以你需要找出实际值和应该在哪里的差值。无论你在什么层次上绘制数据,你都知道它应该在什么位置绘制在最低的水平上,所以你必须在最低层上找出差异,然后再将其应用到上层。

Once you know the "offset" you just compensate for that difference on all skewed x values. The reason these values are multiplied by -30 is application specific. This number will need to change based on the y limit and x limit of the plot.

一旦你知道了“偏移量”,你只需要补偿所有倾斜的x值上的差值。这些值乘以-30的原因是特定于应用程序的。这个数字需要根据图的y极限和x极限来改变。

The "1000" in the np.log(1000) may need to change as well depending on the plot. This should be the lowest level on the skew-t plot (highest y value).

np.log(1000)中的“1000”可能需要根据不同的情节进行更改。这应该是斜线图(最高的y值)的最低水平。

The point is, if you know what temperature the data should be plotted on, and what level you want to plot the data, this function will properly skew the value (when the -30 is adjusted for your specific plot of course).

关键是,如果你知道数据应该被绘制在什么温度上,以及你想要绘制数据的水平,这个函数会正确地扭曲这个值(当-30被调整到你的特定的位置)。

To see the function in action, here's what it looks like to plot a data point with a temperature of 10 at level 500.

要想看到函数的作用,这里有一个数据点,它的温度是10,在500级。

get_skewed_x(500, 10)

The blue dot shows where the datapoint would be plotted.

蓝点表示数据点的绘制位置。

There's probably a more elegant solution, but this is the best I have right now.

也许有一个更优雅的解决方案,但这是我现在所拥有的最好的解决方案。

用matplotlib在半对数图上绘制直线。