初始化設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #r "nuget: Microsoft.Data.Analysis, 0.21.1"
#r "nuget: MathNet.Numerics, 5.0.0"
#r "nuget:ScottPlot, 5.0.*"
using System.IO; using Microsoft.Data.Analysis; using MathNet.Numerics.Distributions;
using Microsoft.DotNet.Interactive.Formatting; Formatter.Register(typeof(ScottPlot.Plot), (p, w) => w.Write(((ScottPlot.Plot)p).GetImageHtml(400, 300)), HtmlFormatter.MimeType);
|
載入 CSV 資料
可參考這篇 DataFrame 載入 CSV 資料
使用 SignalXY 繪圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| ScottPlot.Plot myPlot1 = new();
int[] rowIndex = Enumerable.Range(0, (int)df.Rows.Count).ToArray();
double[] durationsY = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Duration"][x])).ToArray();
ScottPlot.Plottables.SignalXY sigAll1 = myPlot1.Add.SignalXY(rowIndex, durationsY); sigAll1.LegendText = "Duration";
double[] pulseY = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Pulse"][x])).ToArray();
ScottPlot.Plottables.SignalXY sigAll2 = myPlot1.Add.SignalXY(rowIndex, pulseY); sigAll2.LegendText = "Pulse";
double[] maxpulseY = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Maxpulse"][x])).ToArray();
ScottPlot.Plottables.SignalXY sigAll3 = myPlot1.Add.SignalXY(rowIndex, maxpulseY); sigAll3.LegendText = "Maxpulse";
double[] caloriesY = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Calories"][x])).ToArray();
ScottPlot.Plottables.SignalXY sigAll4 = myPlot1.Add.SignalXY(rowIndex, caloriesY); sigAll4.LegendText = "Calories";
myPlot1.Legend.Alignment = ScottPlot.Alignment.UpperRight;
myPlot1
|
使用 Scatter 繪圖,分析關聯性
使用「Duration」作為 x 軸,使用「Calories」作為 y 軸
1 2 3 4 5 6 7 8 9 10
| ScottPlot.Plot myPlot2 = new();
ScottPlot.Plottables.Scatter sp1 = myPlot2.Add.Scatter(durationsY, caloriesY);
sp1.LineWidth = 0;
myPlot2
|
使用「Duration」作為 x 軸,使用「Maxpulse」作為 y 軸
1 2 3 4 5 6 7 8 9 10
| ScottPlot.Plot myPlot3 = new();
ScottPlot.Plottables.Scatter sp2 = myPlot3.Add.Scatter(durationsY, maxpulseY);
sp2.LineWidth = 0;
myPlot3
|
使用 Bars 繪圖,分析 Duration 頻率
目前 ScottPlot 5.0 版未實現直方圖,搭配 MathNet.Numerics 做計算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ScottPlot.Plot myPlot4 = new();
double[] durationsY = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df["Duration"][x])).ToArray();
int binCount = 10;
var histogram = new MathNet.Numerics.Statistics.Histogram(durationsY, binCount);
double[] binEdges = new double[binCount + 1]; double[] binCounts = new double[binCount];
for (int i = 0; i < binCount; i++) { binEdges[i] = histogram[i].LowerBound; binCounts[i] = histogram[i].Count; } binEdges[binCount] = histogram[binCount - 1].UpperBound;
ScottPlot.Plottables.BarPlot bar = myPlot4.Add.Bars(binEdges.Take(binCount).ToArray(), binCounts);
myPlot4.Axes.Left.TickGenerator = new ScottPlot.TickGenerators.NumericFixedInterval(20);
foreach(var item in bar.Bars){ item.BorderColor = ScottPlot.Color.FromColor(System.Drawing.Color.Blue); item.BorderLineWidth = 10; }
myPlot4
|