GiovineItalia / Gadfly.jl

Crafty statistical graphics for Julia.
http://gadflyjl.org/stable/
Other
1.91k stars 250 forks source link

Please add support for filled-in contour plots #551

Open jumutc opened 9 years ago

jumutc commented 9 years ago

I want to create in Gadfly something similar to imagesc/contourf in Winston or Matlab, but using Geom.rectbin seems to me incomplete and cannot to some full extent handle this functionality. Please find several inconsistencies provided in the plot (alike white imagesc regions). Please consider the following snippet of my code:

using MAT
vars = matread(joinpath("..", "data","ripley.mat"))
X = vars["X"]'
y = vars["Y"]

range1 = linspace(-1.5,1.5,200)
range2 = linspace(-0.5,1.5,200)
Xtest = meshgrid(range1,range2)'
diff1 = range1[2] - range1[1]
diff2 = range2[2] - range2[1]

yhat = ... #do predictions for points
yplot = ...#do predictions for plot 
yplot = yplot - minimum(yplot)
yplot = 2*(yplot ./ maximum(yplot)) - 1

using DataFrames
df = DataFrame()
df[:Xmin] = Xtest[1,:][:] 
df[:Ymin] = Xtest[2,:][:]
df[:Xmax] = Xtest[1,:][:] + diff1
df[:Ymax] = Xtest[2,:][:] + diff2
df[:class] = yplot[:]

using Gadfly
set_default_plot_size(20cm, 20cm)
plot(layer(x=X[1,yhat.>0], y=X[2,yhat.>0], Geom.point, Theme(default_color=color("orange"))),
     layer(x=X[1,yhat.<0], y=X[2,yhat.<0], Geom.point, Theme(default_color=color("black"))),
     layer(df, x_min="Xmin", x_max="Xmax", y_min="Ymin", y_max="Ymax", color="class", Geom.rectbin))
jumutc commented 9 years ago

plot(z=reshape(yplot,200,200), Geom.contour, xmin=[-1.5], xmax=[1.5], ymin=[-.5], ymax=[1.5]) is giving something else too contour

darwindarak commented 9 years ago

Geom.contour indexes the z matrix as [xcoord, ycoord]. So you should get the correct contour if you transpose z:

x = linspace(-1.5, 1.5, 200)
y = linspace(-0.5, 1.5, 200)
plot(z=reshape(yplot,200,200)', x=coords, y=coords, Geom.contour)

Do you mind trying (separately)

to help narrow down the source of the problem?

jumutc commented 9 years ago

Hi, thanks for the suggestion.. By using this indexing I can get: contour But in reality I'm more interested in getting filled-in contour plot, like the one I could get with Geom.rectbin

dcjones commented 9 years ago

@jumutc Is the main issue you have with Geom.rectbin the white lines that you're seeing?

There's an SVG trick I used that aligns the rectangles to the nearest pixel, but it seems that can cause issues in a dense grid like in your plot, which explains the white lines. If that's fixed, would it otherwise be sufficient for your needs?

jumutc commented 9 years ago

Hi, @dcjones. That would be perfect for me. But there is also an issue with completely blank regions (outside of y_max, x_max etc) which sometimes appear on the figure. If the latter can be fixed quickly I would be very pleased. Thanks in advance!

CorySimon commented 9 years ago

+1, contour plots filled in with color would be very helpful. (probably a wrapper around rectbin).