vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.24k stars 786 forks source link

feature request for image show #3595

Closed YongcaiHuang closed 1 day ago

YongcaiHuang commented 1 day ago

What is your suggestion?

feature goal

to request the image show function, like matplotlib.pyplot.imshow

motivating

interaction is the advantages of altair. in image processing and even deep learning image cases, exploration of image is necessary. on the other hands, matplotlib requires more code and final effect is not that good.

expected behavior

below is the example

  1. show the image pixel position and value
  2. interaction with widget, like slider
import matplotlib.pyplot as plt
from matplotlib import widgets
from skimage import data, color

ast_rgb = data.astronaut()
ast_hsv = color.rgb2hsv(ast_rgb)
ast_hsv = (ast_hsv * 255).astype('uint8')

fig = plt.figure()
axs = fig.subplots(4, 1, gridspec_kw={'height_ratios': [5, 5, 1, 1]})
img_origin_ax: plt.Axes = axs[0]
img_one_channel_ax: plt.Axes = axs[1]
threshold_slider_ax: plt.Axes = axs[2]
channel_slider_ax: plt.Axes = axs[3]

ori_img = img_origin_ax.imshow(ast_rgb)
img = img_one_channel_ax.imshow(ast_hsv[..., 0], cmap='gray')
img_origin_ax.axis('off')
img_one_channel_ax.axis('off')
threshold_slider = widgets.Slider(threshold_slider_ax, 'Threshold', 0, 255, valinit=128, valstep=1)
channel_slider = widgets.Slider(channel_slider_ax, 'Channel', 0, 2, valinit=0, valstep=1)

def update(val):
    threshold = threshold_slider.val
    channel = int(channel_slider.val)
    img_one_channel_ax.imshow(ast_hsv[..., channel] > threshold, cmap='gray')
    fig.canvas.draw_idle()

threshold_slider.on_changed(update)
channel_slider.on_changed(update)

plt.show()

https://github.com/user-attachments/assets/8a6fd90b-b283-4674-b486-7a7ded8defc7

Have you considered any alternative solutions?

No response

joelostblom commented 1 day ago

Thank you for the feature suggestion. I agree that supporting images would be an important improvement, and it is something that needs to be added in VegaLite before it can be added in Altair. I will close this as a duplicate and refer to the following issue to join the discussion of the implementation of image support in Vega-Lite https://github.com/vega/vega-lite/issues/9389

You might also find the discussions in these issues interesting: