Note
Go to the end to download the full example code.
From numerical values to discrete colors¶
The ptvis.color.DiscreteColorConversion class defines a conversion
from numerical values to discrete colors. Values in an interval is converted
into the same color.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(),
)
plotly.io.show(fig)
Defining a conversion with colors for evenly spaced levels¶
Discrete colors are determined by discretizing a color scale. The colors argument determines colors at evenly spaced levels of the color scale.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
colors=["blue", "green", "red"],
),
)
plotly.io.show(fig)
Defining a conversion with colors at arbitrary levels¶
The levels of the colors argument can be changed by the levels argument. The arugment must be given in ascending order.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
colors=["blue", "green", "red"],
levels=[-1, -0.5, 1],
),
)
plotly.io.show(fig)
Number of intervals¶
The number of intervals or discrete colors can be changed by the n_intervals arguments.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(n_intervals=5),
)
plotly.io.show(fig)
Clipping¶
By default, the minimum and maximum values correspond with the lowest and highest levels. Values corresponding with the lowest and highest levels can be changed by the value_min and value_max arguments. Values out of range are converted into colors of the lowest and highest levels.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
colors=["blue", "green", "red"],
value_min=30,
value_max=60,
),
)
plotly.io.show(fig)
Colors for out-of-range values can be changed by the lower_color and higher_color argument.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = list(range(len(elements)))
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
colors=["blue", "green", "red"],
value_min=30,
value_max=60,
lower_color="black",
higher_color="white",
),
)
plotly.io.show(fig)
Closed endpoint of an interval¶
A closed endpoint of an interval can be changed by the closed argument.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = [i % 3 for i in range(len(elements))]
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
n_intervals=2,
closed="high",
),
)
plotly.io.show(fig)
The include_boundary argument controls whether the higher (lower)
endpoint of the highest (lowest) interval is closed when the closed
argument is "low" ("high").
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = [i % 3 for i in range(len(elements))]
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
n_intervals=2,
closed="high",
include_boundary=False,
lower_color="white",
),
)
plotly.io.show(fig)
Color for NaN¶
A color for NaN can be changed by the na_color argument.
import plotly
import ptvis.color
elements = list(ptvis.Element)
values = [i if i % 10 else float("nan") for i in range(len(elements))]
fig = plotly.graph_objects.Figure()
ptvis.attach_plain_cells(
fig,
elements,
colors=values,
color_conversion=ptvis.color.DiscreteColorConversion(
colors=["blue", "green", "red"],
na_color="white",
),
)
plotly.io.show(fig)