How do I create and plot a ContourSeries with Oxyplot?
Asked Answered
A

1

9

I have a WPF application where I need to visualize y = y(x1, x2) where x1, x2 are linear coordinates. I can do this using the HeatMapSeries in Oxyplot, but when I want to plot two sets of data in the same window, heatmaps are not the proper tool. A couple of contour series would be better. Now, I have tried to achieve this in the same manner as with the HeatMapSeries, that worked pretty well:

public void PlotHeatMap (){

   OxyPlot.PlotModel model = new PlotModel { Title = "2-D data" };
   model.Axes.Add( new OxyPlot.Axes.LinearColorAxis { 
   Position = OxyPlot.Axes.AxisPosition.Right, 
   Palette = OxyPalettes.Jet( 500 ), 
   HighColor = OxyColors.Gray, 
   LowColor = OxyColors.Black } );

   OxyPlot.Series.HeatMapSeries heatmap = new OxyPlot.Series.HeatMapSeries {
     Data = ( Double[ , ] )data,
     X0 = x1min,
     X1 = x1max,
     Y0 = x2min,
     Y1 = x2max
    };

   model.Series.Add( heatmap );
}

Output from the HeatMapSeries

Now, when I try to use the ContourSeries instead, I just replace the HeatMapSeries with a ContourSeries:

public void PlotContour (){

   OxyPlot.PlotModel model = new PlotModel { Title = "2-D data" };
   model.Axes.Add( new OxyPlot.Axes.LinearColorAxis { 
   Position = OxyPlot.Axes.AxisPosition.Right, 
   Palette = OxyPalettes.Jet( 500 ), 
   HighColor = OxyColors.Gray, 
   LowColor = OxyColors.Black } );

   OxyPlot.Series.ContourSeries contour = new OxyPlot.Series.ContourSeries {
      ColumnCoordinates = arrayFromMinToMax1,
      RowCoordinates = arrayFromMinToMax2,
      ContourLevels = arrayOfLevels,
      ContourColors = arrayOfColors, // Same # elements as the levels' array
      Data = ( Double[ , ] )data
    };

   model.Series.Add( contour );
}

This just produce the output:

Output from the ContourSeries attempt

The x- and y-axes are there, and match the min and max coordinates, but I can see no contour lines. I suspect that there is something missing with setting up the Axis (should it be the same as for the HeatMapSeries??). I don't know how to proceed with this contour plot. Are there examples other than e.g. the ContourSeriesExamples at GitHub?

Thanks for any help!

Adriatic answered 18/5, 2015 at 13:58 Comment(7)
Could it simply be that you are plotting the wrong region? Looking at the axes in your contour plot they appear to be flipped compared to the heat map plot. What if you set ColumnCoordinates = arrayFromMinToMax2 and RowCoordinates = arrayFromMinToMax1 instead?Schram
Thanks for pointing that out, Anders, but the result is the same. I updated the image, though.Adriatic
If you look at the HeatMapSeriesExamples it even combines a heat map with contours, so contours should really be straightforward. Let the ContourSeries decide the levels and colors itself, i.e. remove the ContourLevels and ContourColors properties from the initializer, and see if this gives any improved resut.Schram
That's what I thought too. Straightforward! And combining heatmap with contour would actually be my first choice for my application. When I look at the scale of my empty contour plot and see it ranges from 0 to 100, while the data is the same as in the heatmap, i.e. 0 to 0.08. Is there a way this can be problematic?Adriatic
I tested to rescale the Peaks example in the ContourSeriesExamples, and I also shifted it so that values were between -0.03 and 0.07. No problem displaying the contours. Can I recommend that you start with a simpler dataset and make ContourSeries work for that, then move on to your original dataset? By the way, why do you need to cast data to double[,]?Schram
One more thing, although I don't think it has any impact on your problem: you don't need to add a LinearColorAxis to your PlotModel when you are plotting a ContourSeries. Also, could you publish the data array somewhere, e.g. as a Gist on Github or on ideone.com? Maybe the "flows" in the data compicates things for the contour generator?Schram
Anders, the cast is necessary because <code>data</code> is of a user-defined type, sort of a matrix class.Adriatic
A
3

I finally found what was wrong - it was my mistake! The ColumnCoordinates and the RowCoordinates arrays must match the size of the DoubleArray Data! And I didn't make sure they were. Now the contour and heatmap align! Thanks Anders for support and pushing me into my own code!

HeatMap with Contour

Adriatic answered 19/5, 2015 at 12:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.