Different background color for pos / neg #1973
Unanswered
Hvass-Labs
asked this question in
Q&A
Replies: 1 comment 5 replies
-
One way to do this would be to add rect marks for the positive and negative regions. The rect mark would be defined on y (y1 and y2) but not on x and hence spans the horizontal extent of the chart. The challenge is that there’s no way currently to specify a one-sided interval with the rect mark, so you have to specify the extrema yourself — though you can compute them from the data if you want. Plot.plot({
y: {
grid: true,
tickFormat: "+f",
label: "↑ Surface temperature anomaly (°F)"
},
marks: [
Plot.rectY({length: 1}, {y1: 0, y2: 1.4, fill: "red", fillOpacity: 0.3}),
Plot.rectY({length: 1}, {y1: 0, y2: -1.4, fill: "blue", fillOpacity: 0.2}),
Plot.ruleY([0]),
Plot.dot(gistemp, {x: "Date", y: "Anomaly"}),
]
}) One way to compute the extrema from the data is to use the group transform. Here groupZ is used because you only want one group. Plot.plot({
y: {
grid: true,
tickFormat: "+f",
label: "↑ Surface temperature anomaly (°F)"
},
marks: [
Plot.rectY(gistemp, Plot.groupZ({y2: "min"}, {y: "Anomaly", y1: 0, fill: "blue", fillOpacity: 0.2})),
Plot.rectY(gistemp, Plot.groupZ({y2: "max"}, {y: "Anomaly", y1: 0, fill: "red", fillOpacity: 0.2})),
Plot.ruleY([0]),
Plot.dot(gistemp, {x: "Date", y: "Anomaly"}),
]
}) Lastly, another option would be to write a custom mark. Plot.plot({
y: {
grid: true,
tickFormat: "+f",
label: "↑ Surface temperature anomaly (°F)"
},
marks: [
(index, scales, values, dimensions) => {
return svg`<g fill-opacity="0.2">
<rect x="0" y="0" fill="red" height=${scales.y(0) - 0} width=${dimensions.width}></rect>
<rect x="0" y=${scales.y(0)} fill="blue" height=${dimensions.height - scales.y(0)} width=${dimensions.width}></rect>
</g>`;
},
Plot.ruleY([0]),
Plot.dot(gistemp, {x: "Date", y: "Anomaly"}),
]
}) |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to use different background colors for positive and negative regions?
This probably violates all kinds of "International Plotting Treaties & Conventions" but I like it because it makes it very easy to see which data is pos / neg.
The way I do this in Matplotlib for Python, is that I first add all the objects of the plot, then I ask for the min/max axis values, and if the y-axis contains positive values then I call a function called
fill_between
with the relevant min/max axis values and the color green, and likewise for negative values and the color red. I give these a low z-order to push them into the background even though they were added last to the plot.Furthermore, the example below draws something called a "boxen-plot" or "letter-value plot" which shows more details than a box-plot, but less details than a violin-plot. I like it and you don't seem to have it, so you may consider adding that in the future. This is made using Seaborn which is an extension for Matplotlib [Ref 1] [Ref 2]
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions