Interactive Plot Customizations

This notebook will walk you through some of the customization options that are available for interactive plots in DataMapPlot. There are too many options available to fully cover all that can be done, so this notebook will instead highlight some of the major options and give some ideas for how some of the more flexible options might be used to create extra interactive features for your plot. To get started we’ll need to import DataMapPlot.

[1]:
import datamapplot

To demonstrate what DataMapPlot can do we’ll need some data. The examples directory of the DataMapPlot repository contains some pre-prepared datasets for experimenting with. We’ll grab one of those. In practice we need a data map – as set of 2d coordinates, one per data sample we are mapping – and multiple layers of labels idenityfing the “topic” of a data sample, usually based on clusters in the data map, at varying levels of granularity. In this case we’ll use data from the titles and abstracts of papers from the machine learning section of the ArXiv preprint server.

[2]:
import numpy as np
import requests
import io

base_url = "https://github.com/TutteInstitute/datamapplot"
data_map_file = requests.get(
    f"{base_url}/raw/main/examples/arxiv_ml_data_map.npy"
)
arxivml_data_map = np.load(io.BytesIO(data_map_file.content))
arxivml_label_layers = []
for layer_num in range(5):
    label_file = requests.get(
        f"{base_url}/raw/interactive/examples/arxiv_ml_layer{layer_num}_cluster_labels.npy"
    )
    arxivml_label_layers.append(np.load(io.BytesIO(label_file.content), allow_pickle=True))
hover_data_file = requests.get(
    f"{base_url}/raw/interactive/examples/arxiv_ml_hover_data.npy"
)
arxiv_hover_data = np.load(io.BytesIO(hover_data_file.content), allow_pickle=True)

Let’s start by making a relatively basic interactive plot with DataMapPlot so we have an idea of what the starting point looks like, and can better understand what the various customizations we will be applying can do for us.

[3]:
plot = datamapplot.create_interactive_plot(
    arxivml_data_map,
    arxivml_label_layers[0],
    arxivml_label_layers[2],
    arxivml_label_layers[4],
    font_family="Cinzel",
)
plot
[3]:

The most basic thing you can do is to provide your plot with a title and, preferably, a sub-title. This can be done with the title and sub_title keyword arguments. DataMapPlot has default placement and formatting for the title and sub-title that should be adequate for most needs. You can override this formatting using some of the custom_css options you’ll see later, but this shoudl suffice for now.

In general the title should be short, while the sub-title can carry more information. If your title or sub-title get too long it will mess with the formatting. For our current data map we can use "ArXiv ML Landscape" as the title, and a slightly longer sub-title of "Papers from the machine learning section of ArXiv".

You might also want a logo in your interactive plot. You can add this with the logo keyword. Unlike the static plot logos you do not need a numpy based encoding of the image; instead you only need a URL where the image to use as the logo can be found. By default the logo is added to the bottom right corner, and you can control sizing with the logo_width keyword argument.

[4]:
plot = datamapplot.create_interactive_plot(
    arxivml_data_map,
    arxivml_label_layers[0],
    arxivml_label_layers[2],
    arxivml_label_layers[4],
    font_family="Cinzel",
    title="ArXiv ML Landscape",
    sub_title="Papers from the machine learning section of ArXiv",
    logo="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/ArXiv_logo_2022.svg/320px-ArXiv_logo_2022.svg.png"
)
plot
[4]:

Another feature that may be useful is the ability to search the data map for specific items. Basic functionality for this is provided with the enable_search keyword, which will add a text search box that provides live interactive search of the hover_text array. In our case the hover text was the title of the paper, so we have live search on the paper titles. You can try out the search below – search for “clustering” or “language”, or something of your choice, and see how the plot only displays points with the search string in the title.

[5]:
plot = datamapplot.create_interactive_plot(
    arxivml_data_map,
    arxivml_label_layers[0],
    arxivml_label_layers[2],
    arxivml_label_layers[4],
    font_family="Cinzel",
    hover_text=arxiv_hover_data,
    enable_search=True,
)
plot
[5]: