52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using ScottPlot;
|
|
using System.Diagnostics;
|
|
using Point = KMeansBase.Point;
|
|
using ScottPlot.Legends;
|
|
using Color = ScottPlot.Color;
|
|
|
|
namespace kMeans;
|
|
|
|
public static class Helper
|
|
{
|
|
public static Plot CreatePlot(IEnumerable<Point> points, IEnumerable<Point> centroids)
|
|
{
|
|
var plot = new Plot();
|
|
|
|
var legend = new StandardLegend { Alignment = Alignment.UpperRight };
|
|
plot.Legends.Add(legend);
|
|
var colors = new Dictionary<int, Color>();
|
|
var colorGroups = points.GroupBy(x => x.ClusterId);
|
|
foreach (var clusterGroup in colorGroups)
|
|
{
|
|
var color = plot.Add.NextColor;
|
|
colors.Add(clusterGroup.Key, color);
|
|
var xs = clusterGroup.Select(p => p.X).ToArray();
|
|
var ys = clusterGroup.Select(p => p.Y).ToArray();
|
|
plot.Add.Scatter(xs, ys);
|
|
}
|
|
|
|
const MarkerShape marker = MarkerShape.FilledSquare;
|
|
const float size = 10f;
|
|
foreach (var (x, y, clusterId) in centroids)
|
|
{
|
|
var color = colors.TryGetValue(clusterId, out var c) ? c : plot.Add.NextColor;
|
|
var scatter = plot.Add.Scatter(new[] { x }, new[] { y }, color);
|
|
scatter.MarkerStyle.Size = size;
|
|
scatter.MarkerStyle.Shape = marker;
|
|
scatter.Label = $"ClusterId: {clusterId}";
|
|
}
|
|
|
|
return plot;
|
|
}
|
|
|
|
public static void ExportPlot(Plot plot, string path)
|
|
{
|
|
plot.SavePng(path, 1000, 1000);
|
|
}
|
|
|
|
public static void PreviewPlot(string path)
|
|
{
|
|
using var p = Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
|
|
p?.WaitForExit();
|
|
}
|
|
} |