Speedometer Colorplot (xfig example)

From Remeis-Wiki
Jump to navigation Jump to search
“Speedometer”-like colormap for data points
variable width=10, height=10;
variable xfig = xfig_plot_new(width,height);
xfig.world(0, 10, 0, 10);

variable data = struct{ x=[1:9:1], y=[7:7:#9], z=[1:9:1] };        % data to be plotted; the z-component will be color-coded

% --------------------------------
% define colormap for z-component:
variable z_min = nint(min(data.z)), z_max = nint(max(data.z));     % nint -> cast to nearest integer, which is useful for labels
while(pos_modulo(z_max,4)!=0){z_max++;}                            % ensures that z_max is a multiple of 4, which is useful if there are four plus one major ticmarks (see below)
define define_z_color(z)
{
  variable fraction = (1.*(z-z_min)/(z_max-z_min));
  if(fraction<1/4.)
    return xfig_mix_colors("red","gold",fraction*4);
  else if(1/4.<=fraction<2/4.)
    return xfig_mix_colors("green","red",(fraction-1/4.)*4);
  else
    return xfig_mix_colors("blue","green",(fraction-2/4.)*4);
}
% --------------------------------
% ------------------------------
% plot colormap for z-component:
variable position_z_label = [0.05,0.07];                           % position of the color "speedometer" in the plot
xfig_plot_text( xfig, "$z$"R, position_z_label[0], position_z_label[1], 0, 0; depth=1, world0);
variable angle_step = PI/2./z_max;
variable i, z_color;
_for i(0, z_max, 1)
{
  z_color = define_z_color(i);
  % "major ticmarks":
  if(i==0 or i==z_max/4 or i==z_max/2 or i==z_max*3/4 or i==z_max) 
  {
    xfig.plot( position_z_label[0]+cos(angle_step*i)*[0.05,0.3]/width*height, position_z_label[1]+sin(angle_step*i)*[0.05,0.3]; color=z_color, depth=0, world0);
    xfig_plot_text( xfig, sprintf("$%d$"R,i), position_z_label[0]+cos(angle_step*i)*0.35/width*height, position_z_label[1]+sin(angle_step*i)*0.35, 0, 0;
                    depth=1, world0, rotate=angle_step*i*180/PI, color=z_color);
  }
  % "minor ticmarks"
  else
    xfig.plot( position_z_label[0]+cos(angle_step*i)*[0.05,0.275]/width*height, position_z_label[1]+sin(angle_step*i)*[0.05,0.275]; color=z_color, depth=0, world0);
}
% ------------------------------
% -----------------------------
% plot color-coded data points:
_for i(0, length(data.x)-1, 1)
{
  z_color = define_z_color(data.z[i]);
  xfig.plot(data.x[i], data.y[i]; color=z_color, fill=20, fillcolor=z_color, sym="square", size=2);
}
% -----------------------------
xfig.render("colored_data_points.pdf");