Data Coordinates included in plot (Matlab calls it data tip)

I often plot the spectra of the vibration of rotating machines. Here it is critical to know the actual frequency and amplitude of the major sources of vibration. The most convenient way to obtain these values is by plotting the spectra, and then using the mouse to label the peaks in the plot. Matlab calls these data tips. Other custom vibration packages have similar functionality.

I have been searching for a way to do this in a Julia plotting package, mostly by looking at images, as I am new to Julia. This functionality seems to be hard to find.

One workaround is shown in Add Labels and Text to Matplotlib Plots: Annotation Examples, where if the (x, y) values are known they can be plotted programmatically. I am not sure if this functionality exists in plots.jl. A similar approach may be possible in Gadfly Tutorial · Gadfly.jl. About half way down they show an annotation for the creature. Presumably this could be replaced with the (x,y) pair.
Please let me know what you use to annotate plots with a point on a curve that the curser highlights and how well it works.

1 Like

It isn’t entirely clear to me from the question what you want:

  • Do you want to just annotate your plot?
julia> using Plots

julia> plot(rand(10), annotate = (5, 0.5, "my text here"))

image

  • Do you want to see (x,y) coordinates of a plot on mouse hover? This works e.g. with the Plotly backend for Plots.jl:
julia> using Plots; plotly()

julia> plot(rand(10))

image

  • Or do you want to be able to click on a point in your graph and have those coordinates labelled upon clicking? I don’t have an exact idea of how that would work, but I assume you’d have to write some custom code that allows for recording mouse click events - there’s an example of how to do it using PyPlot.jl here: Storing mouse click event coordinates in PyPlot window. Once you’ve recorded the mouse click event you can re-plot with an annotation added.
1 Like

I was asking for the same at [ANN] Gaston v1.0.0 -- Plotting using gnuplot - #4 by RoyiAvital.

Beside PlotLy based solution (Or other HTML based) I couldn’t find how to do it.
This is something I really appreciate in MATLAB’s figure, begin able to explore data values easily.

1 Like

Thanks nilshg for describing what is available.

I like the plotly() based data exploration mentioned above. Then my ideal is to click on one of those points and have that click annotated the graph with the x and y coordinate points. Having this on the plot is valuable for the readers of the report that the plot is put into. Being able to select the precision of the data, say 2 or 3 decimal places would be ideal. Perhaps being able to change the format from fixed to exponential or engineering notation, or ideally to be able to postpend the number with the appropriate SI unit prefix to keep the numbers in fixed format. (T, M, k, m, \micro, n, p etc.).

Ideally the coordinates of the nearest data point is plotted rather than the curser coordinate position. Thanks to RoyiAvital for pointing out the ability of gnuplot to annotate to the curser position, it is a good start. Another feature that would be valuable would be some ability to move the annotation with the curser to minimize its interference with the plot.

I took a look at the Immerse suggestion. An example of how the callback might work would be beneficial. The example of the red circles around the data points looks promising. Is it feasible to add the annotation for the coordinates of these points? And then be able to click to enable this functionality? I also noticed that the examples under Getting and Setting Properties on the Immerse link need updating.

After doing some more trialing in a Pluto Notebook using the above advice and the plots documentation I came up with the following code, where the Vector f, and auto spectra Matrix Gyy are created upstream and I am using the first column of Gyy. Moving the curser shows the x & y values on the plot. The x values are then typed into the annotateFrequencies array and then plotted in the loop giving x & y values at the location of the point. I would show a plot here if I knew how. Edit: Thanks to Rafael’s message below, the plot is now uploaded.

begin
	plotly()       # use for general purpose plotting and looking for peaks
	amp = sqrt.(Gyy[:,1]/2)
	plot1 = plot(f, amp, 
		title=measurements[1],
		legend=false)
	ylabel!("$(measurements[1]) [$(Unit[measurements[1]]) rms]")
	xlabel!("Frequency [Hz]")
	xlims!(0, 50)
	ylims!(0, Inf)
	# put frequency values in the Vector below for annotation
	annotateFrequencies = [6, 7.5, 12.25, 15, 18.25, 22.5, 30, 45]
	idx = round.(Int, annotateFrequencies/deltaf) .+ 1
	for n in idx
		annotate!(f[n],amp[n],
			text("($(round(f[n];digits=2)),$(round(amp[n];digits=2)))",10,:left))
	end
	savefig(plot1, "upload.png")
	plot1
end
2 Likes

@Jake, to save your plotly plot as a png file, in Julia 1.6 there is a message for: using PlotlyBase, then after your plot command, save it to disk with: savefig("plotly_datatips_plot.png"). Then use Discourse upload icon.