Controu plot from matlab table

I have a table with values extracted from a csv I want to make a contour plot from.
Let's use this table as an example
tdata.x = [1;2;1;2];
tdata.y = [3;3;4;4];
tdata.z = randn(4,1);
tdata=struct2table(tdata);
>> tdata
tdata =
4×3 table
x y z
_ _ _______
1 3 0.53767
2 3 1.8339
1 4 -2.2588
2 4 0.86217
I would like to pivot this such that I can use it for plotting a contour, so in principle I want a 2x2 z matrix where rows/columns are given by y and x respectively, something in this direction:
x 1 2
y
3 0.53767 1.8339
4 -2.2588 0.86217

Answers (1)

x = T.x ;
y = T.y ;
z = T.z ;
nx = length(unique(x)) ;
ny = length(unique(y)) ;
X = reshape(x,nx,ny) ;
Y = reshape(y,nx,ny) ;
Z = reshape(z,nx,ny) ;
contour(X,Y,Z)

9 Comments

I am running into some problems with this solution, for example applying this to the attached dataset and the following code results in an incorrect plot which contains a lot of artifacts.
file = 'EMAG2_amsterdam.csv';
map = readtable(file);
lon = map.lon;
lat = map.lat;
z = map.upCont;
nx = length(unique(lon));
ny = length(unique(lat));
X = reshape(lon, nx, ny);
Y = reshape(lat, nx, ny);
Z = reshape(z, nx, ny);
figure(1)
contourf(X,Y,Z)
xlabel('longitude')
ylabel('latitude')
The artifacts are even more apparent when looking at the surf instead of the contourf.
file = 'EMAG2_amsterdam.csv';
map = readtable(file);
lon = map.lon;
lat = map.lat;
z = map.upCont;
m = 100 ; n = 100 ;
x = linspace(min(lon),max(lon),m) ;
y = linspace(min(lat),max(lat),n) ;
[X,Y] = meshgrid(x,y) ;
Z = griddata(lon,lat,z,X,Y) ;
figure(1)
contourf(X,Y,Z)
xlabel('longitude')
ylabel('latitude')
What about the original version was problematic?
Yes..use the present given code.
Morten Nissov
Morten Nissov on 8 Apr 2021
Edited: Morten Nissov on 8 Apr 2021
I was asking what was wrong with the original implementation?
I am using it for much more than just this single visualization so I was curious if there was something that I will need to keep in mind. For this reason as well I'd like to avoid resampling/interpolating for this visualization.
The confusion for me is that, according to documentation, griddata is for scattered data. But the dataset is already gridded, at least as far as I'm aware.
That should work...but the data arrangment should be considered.
Is there another way to transform a table with 3 columns to a grid? It can be done in pandas with pivot, which is how I know the data itself if fine.
The gird looks fine when you use
scatter(X(:),Y(:),[],Z(:),'.')
Show me the PAndas code which worked for you.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('EMAG2_faaborg.csv')
df = df[['lat','lon','upCont']].pivot('lat','lon')
lon = df.columns.levels[1].values
lat = df.index.values
vals = df.values
X,Y = np.meshgrid(lon, lat)
then I can plot using
plt.contourf(X, Y, vals)

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 18 Mar 2021

Commented:

on 9 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!