PlotlyJS & savefig

I like the package PlotlyJS because of the good support of a secondary Y-axis.
Unfortunatly, I discovered that the savefig() seems to be restricted in the number of points it can handle
and I don’t know if this is a PlotlyJS or a Julia-Issue.
In my case it is a frequency spectrum with 24’999’876 points.
This causes savefig() to crash, the error messages are different, one is:

ERROR: Unexpected end of input
Line: 0
Around: ......
           ^

Below is my example. - You can check where your system fails, by modifyfing the factor before
the number of elements num_ = round(Int, 0.01 * 24999876):


using PlotlyJS

function _plot_single_spectra(num_::Int)
	_amplitudes_Shunt = rand(num_)
	_frequencies_Shunt = collect(range(1, num_))
	_amplitudes_Hall = rand(num_)
	_frequencies_Hall = collect(range(1, num_))

    line_amplitudes_shunt   = scatter(; x =_frequencies_Shunt,  y = _amplitudes_Shunt, name = "Shunt")
    line_amplitudes_hall    = scatter(; x =_frequencies_Hall,   y = _amplitudes_Hall,  name = "Hall", yaxis="y2")
    stem_frequ_Shunt_assumed     = stem(;   x = [0.4*num_], y = [1], name = "f_Shunt(assumed)")
    stem_frequ_Hall_assumed      = stem(;   x = [0.6*num_], y = [1], name = "f_Hall(assumed)", yaxis="y2")
    data = [line_amplitudes_shunt, line_amplitudes_hall, stem_frequ_Shunt_assumed, stem_frequ_Hall_assumed]
    mylayout = Layout(
        title_text       = "FFT spectrum",
        xaxis_title_text = "Frequency / Hz",
        yaxis_title_text = "Amplitude Shunt",
		# yaxis_type       = "log",
        yaxis2 = PlotlyJS.attr(
            title      = "Amplitude Hall",
			# type       = "log",
            overlaying = "y",
            side       = "right"
            )
        )
    # ---
    return Plot(data, mylayout)
end

# ---
fn_plot = "c:/tmp/plt/test.pdf"
num_ = round(Int, 0.01 * 24999876)

# ---
hdl_plt = _plot_single_spectra(num_)
println("-----  ready to save plot   -----")
savefig(hdl_plt, fn_plot)

Has someone a proposal how to plot large amount of points with a 2nd y-Axis?


P.S.:
It might be that the main trouble maker is:

@ JSON.Parser C:\Users\...\.julia\packages\JSON\NeJ9k\src\Parser.jl:140

I can reproduce this. I think the issue might lie with PlotlyJS but due to a limitation in parsing the very large JSON string generated from PlotlyJS. The error is very similar to what happens when JSON.jl tries to parse the empty string:

using JSON
JSON.parse("")
ERROR: Unexpected end of input
Line: 0
Around: ......
           ^

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:140
 [3] byteat
   @ ~/.julia/packages/JSON/QXB8U/src/Parser.jl:49 [inlined]
 [4] parse_value(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:160
 [5] parse(str::String; dicttype::Type, inttype::Type{Int64}, allownan::Bool, null::Nothing)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:450
 [6] parse(str::String)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:448

So my vague guess would be that the error occurs around here:
PlotlyJS ~/.julia/packages/PlotlyJS/4jzLr/src/kaleido.jl:104
when any empty string is given to JSON.parse().

@jd-foster Thanks for the “vague guess”! :slight_smile:
Therefore, I will see if the PlotlyJS contributors have an idea, how to avoid this behaviour.
What is your prefered way to plot with multiple Y-axis?

@ellocco When you save a plot as pdf, it is recommended to set DPI and width and height of the pdf image, in inches (1inch=2.54cm).

Running Julia 1.7.0, PlotlyJS 0.18.8 and replacing your savefig with:

savefig(hdl_plt, fn_plot, width=round(Int, 300*2.75), height=300*2, scale=1)

the hdl_plt has been saved without any error. 300 is DPI, (2.75inches, 2inches) is the pdf image (width, height) or in cm, (6.985cm, 5.08cm).
LE: I inserted in your layout:

template="nothing", xaxis_showgrid=false,
4 Likes

Thanks empet!
Unfortunately, on my machine your suggestions does not help :frowning:
Error massage is the same.