Home > Software design >  Plotting a contour map with row names/indices of dataframe as y-values
Plotting a contour map with row names/indices of dataframe as y-values

Time:01-26

The following dataframe:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]

Where the index are pressure levels indicating the altitude of the values.

When making a simple contour plot:

plt.contourf(df)

plot

The y-values don't correspond with the indices values. I would like to have the pressure levels which correspond to the indices values to be used as the values for the y-axis.

I tried using the extent parameter, but this stretches out the values between the min/max. Pressure levels aren't spread out evenly.

CodePudding user response:

As reported in the documentation of enter image description here

CodePudding user response:

I found it out myself as well yesterday:

retrieved the y-values from the index using list(df.index).

Then create an evenly spaced X value if you have any min/max values using np.linspace and take 2 off the total size of the pressure levels:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]


x_vals = np.linspace(min, max, size(df.index) -2))

plt.contourf(lat_vals, press_lvl, clim)

  •  Tags:  
  • Related