Plotly Chart Quality Guidelines: Difference between revisions

From Tygron Support wiki
Jump to navigation Jump to search
No edit summary
Line 350: Line 350:
* [[JavaScript]]
* [[JavaScript]]
* [[Panel]]
* [[Panel]]
* [[How to insert a Plotly Graph]]
|howtos=
|howtos=
* [[How to insert a Plotly Graph]]
* [[How to insert a Plotly Graph]]
* [[How to plot neighborhood demographics in a pie chart]]
* [[How to plot neighborhood demographics in a pie chart]]
}}
}}

Revision as of 10:10, 1 December 2026

This page contains tips on adding a Plotly Chart to a Text Panel content successfully.

Figure Anatomy

Every Plotly figure is built from up to four arguments passed to Plotly.newPlot:

Plotly.newPlot(divId, traces, layout, config);
  • traces – an array of trace objects. Each trace describes one dataset (a set of bars, a line, etc.). Properties such as type, x, y, marker, text, textposition, and showlegend belong here.
  • layout – a single object controlling axes, margins, titles, annotations, and global appearance. Properties such as xaxis, yaxis, margin, title, barmode, and paper_bgcolor belong here.

config (optional)* – controls interactivity: which toolbar buttons appear, drag mode, static vs. interactive rendering, etc.

All code snippets below indicate in a comment whether they target a trace or the layout. See How to insert a Plotly Graph for the full setup including CSS sizing and the staticPlot flag.


Axis Scaling for Narrow Data Bands

A common defect is using a zero baseline or an overly wide axis when the data occupies a narrow band. This is especially relevant when comparing Grid Overlay values across Neighborhoods or Areas, where values often fall within a tight range.

When to avoid a zero baseline

Avoid forcing the axis to zero when:

  • the data range is narrow,
  • zero is not a meaningful reference,
  • the purpose is to compare small differences between categories.

Example situation:

  • all values are within a few units of each other,
  • a zero baseline makes all bars look almost identical.

In that case, use a data-driven range.

Data-driven range

First compute the bounds from your data:

// helper – run once before building the figure
const minValue = Math.min.apply(null, values);
const maxValue = Math.max.apply(null, values);
const span = maxValue - minValue;
const padding = Math.max(span * 0.15, 0.5);

const yMin = minValue - padding;
const yMax = maxValue + padding;

Then set the range on the layout object:

Plotly.newPlot('chart', traces, {
/ layout /
yaxis: {
range: [yMin, yMax] // ← goes on layout.yaxis
}
});

Choose dtick sensibly

dtick controls the step between axis ticks. Set it alongside range on the layout object:

Plotly.newPlot('chart', traces, {
/ layout /
yaxis: {
range: [yMin, yMax],
dtick: 1 // ← layout.yaxis.dtick
}
});

Too many ticks clutter the chart; too few hide structure. Pick a dtick that yields roughly 4–10 ticks over the visible range.

Disclose truncation

If the axis does not start at zero, add a footnote in the surrounding panel HTML:

<p class="footnote">
The y-axis is zoomed to emphasise differences between categories.
</p>

This prevents readers from misinterpreting bar lengths.

Avoid excessively wide ranges

An axis range much wider than the data spread wastes space and reduces comparability. If the data spread is small, do not use a range many times larger than the spread unless there is a strong analytical reason.

Value labels

When precise comparison matters, display values above the bars. These properties belong to the trace object:

/ trace (one element of the traces array) /
{
type: "bar",
x: names,
y: values,
text: values.map(function(v){ return v.toFixed(1) + "°C"; }),
textposition: "outside" // ← trace.textposition
}

Ensure the layout top margin is large enough to show outside labels:

Plotly.newPlot('chart', traces, {
/ layout /
margin: { t: 60 } // ← layout.margin.t
});

See Text Panel Content Guidelines#Encoding_Special_Characters_in_JavaScript_Strings for details on encoding special characters in JavaScript strings.


Preventing Clipped Category Labels

Clipped x-axis labels are a common rendering defect, especially when category names are long.

Increase bottom margin

Rotated labels need vertical space. Set the bottom margin on the layout object:

Plotly.newPlot('chart', traces, {
/ layout /
margin: { b: 140 } // ← layout.margin.b
});

If labels are still clipped, increase the bottom margin further.

Use automargin

Enable automatic margin adjustment on the layout object, where supported:

Plotly.newPlot('chart', traces, {
/ layout /
xaxis: {
automargin: true // ← layout.xaxis.automargin
}
});

This helps but is not a substitute for verifying the rendered image.

Control tick angle

Set a moderate rotation angle on the layout object:

Plotly.newPlot('chart', traces, {
/ layout /
xaxis: {
tickangle: -45 // ← layout.xaxis.tickangle
}
});

Very steep angles (e.g. −90) can require excessive bottom margin.

Reduce font size cautiously

Reducing font size can help, but only if labels remain readable. Set the tick font on the layout object:

Plotly.newPlot('chart', traces, {
/ layout /
xaxis: {
tickfont: { size: 11 } // ← layout.xaxis.tickfont.size
}
});

Abbreviate long names

If full names are impractical, use controlled abbreviations and explain them in a footnote or tooltip.

Use horizontal bars

Horizontal bar charts give long category names more room because the labels move to the y-axis. Change the orientation on the trace object:

/ trace /
{
type: "bar",
x: values, // note: x/y swapped for horizontal bars
y: names,
orientation: "h" // ← trace.orientation
}

When switching to horizontal bars, adjust margin settings accordingly (left margin instead of bottom for rotated labels).

Test with the longest label

During development, test the layout using the longest expected category name, not only the current sample data.


Coloring Bar Charts by Value

A colorscale only affects a Plotly bar trace when the trace carries its own colour array. All of the properties below (color, colorscale, cmin, cmax, showscale, colorbar) live inside marker on the trace object.

Required pattern

/ trace (one element of the traces array) /
var trace = {
type: "bar",
x: names,
y: values,
marker: {
color: values, // ← trace.marker.color (critical!)
colorscale: "Reds", // ← trace.marker.colorscale
cmin: colorMin, // ← trace.marker.cmin
cmax: colorMax, // ← trace.marker.cmax
showscale: true, // ← trace.marker.showscale
colorbar: { // ← trace.marker.colorbar
title: { text: "PET (°C)" },
thickness: 14,
len: 0.8
}
}
};

Plotly.newPlot('chart', [trace], {
/ layout – reserve right margin for the colorbar /
margin: { r: 70 } // ← layout.margin.r
});

The critical property is marker.color. Without it, colorscale, cmin, and cmax have no visible effect and the bars render in the default colour.

Continuous data needs continuous encoding

For continuous quantitative data, use a continuous colorscale. Do not assign a fixed positional palette (first bar always red, second always blue, …) unless position itself is meaningful. Positional coloring becomes misleading when bars are sorted by value.

Add a colorbar

The colorbar is declared inside trace.marker (see the block above). Reserve right margin on the layout object so it does not overlap the bars:

Plotly.newPlot('chart', [trace], {
/ layout /
margin: { r: 70 }
});

Categorical coloring

If you are using discrete categories rather than a continuous scale, enable a legend on the trace object:

/ trace /
{
type: "bar",
x: names,
y: values,
showlegend: true // ← trace.showlegend
}

Make sure the legend entries explain what each colour means.

Verify the rendered gradient

Source code can look correct while the rendered chart still uses default colours. Always inspect the final image. See Text Panel Content Guidelines#Quality_assurance for the full rendered-image checklist.


Choosing Color Domain Bounds

Even when marker.color is present, the colour domain can still be misleading.

The minimum-value problem

Sequential scales such as "Reds" start with a very light, near-white colour. If cmin equals the data minimum exactly, the lowest value maps to that near-white end and can look like missing data.

Pad the lower bound

Compute padded bounds, then pass them to the trace's marker:

// helpers
var minValue = Math.min.apply(null, values);
var maxValue = Math.max.apply(null, values);
var span = maxValue - minValue;
var colorPadding = Math.max(span * 0.15, 0.5);

var colorMin = minValue - colorPadding;
var colorMax = maxValue + colorPadding;

/ trace /
var trace = {
type: "bar",
x: names,
y: values,
marker: {
color: values, // ← trace.marker.color
colorscale: "Reds", // ← trace.marker.colorscale
cmin: colorMin, // ← trace.marker.cmin
cmax: colorMax // ← trace.marker.cmax
}
};

Padding keeps the minimum value visibly coloured rather than blending into the background.

Dynamic vs fixed bounds

Prefer dynamic bounds with padding:

// trace.marker
cmin: minValue - padding,
cmax: maxValue + padding

Use fixed (hard-coded) bounds only when:

  • the data domain is stable across all Scenarios, Iterations, and timeframes,
  • the fixed bounds are documented,
  • the fixed bounds are verified to keep every value visible.

Hard-coded bounds become stale when scenarios, iterations, or timeframes change.

Verify extremes

Always check the rendered output to confirm that:

  • the minimum value is not near-white,
  • the maximum value is not saturated into an unreadable block,
  • intermediate values are visually distinguishable,
  • the colorbar gradient matches the actual bar colours.