-
Hi, As a newbie I understand this is probably a trivial question but I cannot find a worked example of setting colours for multiple series of data. This is my existing code
which plots out nicely. There will be about 10 series and I would like to use one of the many standard schemes I found in the docs here I have tried many permutations but none of them stop the lines coming out all black. One I tried was this:
but this raises an exception:
Many thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For multiple lines, the recommended pattern is to use tidy data where you have a single dataset that represents all of your series, together. For example, you might have a CSV file like this:
You can create a fake dataset like so: data = ["apples", "oranges", "bananas"].flatMap(category => {
let value = 0;
return d3.utcDays(new Date("2021-01-01"), new Date("2022-01-01")).map(date => {
value += Math.round(Math.random() * 100 - 50); // random walk
return {category, date, value};
});
}) You can then render this like so: Plot.plot({
marks: [
Plot.ruleY([0]),
Plot.line(data, {x: "date", y: "value", stroke: "category"})
]
}) If you want to specify a custom scheme, you can set the scheme option on the color scale. The scheme option refers to the name of a scheme; use the range option if you want to specify an array of values. Either Plot.plot({
color: {
scheme: "category10"
},
marks: [
Plot.ruleY([0]),
Plot.line(data, {x: "date", y: "value", stroke: "category"})
]
}) If you have the data as separate series without a category field and want to render them as individual marks, you could also do something like this: Plot.plot({
marks: [
Plot.ruleY([0]),
Plot.line(apples, {x: "date", y: "value", stroke: "red"}),
Plot.line(oranges, {x: "date", y: "value", stroke: "orange"}),
Plot.line(bananas, {x: "date", y: "value", stroke: "yellow"})
]
}) But generally it is preferred that the nominal/categorical field is represented in your data rather than assigning colors manually. Live notebook: https://observablehq.com/d/5c8c14e276cc6a5a |
Beta Was this translation helpful? Give feedback.
For multiple lines, the recommended pattern is to use tidy data where you have a single dataset that represents all of your series, together. For example, you might have a CSV file like this:
You can create a fake dataset like so:
You can then render this like so: