How to visualize the results¶
zensvi.visualization: A module to visualize the results
The visualization sub-package, built with Matplotlib, provides four high-level plotting functions to streamline the presentation of SVI analysis for quick initial data exploration. ZenSVI supports histogram generation for discrete data visualization, such as object detection counts, and kernel density estimation plots for continuous variables, such as semantic segmentation ratios. Users can create image grids to display SVI samples, either randomly selected or sorted by specified variables.
For spatial visualization, the package implements three map formats: point maps showing individual SVI locations, line maps using OpenStreetMap street segments, and hexagonal maps using Uber’s H3 grid. In both line and hexagonal maps, SVI values are aggregated to the nearest features, defaulting to point counts when no specific variable is specified.
Preparation¶
# If zen-svi is not installed
%pip install --upgrade zensvi
import zensvi
print(zensvi.__version__)
# Import the required libraries
from zensvi.visualization import plot_map, plot_image, plot_kde, plot_hist
Point map¶
dir_input = "path/to/input/folder" # path where the images are stored
path_output = "./visualization/output.png" # output file path "path/to/output.png"
path_pid = "path/to/pid" # path to the panorama ID file with latitude and longitude "path/to/pid"
csv_file_pattern = "pixel_ratios.csv" # pattern of the CSV files that contain the pixel ratios (or any other variable to plot)
variable = "vegetation" # variable to plot (e.g. vegetation, building, sky, etc.). This should be the column name in the CSV file. If None, count of the number of images is plotted
plot_type = "point" # plot type (either "point", "line", or "hexagon")
fig, ax = plot_map(
path_pid,
dir_input=dir_input,
csv_file_pattern=csv_file_pattern,
variable_name=variable,
plot_type=plot_type,
path_output=path_output,
resolution=13,
cmap="viridis",
legend=True,
title="Point Map",
legend_title="Vegetation",
dark_mode=False,
)
Line map¶
dir_input = "path/to/input"
path_output = "path/to/output.png" # output file path
path_pid = "path/to/pid" # path to the panorama ID file with latitude and longitude
csv_file_pattern = "pixel_ratios.csv" # pattern of the CSV files that contain the pixel ratios (or any other variable to plot)
variable = "vegetation" # variable to plot (e.g. vegetation, building, sky, etc.). This should be the column name in the CSV file. If None, count of the number of images is plotted
plot_type = "line" # plot type (either "point", "line", or "hexagon")
fig, ax = plot_map(
path_pid,
dir_input=dir_input,
csv_file_pattern=csv_file_pattern,
variable_name=variable,
plot_type=plot_type,
path_output=path_output,
resolution=13,
cmap="viridis",
legend=True,
title="Point Map",
legend_title="Vegetation",
dark_mode=False,
)
Hexagon map¶
dir_input = "path/to/input"
path_output = "path/to/output.png" # output file path
path_pid = "path/to/pid" # path to the panorama ID file with latitude and longitude
csv_file_pattern = "pixel_ratios.csv" # pattern of the CSV files that contain the pixel ratios (or any other variable to plot)
variable = "vegetation" # variable to plot (e.g. vegetation, building, sky, etc.). This should be the column name in the CSV file. If None, count of the number of images is plotted
plot_type = "hexagon" # plot type (either "point", "line", or "hexagon")
fig, ax = plot_map(
path_pid,
dir_input=dir_input,
csv_file_pattern=csv_file_pattern,
variable_name=variable,
plot_type=plot_type,
path_output=path_output,
resolution=13,
cmap="viridis",
legend=True,
title="Point Map",
legend_title="Vegetation",
dark_mode=False,
)
Plot images as grid¶
dir_image_input = "path/to/input"
path_output = "path/to/output.png" # output file path
image_file_pattern = "*.png" # pattern of the image files to plot
dir_csv_input = "path/to/csv" # directory of the CSV files
csv_file_pattern = "pixel_ratios.csv" # pattern of the CSV files
sort_by = "random" # sort the images by either "random" or names of variables in the CSV files (e.g. "vegetation")
fig, ax = plot_image(
dir_image_input,
4, # number of rows
5, # number of columns
dir_csv_input=dir_csv_input, # directory of the CSV files
csv_file_pattern=csv_file_pattern, # pattern of the CSV files
sort_by=sort_by, # sort the images by either "random" or names of variables in the CSV files (e.g. "vegetation")
title="Image Grid",
path_output=path_output,
dark_mode=False, # if True, the background is dark
random_seed=123,
)
Plot variables as Kernel Density Estimation¶
path_input = "path/to/input.csv" # input CSV file
columns = ["vegetation", "building", "sky"] # list of columns to plot
path_output = "path/to/output.png" # output file path
kwargs = {
"clip": (0, 1), # clip the values
"palette": "twilight", # color palette. This can be any color palette from the seaborn library or matplotlib library or your own color palette
}
plot_kde(
path_input,
columns,
path_output = path_output,
legend = True,
title = "KDE Plot",
legend_title = "Categories",
dpi = 300,
font_size = 30,
dark_mode = False,
**kwargs,
)
Plot variables as histograms¶
path_input = "path/to/input.csv" # input CSV file
columns = ["truck", "person", "car"] # list of columns to plot
path_output = "path/to/output.png" # output file path
kwargs = {
"clip": (0, 1), # clip the values
"palette": "twilight", # color palette. This can be any color palette from the seaborn library or matplotlib library or your own color palette
}
plot_hist(
path_input,
columns,
path_output = path_output,
legend = True,
title = "Histogram",
legend_title = "Count",
dpi = 300,
font_size = 30,
dark_mode = False,
**kwargs,
)