简体   繁体   English

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

[英]Plotting straight line on semilog plot with matplotlib

在此处输入图像描述

This isn't, strictly speaking, a semilogy plot.严格来说,这不是半逻辑图。 I used this code to get a logarithmic y-axis :我使用此代码获得对数y-axis

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.我需要从 x 轴以 45 度角绘制直线。 Similar to the line in the image, but actually straight and at a 45 degree angle.类似于图像中的线,但实际上是直线且呈 45 度角。 I also need to shift any x-value based solely on its y-value.我还需要仅根据其 y 值移动任何 x 值。

What is the formula for plotting a straight line for known y-values and unknown x-values at a 45 degree angle?以 45 度角绘制已知 y 值和未知 x 值的直线的公式是什么? (Perhaps a math forum would be more appropriate?) (也许数学论坛会更合适?)

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.所以我无法想出一种方法来自己绘制直线。

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.这里有一些证据。

在此处输入图像描述

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.这里有更多的证据。

在此处输入图像描述

Now, in the context of a skew-t, there's more work to do.现在,在 skew-t 的背景下,还有更多工作要做。 When generating a skew-t, you'll need to work both from a known y-value and a known x-value.生成 skew-t 时,您需要同时使用已知的 y 值和已知的 x 值。 Here's an example function which takes a temperature (x value) and a level (y value) and returns the appropriately skewed x value.这是一个示例函数,它采用温度(x 值)和水平(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.这个函数看起来相当复杂,但那是因为当您通过在 y 值上调用 np.log() 在半对数上创建一条直线时,x 值将远离它需要的位置。 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.无论您在哪个级别绘制数据,您都知道应该在最低级别绘制数据的什么位置,因此您必须先找出最低级别的差异,然后再将其应用到较高级别。

Once you know the "offset" you just compensate for that difference on all skewed x values.一旦知道“偏移量”,您只需补偿所有倾斜的 x 值的差异。 The reason these values are multiplied by -30 is application specific.这些值乘以 -30 的原因是特定于应用程序的。 This number will need to change based on the y limit and x limit of the plot.该数字将需要根据绘图的 y 限制和 x 限制进行更改。

The "1000" in the np.log(1000) may need to change as well depending on the plot. np.log(1000)中的“1000”可能也需要根据情节进行更改。 This should be the lowest level on the skew-t plot (highest y value).这应该是偏斜 t 图上的最低水平(最高 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.要查看该函数的运行情况,请看下面是在 500 级绘制温度为 10 的数据点的样子。

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.可能有更优雅的解决方案,但这是我目前拥有的最好的解决方案。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM