Makie can't plot sin(1/x) : `Makie.convert_arguments`

Hi all,

I try to plot sin(1/x) by using Makie(CairoMakie)
https://makie.juliaplots.org/stable/tutorials/basic-tutorial/

using CairoMakie

x = LinRange(0, 10, 100)
y = sin.(1/x)

fig = Figure()
lines(fig[1, 1], x, y, color = :red)
lines(fig[1, 2], x, y, color = :blue)
lines(fig[2, 1:2], x, y, color = :green)

fig

There are errors when I do the code above ( I don’t really understand how to convert the arguments), is it impossible to plot sin(1/x) for Makie?

Makie.convert_arguments for the plot type Lines{Tuple{LinRange{Float64, Int64}, LinearAlgebra.Transpose{Float64, Vector{Float64}}}} and its conversion trait PointBased() was unsuccessful.

The signature that could not be converted was:
::Vector{Float32}, ::Matrix{Float32}

Makie needs to convert all plot input arguments to types that can be consumed by the backends (typically Arrays with Float32 elements).
You can define a method for Makie.convert_arguments (a type recipe) for these types or their supertypes to make this set of arguments convertible (See http://makie.juliaplots.org/stable/recipes.html).

Alternatively, you can define Makie.convert_single_argument for single arguments which have types that are unknown to Makie but which can be converted to known types and fed back to the conversion pipeline.

Maybe do 1./x

As @jules mentioned you need to do 1 ./ x (note the space) to broadcast the division. 1/x is doing matrix right division (see the docs).

You probably also don’t want to include x=0 in your range since sin(Inf) is not defined.

@isaacsas @jules

this is the code, I still can’t plot sin (1/x):

using CairoMakie, ColorSchemes, Colors
include("/home/browni/LasthrimProjection/makieTheme4.jl") # don't forget to include the theme.

# Generates noise
using Random
Random.seed!(123)
x = -30:0.05:30
# The function
y = sin.(1 ./x)
n=15
y[1:n] = y[1:n] .+ 0.02*randn(n);

fig = Figure(resolution = (600, 400))
ax1 = Axis(fig, xlabel = "x", ylabel = "f(x)",
    xgridvisible = false, 
    ygridvisible = false)
ax2 = Axis(fig, bbox = BBox(140, 260, 260, 350),
    xticklabelsize = 12, yticklabelsize = 12,showgrid=false,
  #title = "inset  at (100, 300, 100, 200)"
)
line1 = lines!(ax1, x, y, color = :red)
# inset
lines!(ax2, x, y, color = :red)
limits!(ax2, -3.1,-1.9,-0.05,0.05)
ax2.yticks = [-0.05,0,0.05]
ax2.xticks = [-3,-2.5,-2]
leg = Legend(fig, [line1], ["f(x)"], halign = :right, valign = :top)

fig[1, 1] = ax1
fig[1, 1] = leg

save("./FigInset1.png", fig, px_per_unit = 1)

fig

This is the source:
https://nbviewer.org/github/lazarusA/MakieNotebooks/blob/main/Makie_inset.ipynb

Is there any other package to plot an inset? A zoom in at certain x axis for Plotly? or GR? or other package?

This is working fine:

using CairoMakie

x = LinRange(-3, 3, 2050)
y = sin.(1 ./x)

fig = Figure()
lines(fig[1, 1], x, y, color = :red)
lines(fig[1, 2], x, y, color = :blue)
lines(fig[2, 1:2], x, y, color = :green)

fig

or

using CairoMakie

x = LinRange(-0.1, 1, 250)
y = sin.(1 ./x)

x1 = LinRange(-1, 0.1, 250)
y1 = sin.(1 ./x)

x2 = LinRange(-30, 3, 250)
y2 = sin.(1 ./x)

fig = Figure()
lines(fig[1, 1], x, y, color = :red)
lines(fig[1, 2], x1, y1, color = :blue)
lines(fig[2, 1:2], x2, y2, color = :green)

fig

But, how to put the one here, into one up there?

or

How to modify the one here to become an inset?

Sorry I don’t understand what you mean.

Sorry I should be more clear.

I want to create a zoom in of a plot in certain interval, how? I play around with this code:

using CairoMakie

x = LinRange(-0.1, 1, 250)
y = sin.(1 ./x)

x1 = LinRange(-1, 0.1, 250)
y1 = sin.(1 ./x)

x2 = LinRange(-30, 3, 250)
y2 = sin.(1 ./x)

fig = Figure()
lines(fig[1, 1], x, y, color = :red)
lines(fig[1, 2], x1, y1, color = :blue)
lines(fig[2, 1:2], x2, y2, color = :green)

fig

You are reusing x in each line calculating y, y1 and y2. I assume you intended to calculate y1 from x1 (not x) and y2 from x2 (also not x)

1 Like

Wow Thanks a lot!!! must have been a miracle for me that you give me this insight.