ggplotd - curve colour

brocolis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 9 18:52:41 PDT 2016


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.



More information about the Digitalmars-d-learn mailing list