www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ggplotd - curve colour

reply brocolis <brocolis deb.null> writes:
How do I set the color of a curve with ggplotd?
Thanks.
May 07 2016
next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Sunday, 8 May 2016 at 01:50:38 UTC, brocolis wrote:
 How do I set the color of a curve with ggplotd?
 Thanks.
You can set colours by name: https://github.com/BlackEdder/ggplotd/blob/master/source/ggplotd/colour.d#L20 Alternatively you can pass through the RGB value (see the link above for the specification). Finally if you have multiple curves and don't want to specify specific colours you can just give them a different identifier (e.g. different double/int value (any type should do)) and it will chose the colours according to the colourgradient used. There is an example on how to specify your own gradient in the hist3D.svg example: http://blackedder.github.io/ggplotd/ggplotd.html
May 08 2016
parent reply brocolis <brocolis deb.null> writes:
Is this correct usage?
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

The output is a blank png file.

Full source:
import ggplotd.ggplotd;
import ggplotd.geom;
import ggplotd.aes;
import ggplotd.axes;

void main()
{
     import std.array : array;
     import std.algorithm : map;
     import std.range : iota;
     import ggplotd.colour;

     auto f = (double x) { return x; };
     auto xs = iota(-5, 5, 0.1 ).array;
     auto ysfit = xs.map!((x) => f(x)).array;
     auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

     gg.put( xaxisOffset( 0) ).put( yaxisOffset( 0) );
     gg.save( "axes.png", 500, 300 );
}
May 08 2016
parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Monday, 9 May 2016 at 02:29:47 UTC, brocolis wrote:
 Is this correct usage?
 auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
 typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

 The output is a blank png file.

 Full source:
 import ggplotd.ggplotd;
 import ggplotd.geom;
 import ggplotd.aes;
 import ggplotd.axes;

 void main()
 {
     import std.array : array;
     import std.algorithm : map;
     import std.range : iota;
     import ggplotd.colour;

     auto f = (double x) { return x; };
     auto xs = iota(-5, 5, 0.1 ).array;
     auto ysfit = xs.map!((x) => f(x)).array;
     auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
 typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

     gg.put( xaxisOffset( 0) ).put( yaxisOffset( 0) );
     gg.save( "axes.png", 500, 300 );
 }
The problem there is that colour also needs to be an InputRange. This is so that different points can have a different colours associated with it, which is particularly useful if you want to plot some data and have different types of data plotted as different colours. In your example you can either do: ``` auto colour = "red".repeat( xs.length ); auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", typeof(ysfit), "y", typeof(colour), "colour")( xs, ysfit, colour) ) ); ``` Or use the mergeRange function mentioned before, which will automatically repeat if one argument is a single element. ``` auto aes = Tuple!( string, "colour" )( "red" ).mergeRange( Aes!(typeof(xs), "x", typeof(ysfit), "y" )( xs, ysfit ) ); ```
May 08 2016
parent brocolis <brocolis deb.null> writes:
On Monday, 9 May 2016 at 06:24:22 UTC, Edwin van Leeuwen wrote:
 On Monday, 9 May 2016 at 02:29:47 UTC, brocolis wrote:
 Is this correct usage?
 auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
 typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

 The output is a blank png file.

 Full source:
 import ggplotd.ggplotd;
 import ggplotd.geom;
 import ggplotd.aes;
 import ggplotd.axes;

 void main()
 {
     import std.array : array;
     import std.algorithm : map;
     import std.range : iota;
     import ggplotd.colour;

     auto f = (double x) { return x; };
     auto xs = iota(-5, 5, 0.1 ).array;
     auto ysfit = xs.map!((x) => f(x)).array;
     auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
 typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );

     gg.put( xaxisOffset( 0) ).put( yaxisOffset( 0) );
     gg.save( "axes.png", 500, 300 );
 }
The problem there is that colour also needs to be an InputRange. This is so that different points can have a different colours associated with it, which is particularly useful if you want to plot some data and have different types of data plotted as different colours. In your example you can either do: ``` auto colour = "red".repeat( xs.length ); auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", typeof(ysfit), "y", typeof(colour), "colour")( xs, ysfit, colour) ) ); ``` Or use the mergeRange function mentioned before, which will automatically repeat if one argument is a single element. ``` auto aes = Tuple!( string, "colour" )( "red" ).mergeRange( Aes!(typeof(xs), "x", typeof(ysfit), "y" )( xs, ysfit ) ); ```
OK! Thank you.
May 09 2016
prev sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Sunday, 8 May 2016 at 01:50:38 UTC, brocolis wrote:
 How do I set the color of a curve with ggplotd?
 Thanks.
Also see the below example on how to merge Colour with an existing range of points using mergeRange: (Copied from http://blackedder.github.io/ggplotd/stat.html) void main() { /// http://blackedder.github.io/ggplotd/images/function.png import std.random : uniform; import std.typecons : Tuple; import ggplotd.stat : statFunction; import ggplotd.ggplotd : GGPlotD; import ggplotd.geom : geomLine, geomPoint; import ggplotd.aes : mergeRange; auto f = (double x) { return x / (1 + x); }; auto aes = statFunction(f, 0.0, 10); auto gg = GGPlotD().put(geomLine(aes)); // Generate some noisy points auto f2 = (double x) { return x / (1 + x) * uniform(0.75, 1.25); }; auto aes2 = f2.statFunction(0.0, 10, 25); // Show points in different colour auto withColour = Tuple!(string, "colour")("aquamarine").mergeRange(aes2); gg = gg.put(withColour.geomPoint); gg.save("function.png"); }
May 08 2016