user1844160
user1844160

Reputation: 303

Plotting straight line on semilog plot with matplotlib

enter image description here

This isn't, strictly speaking, a semilogy plot. I used this code to get a logarithmic 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. 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.

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.

Upvotes: 1

Views: 13271

Answers (1)

user1844160
user1844160

Reputation: 303

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.

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.

enter image description here

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

Here's more proof.

enter image description here

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.

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.

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.

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

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

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.

enter image description here

Upvotes: 2

Related Questions