MathGL 2D plot from 2D data file

632 views
Skip to first unread message

Daniel-M

unread,
May 14, 2012, 2:35:49 PM5/14/12
to mat...@googlegroups.com
i'm trying to make use of MathGL (i'm currently using 1.11 version) to make a plot on a x-y plane of a data set

The task i'm doing is generating plain text with 2 columns of data (x,y points) and i want to plot them using MathGL.

The problem is that, when i compile the code (below) and run the executable, i get a png file with no points, lines, or anithing but the axes.
When i use gr->Plot (see below) instead of gr->Mark i get many lines and it seems that the points lie on the sides of the plot.

I just can't figure out wath's the problem, i'm kind of begginer with OOP and C++, and reading the documentation i could get far than this.

i put the c++ code that i'm using below:

=============== CODE C++ ================
#include<iostream>
#include<fstream>
#include<cmath>

// MathGL Headers

#include<mgl/mgl.h>
#include <mgl/mgl_zb.h>
#include <mgl/mgl_data.h>


int main()
{

    std::ofstream salida("ikeda_map.dat");  // open file for data storing
    double Xi1,Xi=0,Yi1,Yi=0,th;                //defining some variables and "initial conditions"
    double alfa=6,beta=0.4,gama=1,mu=0.9; // defining some parameters for the problem
    int indice,iterations;                               // some indices
   
    salida << Xi << "\t" << Yi << "\n";         // writing initial conditions to file

    for(indice=0;indice<iterations;indice++)    // Iteration of the Ikeda Map.
    {

        th  = beta - alfa/(1+pow(Xi,2)+pow(Yi,2));
        Xi1 = gama + mu*(Xi*cos(th)-Yi*sin(th));
        Yi1 = mu*(Xi*sin(th)+Yi*cos(th));   

        salida << Xi1 << "\t" << Yi1 << "\n";         // saving data points

        Xi=Xi1;
        Yi=Yi1;
    }
   

    mglGraph *gr = new mglGraphZB;    // Creating MathGL Graph (using v 1.11)


    mglData y("ikeda_map.dat");         // Loading Data - maybe here's the problem
    gr->Box();
    gr->Mark(y,"r#.");                         // the type of plot that i want is only points

    gr->WritePNG("ikeda-mgl.png");     // save the plot to a file called ikeda-mgl.png
    delete gr;
    return 0;
   
}

========= end of code C++ =========


i hope you can help me.

Thanks.

Daniel M.

mathgl....@gmail.com

unread,
May 14, 2012, 3:00:22 PM5/14/12
to mat...@googlegroups.com
Dear Daniel

2012/5/14 Daniel-M <daniel...@gmail.com>:
Here you receive data array as matrix 2 * (iterations+1). As I
understand first column of it contain x-values, second one -- y-
values. So, let write it explicitly

mglData xdat=y.SubData(0), ydat=y.SubData(1);

Now you need to set axis ranges as xdat, ydat ranges:

gr->XRange(xdat); gr->YRange(ydat); // for v.1.*
//gr->SetRanges(xdat,ydat); // for v.2.*

>     gr->Box();
>     gr->Mark(y,"r#.");                         // the type of plot that i
> want is only points

Mark() function require 2 arguments mark positions and mark radius. In
yours case the more appropriate is Plot() function, which draw mark of
the same size.

gr->Plot(xdat,ydat,"r#. ");

I add ' ' style for disabling line drawing between marks.

--
All the best,
Alexey Balakin

Daniel-M

unread,
May 14, 2012, 4:41:38 PM5/14/12
to mat...@googlegroups.com
Many thanks! it did work!!

Now i see were i was wrong.

i have just another question, it's there a way to modify the "PNG" image size, i mean, what i a want  to generate a 1680x1050 pixels PNG (or other format) that was another topic that i couldn't find in the documentantion for MathGL v 1.11

Many Thanks for your opportune response!

El lunes, 14 de mayo de 2012 14:00:22 UTC-5, Alexey Balakin escribió:
Dear Daniel

2012/5/14 Daniel-M <>:

Daniel-M

unread,
May 14, 2012, 7:31:49 PM5/14/12
to mat...@googlegroups.com
The way is by using the option SetSize

gr->SetSize(1680,1050);  // for a PNG with 1680x1050 pixels of size
Reply all
Reply to author
Forward
0 new messages