使用 matplotlib 在半对数图上绘制直线 [英] Plotting straight line on semilog plot with matplotlib

查看:37
本文介绍了使用 matplotlib 在半对数图上绘制直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

严格来说,这不是符号图.我使用此代码获得对数 y 轴:

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

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

edit:好的,也许我太笨了.我需要从x轴以45度角绘制直线.类似于图像中的线,但实际上是直线且呈 45 度角.我还需要仅根据其y值移动任何x值.

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.

以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?)

我的教育水平很低,例如,前几天我不得不自学什么是对数,因为我从未在学校学习过.所以我无法想出一种方法来自己绘制直线.

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.

为了在半对数上绘制直线,有两种主要方法.如果您有一个 x 值列表并想要获得将绘制一条直线的相应 y 值,那么您只需在每个 x 值上调用 numpy.exp().

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.

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

这里有一些证据.

如果你想用已知的 y 值和未知的 x 值绘制一条直线,只需做相反的事情.

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

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

这里有更多证据.

现在,在 skew-t 的上下文中,还有更多工作要做.生成时滞t时,您需要同时使用已知的y值和已知的x值.这是一个示例函数,该函数采用一个温度(x值)和一个水平(y值)并返回适当的偏斜x值.

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.

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.

这个函数看起来很复杂,但那是因为当你通过在 y 值上调用 np.log() 在半对数上创建一条直线时,x 值将远离它需要的位置.因此,您需要找到值的实际位置和应有的位置之间的差异.无论在什么级别绘制数据,都知道应该在最低级别绘制数据,因此必须先在最低级别找到差异,然后再将其应用于更高级别.

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.

一旦你知道偏移量"您只需补偿所有偏斜 x 值的差异.这些值乘以 -30 的原因是特定于应用程序的.该数字将需要根据图的y极限和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.

1000"根据图的不同, np.log(1000)中的代码可能也需要更改.这应该是偏斜图上的最低水平(y值最高).

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

要点是,如果您知道应该在什么温度下绘制数据,以及要在什么水平上绘制数据,则此功能将正确偏移该值(当然,当针对您的特定图调整-30时).

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

要查看运行中的函数,下面是在 500 级绘制温度为 10 的数据点的样子.

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.

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 在半对数图上绘制直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆