how to add a legend for multiple curves?
eg:
analog to matlab's legend:
legend('sin(x)','cos(x)')
https://www.mathworks.com/help/matlab/creating_plots/add-title-axis-labels-and-legend-to-graph.html
looked at continousLegend+friends (https://blackedder.github.io/ggplotd/ggplotd/legend.html) but couldn't find the above functionality
Legends are inferred from the colour names, i.e.
#!/usr/bin/env dub
/+ dub.sdl:
name "myscript"
dependency "ggplotd" version="~>1.1.3"
+/
import std.algorithm : map;
import std.math : sin, cos;
import std.typecons : tuple;
import ggplotd.stat : statFunction;
import ggplotd.ggplotd : GGPlotD;
import ggplotd.geom : geomLine, geomPoint;
import ggplotd.aes : aes, merge;
import ggplotd.legend : discreteLegend;
void main() {
auto sinF = (double x) {return sin(x);};
auto sinAes = statFunction(sinF, 0.0, 6.3).
map!((a) => a.merge(aes!("colour")("sin(x)")));
auto cosF = (double x) {return cos(x);};
auto cosAes = statFunction(cosF, 0.0, 6.3).
map!((a) => a.merge(aes!("colour")("cos(x)")));
auto gg = GGPlotD();
gg = gg.put(sinAes.geomLine)
.put(cosAes.geomLine)
.put(discreteLegend);
gg.save("legend.png");
}
As a side note, the API is very much inspired by ggplot2, so most things works similar to that.
I'm having trouble reproducing this solution. With ggplotd-1.1.6 (via dub), this reproducibly throws an assertion error:
core.exception.AssertError@../../../.dub/packages/ggplotd-1.1.6/ggplotd/source/ggplotd/guide.d(539): Value not in storeHash
----------------
0x7ff5b023063d onAssertErrorMsg
???:0
0x7ff5b194b238 __lambda5
../../../.dub/packages/ggplotd-1.1.6/ggplotd/source/ggplotd/guide.d:539
0x7ff5b18a2a3e const std.experimental.color.rgb.RGB!("rgba", double, false, 0).RGB ggplotd.guide.GuideToColourFunction.opCall!(immutable(char)[]).opCall(const(immutable(char)[]), bool)
../../../.dub/packages/ggplotd-1.1.6/ggplotd/source/ggplotd/guide.d:400
For me this is still working. Which dlang and dub versions are you using?
% dub --version
DUB version 1.8.0-2, built on Mar 27 2018
% gdc --version
gdc (Ubuntu 8.2.0-1ubuntu2~18.04) 8.2.0
(__VERSION__ for that one is 2068L)
Os is Ubuntu on WSL
Sorry for the delay, but I just tested with the newest dmd: http://downloads.dlang.org/releases/2.x/2.084.0/dmd_2.084.0-0_amd64.deb and it is working fine for me. This strongly suggest it is a gdc bug. You could try removing line 539 in guide.d, but that would probably result in further errors.
I'd suggest installing the newest dmd version from the link above.