Skip to content
Advertisement

Python- compress lower end of y-axis in contourf plot

The issue

I have a contourf plot I made with a pandas dataframe that plots some 2-dimensional value with time on the x-axis and vertical pressure level on the y-axis. The field, time, and pressure data I’m pulling is all from a netCDF file. I can plot it fine, but I’d like to scale the y-axis to better represent the real atmosphere. (The default scaling is linear, but the pressure levels in the file imply a different king of scaling.) Basically, it should look something like the plot below on the y-axis. It’s like a log scale, but compressing the bottom part of the axis instead of the top. (I don’t know the term for this… like a log scale but inverted?) It doesn’t need to be exact.

Example of a properly-scaled time vs. pressure plot

Working example (written in Jupyter notebook)

JavaScript
JavaScript
JavaScript

The Question

How can I scale the y-axis such that values near the bottom (900, 800) are compressed while values near the top (200) are expanded and given more plot space, like in the sample above my code? I tried using ax.set_yscale('function', functions=(forward, inverse)) but didn’t understand how it works. I also tried simply ax.set_yscale('log'), but log isn’t what I need.

Advertisement

Answer

You can use a custom scale transformation with ax.set_yscale('function', functions=(forward, inverse)) as you suggested. From the documentation:

forward and inverse are callables that return the scale transform and its inverse.

In this case, define in forward() the function you want, such as the inverse of the log function, or a more custom one for your need. Call this function before your y-axis customization.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement