Plotly Chart Quality Guidelines: Difference between revisions

From Tygron Support wiki
Jump to navigation Jump to search
Created page with "This page contains tips on adding a Plotly Chart to a Text Panel content successfully. ==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 dat..."
 
No edit summary
Line 1: Line 1:
This page contains tips on adding a [[Plotly]] Chart to a [[Text Panel]] content successfully.
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:
<syntaxhighlight copy>
Plotly.newPlot(divId, traces, layout, config);
</syntaxhighlight>
* 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==
==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 [[Neighborhood]]s or [[Area]]s, where values often fall within a tight range.
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===
===When to avoid a zero baseline===
Line 22: Line 38:
===Data-driven range===
===Data-driven range===


Compute the range from the data. In JavaScript:
First compute the bounds from your data:


<syntaxhighlight copy>
<syntaxhighlight copy>
// helper – run once before building the figure
const minValue = Math.min.apply(null, values);
const minValue = Math.min.apply(null, values);
const maxValue = Math.max.apply(null, values);
const maxValue = Math.max.apply(null, values);
Line 34: Line 51:
</syntaxhighlight>
</syntaxhighlight>


Then apply:
Then set the range on the layout object:


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


===Choose dtick sensibly===
===Choose dtick sensibly===


Choose a `dtick` that produces a readable number of ticks:
dtick controls the step between axis ticks. Set it alongside range on the layout object:


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


Too many ticks clutter the chart. Too few ticks hide structure.
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===
===Disclose truncation===


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


<syntaxhighlight copy>
<syntaxhighlight copy>
<p class="footnote">
<p class="footnote">
  The y-axis is zoomed to emphasize differences between categories.
The y-axis is zoomed to emphasise differences between categories.
</p>
</p>
</syntaxhighlight>
</syntaxhighlight>
Line 69: Line 92:
===Avoid excessively wide ranges===
===Avoid excessively wide ranges===


An axis range much wider than the data spread wastes space and reduces comparability.
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.


If the data spread is small, do not use a range that is many times larger than the spread unless there is a strong analytical reason.
===Value labels===


===Value labels===
When precise comparison matters, display values above the bars. These properties belong to the trace object:
 
<syntaxhighlight copy>
/ 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
}
</syntaxhighlight>


When precise comparison matters, display values on or above the bars:
Ensure the layout top margin is large enough to show outside labels:


<syntaxhighlight copy>
<syntaxhighlight copy>
text: values.map(v => v.toFixed(1) + "°C"),
Plotly.newPlot('chart', traces, {
textposition: "outside"
/ layout /
margin: { t: 60 } // ← layout.margin.t
});
</syntaxhighlight>
</syntaxhighlight>


Ensure the top margin is large enough to show outside labels. See [[Text Panel Content Guidelines#Encoding_Special_Characters_in_JavaScript_Strings]] for details on encoding special characters in JavaScript strings.
See Text Panel Content Guidelines#Encoding_Special_Characters_in_JavaScript_Strings for details on encoding special characters in JavaScript strings.


----
----
Line 92: Line 128:
===Increase bottom margin===
===Increase bottom margin===


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


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


Line 104: Line 141:
===Use automargin===
===Use automargin===


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


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


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


===Control tick angle===
===Control tick angle===


Use a moderate angle:
Set a moderate rotation angle on the layout object:


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


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


===Reduce font size cautiously===
===Reduce font size cautiously===


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


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


Line 142: Line 188:
===Use horizontal bars===
===Use horizontal bars===


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


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


This moves category labels to the y-axis, where they often have more room.
When switching to horizontal bars, adjust margin settings accordingly (left margin instead of bottom for rotated labels).


===Test with the longest label===
===Test with the longest label===
Line 163: Line 210:
==Coloring Bar Charts by Value==
==Coloring Bar Charts by Value==


A colorscale only affects a Plotly bar trace when the trace has a color array.
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===
===Required pattern===


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


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


Without it, `colorscale`, `cmin`, and `cmax` may have no visible effect, and the bars may render in the default color.
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===
===Continuous data needs continuous encoding===


For continuous quantitative data, use a continuous colorscale.
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.
 
Do not use a fixed positional palette where the first bar is always one color, the second bar another color, and so on, unless the position itself is meaningful.
 
Positional coloring can be misleading when bars are sorted by value.


===Add a colorbar===
===Add a colorbar===


When using a continuous colorscale, show 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:


<syntaxhighlight copy>
<syntaxhighlight copy>
showscale: true,
Plotly.newPlot('chart', [trace], {
colorbar: {
/ layout /
  title: { text: "Metric (unit)" },
margin: { r: 70 }
  thickness: 14,
});
  len: 0.8
}
</syntaxhighlight>
 
Reserve right margin for the colorbar:
 
<syntaxhighlight copy>
margin: {
  r: 70
}
</syntaxhighlight>
</syntaxhighlight>


===Categorical coloring===
===Categorical coloring===


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


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


Make sure the legend explains what each color means.
Make sure the legend entries explain what each colour means.


===Verify the rendered gradient===
===Verify the rendered gradient===


Source code can look correct while the rendered chart still uses default colors. Always verify the final image. See [[Text Panel Content Guidelines#Quality_assurance]] for the full rendered-image checklist.
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.


----
----
Line 242: Line 281:
==Choosing Color Domain Bounds==
==Choosing Color Domain Bounds==


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


===The minimum-value problem===
===The minimum-value problem===


Sequential scales such as `Reds` often start with a very light or near-white color. If `cmin` is set exactly to the data minimum, the minimum value maps to the lightest end of the scale and may appear gray or white.
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.
 
This can make the lowest-value category look like missing data.


===Pad the lower bound===
===Pad the lower bound===


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


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


const colorMin = minValue - colorPadding;
var colorMin = minValue - colorPadding;
const colorMax = maxValue + colorPadding;
var colorMax = maxValue + colorPadding;
</syntaxhighlight>


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


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


===Dynamic vs fixed bounds===
===Dynamic vs fixed bounds===
Line 283: Line 322:


<syntaxhighlight copy>
<syntaxhighlight copy>
// trace.marker
cmin: minValue - padding,
cmin: minValue - padding,
cmax: maxValue + padding
cmax: maxValue + padding
</syntaxhighlight>
</syntaxhighlight>


Use fixed bounds only when:
Use fixed (hard-coded) bounds only when:


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


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


===Verify extremes===
===Verify extremes===


Always check that:
Always check the rendered output to confirm that:


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


{{article end
{{article end

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.