[Scilab-users] filled contour plot

classic Classic list List threaded Threaded
9 messages Options
Claus Futtrup Claus Futtrup
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] filled contour plot

Dear Scilabers

In my endeavours to plot radiation pattern for loudspeakers, besides polar plot, I'm also attemptning to view my data with a sonogram (2D contour plot). Here I ask for help to manage the colors of the plot. Here's the code (same reduced data as before):

// Test data
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];
magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];
// 3 freq x 7 angles = 21 data points, put into a matrix.
// In the complete dataset there's 183 frequencies and 72 angles.

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);

angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

// Plot example:
contourplt = scf();
xset("fpf"," "); // Floating Point Format
contourplt.color_map=jetcolormap(5); // blue = cold, red = hot :-)
contour2d(frequencies,angles,magnitudes,50:10:90,logflag="ln");
contourf(frequencies,angles,magnitudes); // FILL with color
contour2d(frequencies,angles,magnitudes,50:10:90);
legends(string(50:10:90),1:5,"lr");

P.S. "funny" - I'm plotting contour2d first, because then I can specify logflag and the numeric labels goes bye-bye. If I execute contourf first, I cannot use log-x-axis. contourf complains that Nax cannot work with log-x-axis data, but contourf fills the plot anyway. I then redo the contour2d to get my contourlines back (because contourf paints over them).

Help how to get the right colors in the plot would be highly appreciated. In particular contourf complains: Colormap too small

Can I manage colormap for contour2d and contourf separately?

Best regards,
Claus

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

Re: filled contour plot

Hi Scilabers

I can explain a bit more. If I double the number in the colormap (from 5 to 10), then contourf doesn't fail (in the previous code example).
Then the contour lines only use the first half of the color map (from blue to green) ... maybe it would be better if contour-lines where white. Can I do that, somehow?

Best regards,
Claus

On Sun, Jan 21, 2018 at 3:55 PM, Claus Futtrup <[hidden email]> wrote:
Dear Scilabers

In my endeavours to plot radiation pattern for loudspeakers, besides polar plot, I'm also attemptning to view my data with a sonogram (2D contour plot). Here I ask for help to manage the colors of the plot. Here's the code (same reduced data as before):

// Test data
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];
magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];
// 3 freq x 7 angles = 21 data points, put into a matrix.
// In the complete dataset there's 183 frequencies and 72 angles.

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);

angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

// Plot example:
contourplt = scf();
xset("fpf"," "); // Floating Point Format
contourplt.color_map=jetcolormap(5); // blue = cold, red = hot :-)
contour2d(frequencies,angles,magnitudes,50:10:90,logflag="ln");
contourf(frequencies,angles,magnitudes); // FILL with color
contour2d(frequencies,angles,magnitudes,50:10:90);
legends(string(50:10:90),1:5,"lr");

P.S. "funny" - I'm plotting contour2d first, because then I can specify logflag and the numeric labels goes bye-bye. If I execute contourf first, I cannot use log-x-axis. contourf complains that Nax cannot work with log-x-axis data, but contourf fills the plot anyway. I then redo the contour2d to get my contourlines back (because contourf paints over them).

Help how to get the right colors in the plot would be highly appreciated. In particular contourf complains: Colormap too small

Can I manage colormap for contour2d and contourf separately?

Best regards,
Claus


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

Re: filled contour plot

Hi Claus,

 

Pls check if this solution is satisfactory:

 

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];
 
magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];
 
negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);
 
clf;
contourplt = scf();
contourplt.color_map=jetcolormap(6); // blue = cold, red = hot :-)
nz = 40:10:90;
contour2d(frequencies,angles,magnitudes,nz,logflag="ln");
contourf(frequencies,angles,magnitudes,nz); 
legends(string(nz),1:length(nz),"lr");
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

 

Regards,

Rafael

 

 


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

Re: filled contour plot

Hi Rafale

Thanks for your kind help. I see how a carefully selected nz controls the plot.

Actually my data set is much larger. If we look at the magnitudes, they go from 42 to 92 (in the big data from 28 to 94) dB. If I could make a very beautiful color map of 94-28 = 66 different colors, that would be excellent. My experiments show that with jetcolormap it makes sense to have 10. So 10 it is.

If the lines then only go from black to yellow, so be it ... but if they could be black (or better - white - which will show very clearly in the map), it would be better. I look at the examples in the contourf ... and the last example shows many gradients from yellow to red + some black dash-dotted contour lines. My only problem is that this example is not well documented (there's essentially no explanation at all). Looking at the parameters is cryptic.

P.S. Yes, I add xlabel and ylabel. :-) ... Good idea.

Best regards,
Claus

On Sun, Jan 21, 2018 at 5:01 PM, Rafael Guerra <[hidden email]> wrote:

Hi Claus,

 

Pls check if this solution is satisfactory:

 

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];
 
magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];
 
negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);
 
clf;
contourplt = scf();
contourplt.color_map=jetcolormap(6); // blue = cold, red = hot :-)
nz = 40:10:90;
contour2d(frequencies,angles,magnitudes,nz,logflag="ln");
contourf(frequencies,angles,magnitudes,nz); 
legends(string(nz),1:length(nz),"lr");
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

 

Regards,

Rafael

 

 


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



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

Re: filled contour plot

Hi Claus,

Please check code below which produces the plot herein.
Optionally you can add contours to it.
However the contour level labels do not seem to display on such log-plots. A
possible contour2d bug?

<http://mailinglists.scilab.org/file/t495698/contourf_colorbar.gif>  

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];

magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

clf;
nz = 40:10:90;
nz2 = 40:1:90;
cplot = gcf();
cplot.color_map= jetcolormap(length(nz2)); // blue = cold, red = hot :-)
contour2d(frequencies,angles,magnitudes,nz,logflag="ln");
contourf(frequencies,angles,magnitudes,nz2);
cplot.children.data_bounds = [200,-180;2e4,180];
// contour2d(frequencies,angles,magnitudes,nz,logflag="ln");  // Optionally
add contours
// xset("fpf","%.0f");   // bug? not working for logarithmic axes...
colorbar(min(magnitudes),max(magnitudes))
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

Regards,
Rafael




--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Claus Futtrup Claus Futtrup
Reply | Threaded
Open this post in threaded view
|

Re: filled contour plot

Hi Rafael

Thank you very much for your kind help. The very detailed contourf  works now. Also thanks for the tip with the colorbar - it looks great. I see how re-painting with contour2d brings back the contour lines. With the many colours in the colormap, all lines are blue, which is OK. :-) I guess the answer is I cannot use a different colormap (or any specific color) for these lines.

In the last example of contourf you see some black lines. The example shows how to remove the lines (the last few lines of code) and it appeasr that feeding a vector of zeros into contourf as the last input parameter turns these lines on. I wonder why the example uses 0*ones(m) and not simply zeros(m) ... there are so many strange things wtih example 3, and the learning curve is much too steep. Like I mentioned before, IMO example 3 is cryptic.

Again, Rafael, thank you for your kind help. I believe the plot is now how it should be.

Best regards,
Claus

On Sun, Jan 21, 2018 at 9:38 PM, Rafael Guerra <[hidden email]> wrote:
Hi Claus,

Please check code below which produces the plot herein.
Optionally you can add contours to it.
However the contour level labels do not seem to display on such log-plots. A
possible contour2d bug?

<http://mailinglists.scilab.org/file/t495698/contourf_colorbar.gif>

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];

magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

clf;
nz = 40:10:90;
nz2 = 40:1:90;
cplot = gcf();
cplot.color_map= jetcolormap(length(nz2)); // blue = cold, red = hot :-)
contour2d(frequencies,angles,magnitudes,nz,logflag="ln");
contourf(frequencies,angles,magnitudes,nz2);
cplot.children.data_bounds = [200,-180;2e4,180];
// contour2d(frequencies,angles,magnitudes,nz,logflag="ln");  // Optionally
add contours
// xset("fpf","%.0f");   // bug? not working for logarithmic axes...
colorbar(min(magnitudes),max(magnitudes))
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

Regards,
Rafael




--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users


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

Re: filled contour plot

Hi Claus,

Yes, you can use different colors for the contours.
See sample code here below which displays grey contours but could be adapted
to display contours with different colors for each contour level.

<http://mailinglists.scilab.org/file/t495698/contour2d_logscale_colorfiled_colored_contour.png>

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];

magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

clf;
zn = 40:10:90;
nz = length(zn);
zn2 = 40:0.5:90;
nz2 = length(zn2);
f = gcf();
f.color_map= jetcolormap(nz2);
contour2d(frequencies,angles,magnitudes,zn,logflag="ln");
contourf(frequencies,angles,magnitudes,zn2);
f.children.data_bounds = [200,-180;2e4,180];
xset("fpf","%.0f");  // bug? not working for logarithmic axes...
cntcolorname = "grey";  // define contour color
cntcolor = color(cntcolorname);
cntcolor = repmat(cntcolor, 1, nz); // define array with as many colors as
contours
f.color_map = [f.color_map; name2rgb(cntcolorname)];
contour2d(frequencies,angles,magnitudes,zn,style=cntcolor,logflag="ln");
e = gce();
N = length(e.children);
for i = 1:nz-1
    e.children(i).children.line_style = 2;  // dashed contours
end

colorbar(min(magnitudes),max(magnitudes),[1 nz2])
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

Regards,
Rafael



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Claus Futtrup Claus Futtrup
Reply | Threaded
Open this post in threaded view
|

Re: filled contour plot

Hi Rafael

These examples are an enormous help to me. I can say for sure, I would have never found out by reading the documentation. The "refmat" is new to me. I now see how you can "stack" several colormaps with the plot, simply  f.color_map = [f.color_map; name2rgb(cntcolorname)]; ... the semicolon gives space to a new one. I see how you call contour2d with the style option the second time. Supposedly the default is the first one, but you invoke the second one with the style option. I see how you now define the length of the colorbar because if you don't, then the colorbar will paint both colormaps on the bar :-/

Thank you for suggesting the grey colors, it works excellent together with the jetcolors.

Best regards,
Claus

On Mon, Jan 22, 2018 at 8:51 PM, Rafael Guerra <[hidden email]> wrote:
Hi Claus,

Yes, you can use different colors for the contours.
See sample code here below which displays grey contours but could be adapted
to display contours with different colors for each contour level.

<http://mailinglists.scilab.org/file/t495698/contour2d_logscale_colorfiled_colored_contour.png>

//START OF CODE
frequencies = [200 2000 20000]; // Hz
angles = [0 30 60 90 120 150 180]; // degrees - convert to radians!
mag00 = [69.253448 92.163385 87.414209]; // on-axis freq-response
mag30 = [68.461357 90.569082 76.073549];
mag60 = [65.842262 90.745621 74.152067];
mag90 = [59.420658 80.893365 55.975304];
mag120 = [52.505173 80.795915 44.363582];
mag150 = [53.829987 59.793715 42.443964];
mag180 = [55.650341 73.457973 55.608187];

magnitudes = [mag00' mag30' mag60' mag90' mag120' mag150' mag180'];

negative_angles = -angles(2:7);
neg_angle_magnitudes = magnitudes(:,2:7);
angles = [angles negative_angles];
magnitudes = [magnitudes neg_angle_magnitudes];
[angles idx] = gsort(angles,'g','i');
magnitudes = magnitudes(:,idx);

clf;
zn = 40:10:90;
nz = length(zn);
zn2 = 40:0.5:90;
nz2 = length(zn2);
f = gcf();
f.color_map= jetcolormap(nz2);
contour2d(frequencies,angles,magnitudes,zn,logflag="ln");
contourf(frequencies,angles,magnitudes,zn2);
f.children.data_bounds = [200,-180;2e4,180];
xset("fpf","%.0f");  // bug? not working for logarithmic axes...
cntcolorname = "grey";  // define contour color
cntcolor = color(cntcolorname);
cntcolor = repmat(cntcolor, 1, nz); // define array with as many colors as
contours
f.color_map = [f.color_map; name2rgb(cntcolorname)];
contour2d(frequencies,angles,magnitudes,zn,style=cntcolor,logflag="ln");
e = gce();
N = length(e.children);
for i = 1:nz-1
    e.children(i).children.line_style = 2;  // dashed contours
end

colorbar(min(magnitudes),max(magnitudes),[1 nz2])
xlabel("frequency [Hz]");
ylabel("angle (deg)");
//END OF CODE

Regards,
Rafael



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users


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

Re: filled contour plot

Hi Claus,

 

You are welcome.

Herein some improved code to replace the section that sets dashed line contours:

 

for i = 1:nz

    if e.children(i).type == "Compound" then

        e.children(i).children.line_style = 2;  // dashed contours

    end

end

 

Regards,

Rafael

 


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