hvPlot.rgb#
- hvPlot.rgb(x=None, y=None, z=None, bands=None, **kwds)[source]#
RGB plot
rgb can be used to display images that are distributed as three separate “channels” or “bands”.
Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.rgb.html
Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html
- Parameters:
- xstring, optional
The coordinate variable along the x-axis. By default the third coordinate of the dataset.
- ystring, optional
The coordinate variable along the y-axis. By default the second coordinate of the dataset.
- bandsstring, optional
The coordinate variable to draw the RGB channels from. By default the first coordinate of the dataset.
- zstring, optional
The data variable to plot
- **kwdsoptional
Additional keywords arguments are documented in Plotting Options. Run
hvplot.help('rgb')
for the full method documentation.
- Returns:
holoviews.element.RGB
/ Panel objectYou can print the object to study its composition and run:
import holoviews as hv hv.help(the_holoviews_object)
to learn more about its parameters and options.
References
Backend-specific styling options#
alpha, muted, visible
alpha, filterrad, interpolation, visible
Examples#
Basic RGB plot#
This example demonstrates how to create a simple RGB image using synthetic data. Three 2D arrays representing the red, green, and blue color channels are stacked into a 3D xarray.DataArray
and visualized using the rgb
method. This DataArray
is tailored to the default configuration of the rgb
method: band
as the first coordinate, y
the second, and x
the third. Set these keywords appropriately if your data is not structured this way.
import numpy as np
import xarray as xr
import hvplot.xarray
colors = np.array([
[255, 0, 0], # Red
[ 0, 255, 0], # Green
[ 0, 0, 255], # Blue
], dtype=np.uint8)
rgb_array = np.repeat(colors[:, np.newaxis, :], 2, axis=1)
rgb_array = np.transpose(rgb_array, (2, 0, 1))
da = xr.DataArray(
rgb_array,
dims=("band", "y", "x"),
coords={"band": ["R", "G", "B"], "y": np.arange(3), "x": np.arange(2)},
)
print(da.coords)
da.hvplot.rgb(aspect=1)
Coordinates:
* band (band) <U1 12B 'R' 'G' 'B'
* y (y) int64 24B 0 1 2
* x (x) int64 16B 0 1
From a PNG file#
In this example, the dataset is created by loading a PNG file into an xarray.Dataset
object with Pillow. Note that in this case we need to set x
, y
, and band
as the way the data is shaped does not match with hvPlot’s default settings.
Note
Since RGB images are stored starting at the top left pixel, we should start there when plotting to make sure that we get the picture in the correct orientation. This is done automatically when the y-coordinate values are descending. Otherwise, we need to set to flip_yaxis
option to True
to flip the y-axis.
import hvplot.xarray # noqa
ds = hvplot.sampledata.penguins_rgba("xarray")
print(ds)
ds.hvplot.rgb(x="x", y="y", bands="channel", flip_yaxis=True, aspect=1)
<xarray.Dataset> Size: 42kB
Dimensions: (y: 100, x: 100, channel: 4)
Coordinates:
* y (y) int64 800B 0 1 2 3 4 5 6 7 8 9 ... 91 92 93 94 95 96 97 98 99
* x (x) int64 800B 0 1 2 3 4 5 6 7 8 9 ... 91 92 93 94 95 96 97 98 99
* channel (channel) <U1 16B 'R' 'G' 'B' 'A'
Data variables:
rgba (y, x, channel) uint8 40kB 104 157 211 255 104 ... 255 69 66 60 255
From a GeoTIFF file#
The dataset used in the example below is derived from USGS Landsat 7 imagery and is created by loading a GeoTIFF file into an xarray.Dataset
object with rioxarray. Note that we need to set x
and y
as the way the data is shaped does not match with the default’s setting for these two keywords. Also, note that the y
coordinates are descending so we do not need to set flip_yaxis=True
.
import hvplot.xarray # noqa
ds = hvplot.sampledata.landsat_rgb("rioxarray")
print(ds)
ds.hvplot.rgb(x="x", y="y", aspect=1, title="")
<xarray.Dataset> Size: 157kB
Dimensions: (band: 3, x: 237, y: 215)
Coordinates:
* band (band) int64 24B 1 2 3
* x (x) float64 2kB 1.025e+05 1.035e+05 ... 3.378e+05 3.388e+05
* y (y) float64 2kB 2.826e+06 2.825e+06 ... 2.613e+06 2.612e+06
spatial_ref int64 8B 0
Data variables:
rgb (band, y, x) uint8 153kB ...
Since this dataset contains geographic information, we can set the geo
and tiles
options to True
to overlay the image on web map tiles.
import hvplot.xarray # noqa
ds = hvplot.sampledata.landsat_rgb("rioxarray")
print(ds.rio.crs)
ds.hvplot.rgb(x="x", y="y", aspect=1, title="", tiles=True, geo=True, padding=1)
EPSG:32618