Beginner question: contour-style plot in gadfly of density data?

306 views
Skip to first unread message

ameliafit...@gmail.com

unread,
May 13, 2016, 10:34:19 AM5/13/16
to julia-users
Dear Julia users, 

I have a rookie question about plotting in Gadfly. I have some density data in a plain-text file in the form  of x y d, where d is the density at the point (x,y). I have about 10,000 lines of this data. I'm currently plotting old-school using gnuplot and since I don't like the looks of what I've been able to make, I'm hoping to be able to do something more elegant like Gadfly. I'm a relatively new Julia user as well. I like the "volcano" contour plot from the Gadfly documentation ( second plot from the top at http://dcjones.github.io/Gadfly.jl/geom_contour.html). I'm just not sure how to go about it. 

Has anyone done something like this before? I think it could be a really beautiful way to represent my data if I can get it to work. Any hints or suggestions would be greatly appreciated!

Cheers, 

Amelia

Tom Breloff

unread,
May 13, 2016, 11:13:14 AM5/13/16
to julia-users
Something like this might work for you... it uses a trisurface from matplotlib.

Inline image 1

or if your data can be nicely gridded, you can do a heatmap or a contour:

Inline image 2

or maybe you'd prefer to let someone else do the gridding for you... a histogram2d might be the ticket:

Inline image 3
Lots of options... it's up to you to choose!

Scott T

unread,
May 13, 2016, 8:30:36 PM5/13/16
to julia-users
Two key questions - is your data gridded? And do you plan to sample from these density values later, or are you just wanting to plot it and see what it looks like?

If your data is gridded (your ~10000 lines cover every combination of x and y values in the range that you are interested in), then you can use the contour command in Gadfly, which is the volcano plot you described. You'll first need to reshape the data so it's a 2D array: think of it as displaying a 2D image, where the number at each point is the density. However, for displaying this kind of data, I prefer heatmaps, and I don't know if Gadfly supports those - you may have to look into the histogram2d command.

If it is not gridded (the x and y points don't have any particular structure to them), it's still possible, but you have to choose a way to decide how you want to turn it from unstructured data into a 2D image. The histogram2d approach that Tom showed above is one option, where you treat each density measurement as a weighted measurement in a histogram. But if your data represents single measurements of a function that has meaningful values away from those measured points, you probably want to interpolate between those points. For this you can use a package like Dierckx, which does interpolations on unstructured data. I also have some simple code that does barycentric triangular interpolation between unstructured points, in case you wanted to have a look at that. 

This may be overkill, however, if you just want to look at the data and don't plan to interpolate or draw from those density values later. If that's the case, the trisurface plot above might be just what you need for showing you the shape of your density data.

Whatever you choose, I can recommend Tom's Plots package as a nice interface to the other plotting packages in Julia - it makes it easy to switch between different plotting options like Gadfly and PyPlot depending on what features they offer.

Cheers,
Scott

ameliafit...@gmail.com

unread,
May 16, 2016, 11:49:04 AM5/16/16
to julia-users
Yes, my data is gridded. And no, I don't plan to sample these density values later. I just want to plot it and see what it looks like :) I'm not sure how to reshape the data into a 2D array though, or how to make a contour plot from a dataset rather than from a function. Is there anything on this in the documentation? 

Thanks for your help, 

Amelia

Tom Breloff

unread,
May 16, 2016, 11:56:14 AM5/16/16
to julia-users
If your data is gridded (but just in vector form), then likely you could just "zmat = reshape(z, numrows, numcols)" and then "contour(zmat)".

ameliafit...@gmail.com

unread,
May 17, 2016, 9:11:27 AM5/17/16
to julia-users
That looks like it might be exactly what I'm hoping to do. Coming from a gnuplot background, I'm having a lot of trouble with the Gadlfy syntax, though. I can't seem to find any comprehensive documentation that lists all of the commands/options, and trying to understand it inductively from the minimal examples on the Gadfly site is frustrating. So far I haven't been able to make the contour plot that I'm hoping to make. Is there a resource you would recommend for learning Gadfly?

Tom Breloff

unread,
May 17, 2016, 9:36:34 AM5/17/16
to julia-users
I would recommend learning to use Plots instead!
Reply all
Reply to author
Forward
0 new messages