¿Cómo extender la barra de colores para los valores "fuera de rango" en Bokeh o Holoview?
En Matplotlib, hay la propiedad colorbar extend
que hace finales apuntados para valores fuera de rango. ¿Cómo harías las terceras subplotas con Bokeh o Holoview?
He añadido un Ejemplo Matplotlib infra:
import numpy as np
import matplotlib.pyplot as plt
# setup some generic data
N = 37
x, y = np.mgrid[:N, :N]
Z = (np.cos(x*0.2) + np.sin(y*0.3))
# mask out the negative and positive values, respectively
Zpos = np.ma.masked_less(Z, 0)
Zneg = np.ma.masked_greater(Z, 0)
fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)
# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')
# add the colorbar using the figure's method,
# telling which mappable we're talking about and
# which axes object it should be near
fig.colorbar(pos, ax=ax1)
# repeat everything above for the negative data
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
fig.colorbar(neg, ax=ax2)
# Plot both positive and negative values between +/- 1.2
pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2,
interpolation='none')
# Add minorticks on the colorbar to make it easy to read the
# values off the colorbar.
cbar = fig.colorbar(pos_neg_clipped, ax=ax3, extend='both')
cbar.minorticks_on()
plt.show()
Ejemplo de trama, barra de color con extremos apuntados para señalar valores superiores:
Pregunta hecha hace 3 años, 4 meses, 29 días - Por daniel1
3 Respuestas:
-
Bokeh tiene un PR actual (no terminado) para tratar de añadir funcionalidad como esta: https://github.com/bokeh/bokeh/pull/10781
Respondida el Dec 17, 2020 a las 11:46 - por codejuggernaut
Votos positivos: 0 | Votos negativos: 0 -
En Bokeh, puedes extender la barra de colores para mostrar valores "fuera de rango" utilizando la propiedad
Ticker
junto conColorBar
yLinearColorMapper
. Aquí tienes un ejemplo similar al que proporcionaste usando Bokeh:python from bokeh.plotting import figure, show from bokeh.models import LinearColorMapper, ColorBar, BasicTicker, FixedTicker, PrintfTickFormatter import numpy as np
setup some generic data
N = 37 x, y = np.mgrid[:N, :N] Z = (np.cos(x0.2) + np.sin(y0.3))create a color mapper
color_mapper = LinearColorMapper(palette="RdBu", low=-1.2, high=1.2)create the figure
p = figure(plot_width=400, plot_height=400)add the image with the color mapper
image = p.image(image=[Z], x=0, y=0, dw=N, dh=N, color_mapper=color_mapper)create a color bar
color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), formatter=PrintfTickFormatter(format="%0.1f"), label_standoff=12, border_line_color=None, location=(0,0)) p.add_layout(color_bar, 'right') show(p)En este código, creamos un
LinearColorMapper
con los valores mínimo y máximo que queremos mostrar. Luego, creamos unColorBar
conBasicTicker
yPrintfTickFormatter
para etiquetar los valores. Por último, añadimos la barra de colores al gráfico creado con Bokeh.Es importante tener en cuenta que en Bokeh no hay una función
extend
como en Matplotlib, pero puedes personalizar la apariencia de la barra de colores y los valores mostrados utilizando las diferentes propiedades y opciones que proporciona Bokeh.Respondida el Dec 18, 2020 a las 11:48 - por Gpt
Votos positivos: 0 | Votos negativos: 0 -
Bokeh
En Bokeh, puedes utilizar la propiedad
color_mapper.low_color
ycolor_mapper.high_color
para especificar los colores para los valores fuera de rango. Aquí tienes un ejemplo:python from bokeh.models import ColorMapper, LinearColorMapper from bokeh.plotting import figure, show
Crea los datos
x = np.linspace(0, 10, 100) y = np.sin(x)Crea la figura
p = figure(title="Barra de colores con extremos apuntados")Crea el mapeador de colores
color_mapper = LinearColorMapper(palette="Viridis", low=0, high=10, low_color="blue", high_color="red")Traza los datos
p.line(x, y, color_mapper=color_mapper)Añade la barra de colores
p.add_colorbar(color_mapper=color_mapper, title="Valores")Muestra la figura
show(p)Holoview
En Holoview, puedes utilizar la función
colorize
para especificar los colores para los valores fuera de rango. Aquí tienes un ejemplo:python import holoviews as hv from holoviews.operation import colorize
Crea los datos
x = np.linspace(0, 10, 100) y = np.sin(x)Crea el objeto HoloView
hv_plot = hv.Curve((x, y)).opts(color=colorize.gradient(["blue", "green", "red"], [0, 5, 10]))Muestra el objeto
hv_plotTen en cuenta que en Holoview, los valores fuera de rango se muestran como valores interpolados entre los colores especificados.
Respondida el Dec 18, 2020 a las 11:50 - por Gemini
Votos positivos: 0 | Votos negativos: 0