Skip to content

Contour Lines: Duplicate Key Error #4787

@ScottSSapphire

Description

@ScottSSapphire

Issue: (Describe the bug here)

Receiving the following error when trying to add contour lines.

I'm following the contour lines with heatmap cookbook example, and the heatmap portion works great, but I get the below when attempting to add contour lines using the same data used for the heatmap.

Error:  "An item with the same key has already been added. Key: 6517"

Stack Trace:
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
   at ScottPlot.Triangulation.RectangularGrid.<>c__DisplayClass0_0.<GetContourLines>b__0(Double z)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ScottPlot.Triangulation.RectangularGrid.GetContourLines(Coordinates3d[,] coordinateGrid, Double[] zs, Boolean fixContourDirections)
   at ScottPlot.Plottables.ContourLines.Update(Coordinates3d[,] coordinateGrid)
   at ScottPlot.PlottableAdder.ContourLines(Coordinates3d[,] coordinates, Int32 count)
   at ScottPlotContourLineError.Program.Main(String[] args) in C:\Users\scott.stanaland\source\repos\ScottPlotContourLineError\Program.cs:line 51

The data needed to repro the issue was too large to embed in the code, so a text file is attached.

testdata.txt

ScottPlot Version: (What NuGet package are you using?)

5.0.54

Code Sample: See http://scottplot.net/faq/repro/ for tips about creating reproducible code samples

using ScottPlot;
using ScottPlot.MultiplotLayouts;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using static System.Runtime.InteropServices.JavaScript.JSType;


namespace ScottPlotContourLineError
{
    internal class Program
    {
        static void Main(string[] args)
        {


            double[,] data;

            double[] ud = { 10.00, 9.80, 9.60, 9.40, 9.20, 9.00, 8.80, 8.60, 8.40, 8.20, 8.00, 7.80, 7.60, 7.40, 7.20, 7.00, 6.80, 6.60, 6.40, 6.20, 6.00, 5.80, 5.60, 5.40, 5.20, 5.00, 4.80, 4.60, 4.40, 4.20, 4.00, 3.80, 3.60, 3.40, 3.20, 3.00, 2.80, 2.60, 2.40, 2.20, 2.00, 1.80, 1.60, 1.40, 1.20, 1.00, 0.80, 0.60, 0.40, 0.20, 0.00, -0.20, -0.40, -0.60, -0.80, -1.00, -1.20, -1.40, -1.60, -1.80, -2.00, -2.20, -2.40, -2.60, -2.80, -3.00, -3.20, -3.40, -3.60, -3.80, -4.00, -4.20, -4.40, -4.60, -4.80, -5.00, -5.20, -5.40, -5.60, -5.80, -6.00, -6.20, -6.40, -6.60, -6.80, -7.00, -7.20, -7.40, -7.60, -7.80, -8.00, -8.20, -8.40, -8.60, -8.80, -9.00, -9.20, -9.40, -9.60, -9.80, -10.00 };
                       
            double[] lr = { -20.00, -19.80, -19.60, -19.40, -19.20, -19.00, -18.80, -18.60, -18.40, -18.20, -18.00, -17.80, -17.60, -17.40, -17.20, -17.00, -16.80, -16.60, -16.40, -16.20, -16.00, -15.80, -15.60, -15.40, -15.20, -15.00, -14.80, -14.60, -14.40, -14.20, -14.00, -13.80, -13.60, -13.40, -13.20, -13.00, -12.80, -12.60, -12.40, -12.20, -12.00, -11.80, -11.60, -11.40, -11.20, -11.00, -10.80, -10.60, -10.40, -10.20, -10.00, -9.80, -9.60, -9.40, -9.20, -9.00, -8.80, -8.60, -8.40, -8.20, -8.00, -7.80, -7.60, -7.40, -7.20, -7.00, -6.80, -6.60, -6.40, -6.20, -6.00, -5.80, -5.60, -5.40, -5.20, -5.00, -4.80, -4.60, -4.40, -4.20, -4.00, -3.80, -3.60, -3.40, -3.20, -3.00, -2.80, -2.60, -2.40, -2.20, -2.00, -1.80, -1.60, -1.40, -1.20, -1.00, -0.80, -0.60, -0.40, -0.20, -0.00, 0.20, 0.40, 0.60, 0.80, 1.00, 1.20, 1.40, 1.60, 1.80, 2.00, 2.20, 2.40, 2.60, 2.80, 3.00, 3.20, 3.40, 3.60, 3.80, 4.00, 4.20, 4.40, 4.60, 4.80, 5.00, 5.20, 5.40, 5.60, 5.80, 6.00, 6.20, 6.40, 6.60, 6.80, 7.00, 7.20, 7.40, 7.60, 7.80, 8.00, 8.20, 8.40, 8.60, 8.80, 9.00, 9.20, 9.40, 9.60, 9.80, 10.00, 10.20, 10.40, 10.60, 10.80, 11.00, 11.20, 11.40, 11.60, 11.80, 12.00, 12.20, 12.40, 12.60, 12.80, 13.00, 13.20, 13.40, 13.60, 13.80, 14.00, 14.20, 14.40, 14.60, 14.80, 15.00, 15.20, 15.40, 15.60, 15.80, 16.00, 16.20, 16.40, 16.60, 16.80, 17.00, 17.20, 17.40, 17.60, 17.80, 18.00, 18.20, 18.40, 18.60, 18.80, 19.00, 19.20, 19.40, 19.60, 19.80, 20.00 };
            // data needed to repro the issue was too long to embed in the code because the text length was too long for the github issue
            // including a text file with the data
            data = GetTestData();


            Plot plt = new();

            Coordinates3d[,] cs = new Coordinates3d[ud.Length, lr.Length];
            for (int y = 0; y < cs.GetLength(0); y++)
            {
                for (int x = 0; x < cs.GetLength(1); x++)
                {
                    cs[y, x] = new(ud[y], lr[x], data[y, x]);
                }
            }

            var hm = plt.Add.Heatmap(cs);

            CoordinateRange yRange = new(ud.Min(), ud.Max());
            CoordinateRange xRange = new(lr.Min(), lr.Max());

            hm.Rectangle = new(xRange, yRange);

            hm.Colormap = new ScottPlot.Colormaps.Jet(); // .MellowRainbow();
            hm.Smooth = true;
            plt.Add.ColorBar(hm);

            // show contour

            //// *** this line caused the error
            var contour = plt.Add.ContourLines(cs, count: 5);
            contour.LabelStyle.Bold = true;
            contour.LinePattern = LinePattern.DenselyDashed;
            contour.LineColor = Colors.Black.WithAlpha(.5);


            plt.YLabel("Up - Down");
            plt.XLabel("Left - Right");


            // display it in a Windows Form
            Form form = new() { Width = 800, Height = 600 };
            ScottPlot.WinForms.FormsPlot formsPlot1 = new() { Dock = DockStyle.Fill };
            formsPlot1.Reset(plt);
            formsPlot1.Refresh();
            form.Controls.Add(formsPlot1);
            form.ShowDialog();


        }

        static double[,] GetTestData()
        {

            string datastr;

            datastr = File.ReadAllText("C:\\worktemp\\testdata.txt");

            var rows = datastr.Split("|");

            if (rows == null) return null;
            var firstrowcols = rows[0].Split(",");

            double[,] data = new double[rows.Length, firstrowcols.Length];

            for (int y = 0; y < rows.Length; y++)
            {
                for (int x = 0; x < firstrowcols.Length; x++)
                {
                    var cols = rows[y].Split(",");

                    double num;

                    if (!double.TryParse(cols[x], out num))
                    {
                        num = double.NaN;
                    }
                    data[y, x] = num;
                }

            }

            return data;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions