'Plotting a contour map with row names/indices of dataframe as y-values
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)
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.
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | B.Quaink |

