Repository navigation
Open
Labels
Description
When creating a scatter plot with colors including np.nan the figure shows fine.
import json
import numpy as np
import plotly.graph_objects as go
go.Figure(go.Scatter(x=[0, 0], y=[0, 1], marker={"color": [0, np.nan]}))However, if I convert the figure to JSON and load it back, there will be an error.
fig = go.Figure(go.Scatter(x=[0, 0], y=[0, 1], marker={"color": [0, np.nan]}))
go.Figure(**json.loads(fig.to_json()))Error message:
ValueError:
Invalid element(s) received for the 'color' property of scatter.marker
Invalid elements include: [None]
The 'color' property is a color and may be specified as:
- A hex string (e.g. '#ff0000')
- An rgb/rgba string (e.g. 'rgb(255,0,0)')
- An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
- An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
- A named CSS color:
aliceblue, antiquewhite, aqua, aquamarine, azure,
beige, bisque, black, blanchedalmond, blue,
blueviolet, brown, burlywood, cadetblue,
chartreuse, chocolate, coral, cornflowerblue,
cornsilk, crimson, cyan, darkblue, darkcyan,
darkgoldenrod, darkgray, darkgrey, darkgreen,
darkkhaki, darkmagenta, darkolivegreen, darkorange,
darkorchid, darkred, darksalmon, darkseagreen,
darkslateblue, darkslategray, darkslategrey,
darkturquoise, darkviolet, deeppink, deepskyblue,
dimgray, dimgrey, dodgerblue, firebrick,
floralwhite, forestgreen, fuchsia, gainsboro,
ghostwhite, gold, goldenrod, gray, grey, green,
greenyellow, honeydew, hotpink, indianred, indigo,
ivory, khaki, lavender, lavenderblush, lawngreen,
lemonchiffon, lightblue, lightcoral, lightcyan,
lightgoldenrodyellow, lightgray, lightgrey,
lightgreen, lightpink, lightsalmon, lightseagreen,
lightskyblue, lightslategray, lightslategrey,
lightsteelblue, lightyellow, lime, limegreen,
linen, magenta, maroon, mediumaquamarine,
mediumblue, mediumorchid, mediumpurple,
mediumseagreen, mediumslateblue, mediumspringgreen,
mediumturquoise, mediumvioletred, midnightblue,
mintcream, mistyrose, moccasin, navajowhite, navy,
oldlace, olive, olivedrab, orange, orangered,
orchid, palegoldenrod, palegreen, paleturquoise,
palevioletred, papayawhip, peachpuff, peru, pink,
plum, powderblue, purple, red, rosybrown,
royalblue, rebeccapurple, saddlebrown, salmon,
sandybrown, seagreen, seashell, sienna, silver,
skyblue, slateblue, slategray, slategrey, snow,
springgreen, steelblue, tan, teal, thistle, tomato,
turquoise, violet, wheat, white, whitesmoke,
yellow, yellowgreen
- A number that will be interpreted as a color
according to scatter.marker.colorscale
- A list or array of any of the above
Interestingly, the to_plotly_json method works fine:
fig = go.Figure(go.Scatter(x=[0, 0], y=[0, 1], marker={"color": [0, np.nan]}))
go.Figure(**json.loads(json.dumps(fig.to_plotly_json())))Questions: Is this the expected behavior for the to_json method, and why it behaves differently from the to_plotly_json method?
Activity
alexcjohnson commentedon Jul 13, 2023
Oh that's an interesting one. If you just
json.dumps([0, np.nan])you get'[0, NaN]'which is invalid JSON: in a browserJSON.parse('[0, NaN]')throws an error. The actual stringification we do fixes this incorrect behavior of the basejsonpackage and gives[0,null], which is valid JSON and is handled correctly by plotly.js (generally anullwill just be ignored and we'll do whatever the default would have been). But when we bring'null'back into Python we don't know that it came fromnp.nan, it could have also come fromNoneso that's what we turn it into.The problem is that
np.nanis a number (ironic, given that NaN stands for not a number, buttype(np.nan) == float) whereasNoneis not.Anyone see a problem changing the number validator to always accept
None? That's the only way I see to get around this.