Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text labels to a scatterplot?

Tags:

plot

julia

Is there a way to add text labels to the points on a scatterplot? Each point has a string associated with it as its label. I like to label only as many points as it can be done withour overlapping?

df = DataFrame(x=rand(100), y=rand(100), z=randstring.(fill(5,100)))
scatter(df.x, df.y)
annotate!(df.x, df.y, text.(df.z))
like image 872
siebenstein Avatar asked Sep 04 '25 01:09

siebenstein


2 Answers

using StatisticalGraphics package:

using InMemoryDatasets
using StatisticalGraphics
using Random

ds=Dataset(x=rand(100), y=rand(100), z=randstring.(fill(5,100)))
sgplot(ds, Scatter(x=:x,y=:y,labelresponse=:z))

enter image description here

like image 113
giantmoa Avatar answered Sep 07 '25 16:09

giantmoa


Here is something I wrote for Makie.jl that suited my needs:

Non-overlapping labels for scatter plots

It works best for single line, short text labels, and where all labels have similar lengths with one another. It is still WIP, as I am working to improve it for placement of longer text labels.

Here are some samples of what it can do:

Near linear distribution of data Random distribution of data Quadratic distribution of data

Essentially, you call function viz to plot a scatter chart on your (x, y) data set:

resolution = (600, 600)     # figure size (pixels) -- need not be a equal dimension
fontpt = 12                 # label font size (points)
flabel = 1.5                # inflate the label size to create some margins
fdist = 0.3                 # inflate the max. distance between a label and its
                            #   anchor point before a line is drawn to connect. them.
                            #   Smaller values would create more connecting lines.

viz(x, y, labels; resolution=resolution, flabel=flabel, fdist=fdist, fontpt=fontpt)

where labels is a list containing the text labels for every pair of (x, y) point.

like image 41
cbsteh Avatar answered Sep 07 '25 18:09

cbsteh