使用mathematica在C#中绘图

问题描述 投票:1回答:1

[请帮助我绘制从C#发送到mathematica内核的数据。我想用C#编写mathematica代码并绘制光谱数据。我认为,我已经添加了必要的程序集和参考。我的C#代码如下。

public void button2_Click(object sender, System.EventArgs e)
{
       double baseline;
       int indexOfPeak;
       int minimumIndicesBetweenPeaks;
       int numberOfPixels; 
       double[] spectrum;
       int startingIndex;
       if (spectrometerIndex == -1)
          return; 
       numberOfPixels = wrapper.getNumberOfPixels(spectrometerIndex);
       wrapper.setIntegrationTime(spectrometerIndex, 500000);
       wrapper.setBoxcarWidth(spectrometerIndex, 10);
       wrapper.setCorrectForElectricalDark(spectrometerIndex, 1);
       spectrum = (double[])wrapper.getSpectrum(spectrometerIndex);
       for (int index = 0; index < numberOfPixels; ++index)
       {
           listBox2.Items.Add("pixel[" + index + "] = " +
           spectrum[index]);               
       }
       MathKernel mathKernel = new MathKernel();
       mathkernel.ListPlot[spectrum];
    }            
}
c# plot wolfram-mathematica
1个回答
1
投票

这里有几种方法,基于帖子here

例如,您可能需要为Mathematica格式化spectrum数据的格式

data = "1.234, 2.345, 3.456";

对于GIF格式

MathKernel mathKernel = new MathKernel();
mathKernel.CaptureGraphics = true;
mathKernel.GraphicsFormat = "GIF";
mathKernel.Compute("Show[ListPlot[" + data + "]]");
mathKernel.Graphics[0].Save("C:\\Temp\\plot.gif", System.Drawing.Imaging.ImageFormat.Gif);

用于增强型图元文件(可缩放图形)

MathKernel mathKernel  = new MathKernel();
mathKernel.Compute("ExportString[ListPlot[" + data + "], {\"Base64\", \"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(mathKernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\plot.emf", decodedBytes);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.