#5261·ScottPlot

Plot.Render(SKCanvas, PixelRect) clears the entire canvas instead of only the given rectangle

Author: pbandykCreated Jul 30, 2026Updated Jul 30, 2026

Summary

Plot.Render(SKCanvas canvas, PixelRect rect) is documented as "Render onto an existing canvas inside the given rectangle." In practice it clears the entire canvas before drawing — not just rect — so any pixels previously drawn outside rect (including content composited from another plot) are wiped to transparent.

This makes it impossible to composite several plots into different sub-rectangles of one canvas via this overload: each Render call erases the ones before it, and only the last plot's rectangle survives. Everything else ends up transparent (which reads as white/blank when the PNG is viewed on an opaque background).

The clear is unconditional with respect to background opacity — it happens even when FigureBackground.Color is fully opaque (A = 255), which is the default.

Repro

csharp
using ScottPlot;
using SkiaSharp;

// Two ordinary plots, to be composited into the top and bottom halves of one canvas.
var top = new Plot();
top.Add.Signal(new double[] { 1, 4, 2, 5, 3 });

var bottom = new Plot();
bottom.Add.Signal(new double[] { 5, 2, 4, 1, 3 });

int w = 400, h = 400;
using var surface = SKSurface.Create(new SKImageInfo(w, h));
surface.Canvas.Clear(SKColors.White);

// PixelRect is (left, right, bottom, top).
top.Render(surface.Canvas, new PixelRect(0, w, h / 2, 0));     // top half
bottom.Render(surface.Canvas, new PixelRect(0, w, h, h / 2));  // bottom half

using var image = surface.Snapshot();
File.WriteAllBytes("composited.png", image.Encode(SKEncodedImageFormat.Png, 100).ToArray());

Expected

  • Top half shows the first plot, bottom half shows the second.

Actual

  • Only the bottom (last-rendered) plot is visible. The top half is blank — bottom.Render(...) cleared the whole canvas, wiping the first plot and the initial Clear(White) fill to transparent.

To show the erase is unconditional (not an alpha/blend artifact), give each plot an opaque background and sample a pixel in the top half after both renders:

csharp
var top = new Plot();    top.FigureBackground.Color = Colors.Yellow;  // opaque, A=255
var bottom = new Plot(); bottom.FigureBackground.Color = Colors.Blue; // opaque, A=255

// ... render top into the top half, then bottom into the bottom half ...

using var bmp = SKBitmap.FromImage(image);
// A point in the TOP half (the figure margin of the first plot):
var px = bmp.GetPixel(4, 4);   // => #00000000  (fully transparent — the yellow plot was erased)

Observed: the top-half pixel is #00000000 even though the top plot's figure background is opaque yellow. The bottom-half pixel is the expected opaque blue. So the second Render cleared the whole canvas, then filled only its own rectangle.

Likely cause

Plot.Render(SKCanvas, PixelRect) delegates to RenderManager.Render(canvas, rect) → RenderOnce(...), which issues a canvas.Clear() over the whole canvas with no ClipRect to rect first. In the normal single-plot, full-canvas case the subsequent figure-background fill repaints the whole canvas, so the clear is invisible; it only becomes observable when rect is a sub-region of the canvas.

Expected fix (either is fine)

  • Clip the clear to rect (e.g. canvas.Save(); canvas.ClipRect(rect); ... ; canvas.Restore()), so the overload honors its documented "inside the given rectangle" contract; or
  • Update the doc to state that this overload takes ownership of the whole canvas and cannot be used to composite multiple plots.

Workaround (in case anyone else hits this)

Render each plot to its own surface sized to its target rectangle, then composite the snapshots onto the master canvas — Plot.Render(SKSurface) renders into the surface's own bounds, so the whole-canvas clear is harmless:

csharp
foreach (var (plot, rect) in placements)
{
    using var band = SKSurface.Create(new SKImageInfo(rect.Width, rect.Height));
    plot.Render(band);
    using var snap = band.Snapshot();
    masterCanvas.DrawImage(snap, rect.Left, rect.Top);
}

For simple grid/stacked layouts, ScottPlot.Multiplot composes multiple plots into one image correctly and is the intended high-level API; the manual approach above is only needed when you want explicit control over each plot's rectangle.

Environment

  • ScottPlot 5.1.58
  • .NET 8
  • Windows 11