Issues with contourf and tricontourf when creating plots from Argo Float data (pandas)

Hello all,

I am having an issue creating plots using contourf and tricontorf from an Argo Float dataset. I work with python and read in the data to Spyder using pandas, putting all of my variables into a pandas data frame. All of my variables are 1D, and the same size.

Because of all my variables being 1-D, I had issues trying to create biogeochemical contour plots from my data. I am aiming to make plots where my x-axis is the date time, y-axis is depth, and Z is a biogeochemical variable, like Oxygen for example. Contourf requires that the Z input be a 2-D array, but all of my variables in my pandas data frame are 1-D and the same size as each other.

This led me to attempt creating a function that instead uses tricontourf to create these plots, where I can choose my variable for the x-axis, the y-axis is depth, and I can choose a variable as my Z input. When I use the Argo Float’s station numbers as my variable for x-axis, I get really clean plots (Left Figure). However, something goes wrong when I use date as my x-axis and I’m not sure how to fix it (Right Figure). The dtype that is used as an input for the date is datetime64[ns].

I’ve seen a number of posts around Stack Overflow and other sources that both attempt to fix tricontourf, as well as some solutions that recommend converting your Z-input 1D array into a 2D array so it would run with contourf, but I have had no success with their suggestions to get either contourf or tricontourf to create the contour plots I am hoping for.

If anyone has worked with either contourf or tricontourf closely, recognizes my issue, has any suggestions on how to tackle this issue, or has any questions to help figure out what my issue may be, I would greatly appreciate your input! Thank you so much for your time!

1 Like

Hi @nicolagCO2 and welcome to the forum!

I would consider backing up a little bit and asking “what is the best data structure for representing ARGO data”? If all of your variables are 1D and the same size, I would recommend using Xarray, not Pandas, to hold the data. That is in line with what the argopy package seems to do.

Once have your data in a tidy xarray data structure, it should be very straightforward to create the plot you want.

I hope this is helpful.

1 Like

Hi @nicolagCO2! I was also going to mention the argopy package like Ryan did (there is also a package in R by the way!).

This might not be very useful but I find your right hand plot more realistic-looking than the left hand plot — assuming there should be gaps in the data as the argo floats go up and down in the water column. The left side plot seems like it has horizontal interpolation artifacts. But yes if you can avoid your issue by using argopy, that seems like the way to go!

Otherwise I can try to dig up some old code I wrote to plot individual data points in a plot like this as little patches that take up an appropriate – but not overlapping – amount of space which can look nicer than doing a scatter plot where the markers may overlap (or a contour plot like this).

1 Like

Hi @rabernat and @kthyng, thank you so much for your guidance! I am going to take a look at both argopy and restructuring my data using xarray. I use xarray in other areas of my code unrelated to these plots, so I may just switch everything to xarray to keep the same tools throughout my code. I will keep posted if I can solve this issue with your help! Thanks again!