Menu

Plotting Poins From Array

Help
Anonymous
2012-09-20
2012-10-16
  • Anonymous

    Anonymous - 2012-09-20

    I have a 3 arrays, one containing x values, one containing y values, one
    containing z values.

    I would like to know how i can use MathGL to make a 3d scatter plot of this
    points.

    Any help would be appreciated.

    This was my attempt, but there is nothing displayed:

    #include <QApplication>
    #include <QDialog>
    #include <QLabel>
    #include <QPixmap>
    #include <QVBoxLayout>
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <mgl/mgl.h>
    #include <mgl/mgl_qt.h>
    
    using namespace std;
    int main(int argc, char* argv[])
    {
      //Qt startup
      QApplication a(argc, argv);
    
      mglData dataX(40200);
      mglData dataY(40200);
      mglData dataZ(40200);
      //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        vector<double> x;
        vector<double> y;
        vector<double> rx;
        vector<double> lx;
        vector<double> ry;
        vector<double> ly;
        vector<double> rz;
        vector<double> lz;
    
        int i = 0;
        int flag = 0;
        double thisSalary;
        ifstream empFile;
        empFile.open("E_field.gnu");
    
        while(empFile>>thisSalary)
                {
                        /*empFile >> thisSalary;
                        cout<<thisSalary<<endl;
                        i++;*/
                        //cout<<flag<<endl;
                        if(flag == 0 && !empFile.eof())
                        {
                                //empFile >> thisSalary;
                                x.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 1)
                        {
                                //empFile >> thisSalary;
                                y.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 2)
                        {
                                //empFile >> thisSalary;
                                rx.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 3)
                        {
                                //empFile >> thisSalary;
                                lx.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 4)
                        {
                                //empFile >> thisSalary;
                                ry.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 5)
                        {
                                //empFile >> thisSalary;
                                ly.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 6)
                        {
                                //empFile >> thisSalary;
                                rz.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 7)
                        {
                                //empFile >> thisSalary;
                                lz.push_back(thisSalary);
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 8)
                        {
                                //empFile >> thisSalary;
                                //cout<<thisSalary<<endl;
                                flag++;
                                i++;
                        }
                        else if(flag == 9)
                        {
                                //empFile >> thisSalary;
                                //cout<<thisSalary<<endl;
                                flag = 0;
                                i++;
                        }
    
                }
                empFile.close();
    
      //Populate array
    
      for(int i =0; i<40200; i++)
      {
        dataX.a[i] = x[i];
        dataY.a[i] = y[i];
        dataZ.a[i] = ry[i];
      }
    
      //Using MathGL to write a plot to file
      cout<<x.size()<<endl;
      mglGraphZB plot;
      plot.Alpha(true);
      plot.Light(true);
      plot.Light(0,mglPoint(1,0,-1));
      mglData data(2,2);
      data.Modify("x*y");
      plot.Axis(mglPoint(0,0,0),mglPoint(1,1,1));
      plot.Rotate(80,40);
      plot.Plot(dataZ);
      plot.Box();
      plot.Puts(mglPoint(0.7,1,1.2),"a(x,y)");
      plot.WriteBMP("CppMathGlExample1.bmp");
    
      //Use Qt to display the saved plot
      QDialog dialog;
      QVBoxLayout layout;
      QPixmap pixmap("CppMathGlExample1.bmp");
      QLabel label;
      label.setPixmap(pixmap);
      layout.addWidget(&label);
      dialog.setLayout(&layout);
      dialog.show();
      return a.exec();
    }
    
     
  • Alexey Balakin

    Alexey Balakin - 2012-09-20

    Just use parametric form of Plot() function. But first you need to be ensure
    that yours data are located inside the axis range. The code can look like

    plot.SetRanges(dataX,dataY,dataZ);  // to be sure that data points are visible
    plot.Plot(dataX,dataY,dataZ);
    
     

Log in to post a comment.