[Scilab-users] hist3d & density plots

classic Classic list List threaded Threaded
4 messages Options
Antoine Monmayrant Antoine Monmayrant
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] hist3d & density plots

Hi all,

I'm trying to plot the density of scatter points in (x,y).
Basically, I have a huge Xvec and a huge Yvec of same size that
represents the position of a huge number of points in a 2D plane.
I would like to get a density plot, that is given a certain grid, a 2D
plot representing the number of scatter points within a each cell of the
grid.
In matplotib, this corresponds to functions like "hist2d" and "hexbin".
Is there any equivalent in Scilab?
Or do I have to calculate by hand my density matrix and then use graplot
or contourfill to plot my density?
Any pointer at the least inefficient way to do this calculation is more
than welcome...

Thank you in advance for your help,


Antoine

--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

  Antoine Monmayrant LAAS - CNRS
  7 avenue du Colonel Roche
  BP 54200
  31031 TOULOUSE Cedex 4
  FRANCE

  Tel:+33 5 61 33 64 59
 
  email : [hidden email]
  permanent email : [hidden email]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: hist3d & density plots

Le 10/09/2018 à 09:56, [hidden email] a écrit :

> Hi all,
>
> I'm trying to plot the density of scatter points in (x,y).
> Basically, I have a huge Xvec and a huge Yvec of same size that
> represents the position of a huge number of points in a 2D plane.
> I would like to get a density plot, that is given a certain grid, a 2D
> plot representing the number of scatter points within a each cell of
> the grid.
> In matplotib, this corresponds to functions like "hist2d" and "hexbin".
> Is there any equivalent in Scilab?
> Or do I have to calculate by hand my density matrix and then use
> graplot or contourfill to plot my density?
> Any pointer at the least inefficient way to do this calculation is
> more than welcome...
>
> Thank you in advance for your help,
>
>
> Antoine
>
Hello Antoine,

The most efficient at the Scilab level would be to use the incremental
feature of "sparse":

function d=bins2d(x,y,xbins,ybins)
      n = size(x,"*");
     [ix,cx] = dsearch(x,xbins);
     [iy,cy] = dsearch(y,ybins);
     kin = find((ix>0) & (iy>0));
     d = full(sparse([ix(kin) iy(kin)], ones(kin), [size(xbins,"*")-1,
size(ybins,"*")-1]));
end

x = rand(10000,1,'normal');
y = rand(10000,1,'normal');

--> d = bins2d(x,y,-5:5,-5:5)
  d  =

    0.   0.   0.    0.     1.      0.      0.     0.    0.   0.
    0.   0.   0.    3.     4.      4.      0.     0.    0.   0.
    0.   0.   6.    31.    82.     80.     31.    2.    0.   0.
    0.   0.   24.   183.   477.    477.    200.   25.   3.   0.
    0.   5.   68.   439.   1131.   1158.   426.   57.   4.   0.
    0.   5.   91.   485.   1168.   1220.   438.   72.   3.   0.
    0.   4.   30.   190.   470.    472.    173.   21.   3.   0.
    0.   0.   6.    30.    83.     78.     24.    2.    0.   0.
    0.   1.   0.    2.     2.      3.      3.     0.    0.   0.
    0.   0.   0.    0.     0.      0.      0.     0.    0.   0.


--> sum(d)
  ans  =

    10000.

This feature is fixed in scilab-branch-6.0:

S.

--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
amonmayr amonmayr
Reply | Threaded
Open this post in threaded view
|

Re: hist3d & density plots

Le 10/09/2018 à 11:00, Stéphane Mottelet a écrit :

> Le 10/09/2018 à 09:56, [hidden email] a écrit :
>> Hi all,
>>
>> I'm trying to plot the density of scatter points in (x,y).
>> Basically, I have a huge Xvec and a huge Yvec of same size that
>> represents the position of a huge number of points in a 2D plane.
>> I would like to get a density plot, that is given a certain grid, a
>> 2D plot representing the number of scatter points within a each cell
>> of the grid.
>> In matplotib, this corresponds to functions like "hist2d" and "hexbin".
>> Is there any equivalent in Scilab?
>> Or do I have to calculate by hand my density matrix and then use
>> graplot or contourfill to plot my density?
>> Any pointer at the least inefficient way to do this calculation is
>> more than welcome...
>>
>> Thank you in advance for your help,
>>
>>
>> Antoine
>>
> Hello Antoine,
>
> The most efficient at the Scilab level would be to use the incremental
> feature of "sparse":
>
> function d=bins2d(x,y,xbins,ybins)
>      n = size(x,"*");
>     [ix,cx] = dsearch(x,xbins);
>     [iy,cy] = dsearch(y,ybins);
>     kin = find((ix>0) & (iy>0));
>     d = full(sparse([ix(kin) iy(kin)], ones(kin), [size(xbins,"*")-1,
> size(ybins,"*")-1]));
> end
>
> x = rand(10000,1,'normal');
> y = rand(10000,1,'normal');
>
> --> d = bins2d(x,y,-5:5,-5:5)
>  d  =
>
>    0.   0.   0.    0.     1.      0.      0.     0.    0.   0.
>    0.   0.   0.    3.     4.      4.      0.     0.    0.   0.
>    0.   0.   6.    31.    82.     80.     31.    2.    0.   0.
>    0.   0.   24.   183.   477.    477.    200.   25.   3.   0.
>    0.   5.   68.   439.   1131.   1158.   426.   57.   4.   0.
>    0.   5.   91.   485.   1168.   1220.   438.   72.   3.   0.
>    0.   4.   30.   190.   470.    472.    173.   21.   3.   0.
>    0.   0.   6.    30.    83.     78.     24.    2.    0.   0.
>    0.   1.   0.    2.     2.      3.      3.     0.    0.   0.
>    0.   0.   0.    0.     0.      0.      0.     0.    0.   0.
>
>
> --> sum(d)
>  ans  =
>
>    10000.
Thanks a lot, it works and is really fast even 2*10⁶ scatter points!
>
> This feature is fixed in scilab-branch-6.0:
Huh, what do you mean? Is there a new function bins2d or hist2d in
scilab-branch-6.0?

Antoine
>
> S.
>

--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

  Antoine Monmayrant LAAS - CNRS
  7 avenue du Colonel Roche
  BP 54200
  31031 TOULOUSE Cedex 4
  FRANCE

  Tel:+33 5 61 33 64 59
 
  email : [hidden email]
  permanent email : [hidden email]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: hist3d & density plots

Le 10/09/2018 à 12:57, [hidden email] a écrit :

> Le 10/09/2018 à 11:00, Stéphane Mottelet a écrit :
>> Le 10/09/2018 à 09:56, [hidden email] a écrit :
>>> Hi all,
>>>
>>> I'm trying to plot the density of scatter points in (x,y).
>>> Basically, I have a huge Xvec and a huge Yvec of same size that
>>> represents the position of a huge number of points in a 2D plane.
>>> I would like to get a density plot, that is given a certain grid, a
>>> 2D plot representing the number of scatter points within a each cell
>>> of the grid.
>>> In matplotib, this corresponds to functions like "hist2d" and "hexbin".
>>> Is there any equivalent in Scilab?
>>> Or do I have to calculate by hand my density matrix and then use
>>> graplot or contourfill to plot my density?
>>> Any pointer at the least inefficient way to do this calculation is
>>> more than welcome...
>>>
>>> Thank you in advance for your help,
>>>
>>>
>>> Antoine
>>>
>> Hello Antoine,
>>
>> The most efficient at the Scilab level would be to use the
>> incremental feature of "sparse":
>>
>> function d=bins2d(x,y,xbins,ybins)
>>      n = size(x,"*");
>>     [ix,cx] = dsearch(x,xbins);
>>     [iy,cy] = dsearch(y,ybins);
>>     kin = find((ix>0) & (iy>0));
>>     d = full(sparse([ix(kin) iy(kin)], ones(kin), [size(xbins,"*")-1,
>> size(ybins,"*")-1]));
>> end
>>
>> x = rand(10000,1,'normal');
>> y = rand(10000,1,'normal');
>>
>> --> d = bins2d(x,y,-5:5,-5:5)
>>  d  =
>>
>>    0.   0.   0.    0.     1.      0.      0.     0.    0.   0.
>>    0.   0.   0.    3.     4.      4.      0.     0.    0.   0.
>>    0.   0.   6.    31.    82.     80.     31.    2.    0.   0.
>>    0.   0.   24.   183.   477.    477.    200.   25.   3.   0.
>>    0.   5.   68.   439.   1131.   1158.   426.   57.   4.   0.
>>    0.   5.   91.   485.   1168.   1220.   438.   72.   3.   0.
>>    0.   4.   30.   190.   470.    472.    173.   21.   3.   0.
>>    0.   0.   6.    30.    83.     78.     24.    2.    0.   0.
>>    0.   1.   0.    2.     2.      3.      3.     0.    0.   0.
>>    0.   0.   0.    0.     0.      0.      0.     0.    0.   0.
>>
>>
>> --> sum(d)
>>  ans  =
>>
>>    10000.
> Thanks a lot, it works and is really fast even 2*10⁶ scatter points!
>>
>> This feature is fixed in scilab-branch-6.0:
> Huh, what do you mean? Is there a new function bins2d or hist2d in
> scilab-branch-6.0?
No. "sparse" is broken in scilab-6.0.1, but fixed in scilab-branch-6.0.
Hence the above code will work in scilab-5.5.2 or >= branch-6.0

S.
>
> Antoine
>>
>> S.
>>
>

--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users