# PlotlyJS & savefig

**URL:** <https://discourse.julialang.org/t/plotlyjs-savefig/83637>\
**Category:** General Usage\
**Tags:** plotting, plotlyjs\
**Created:** [July 1, 2022, 5:16pm UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637 "2022-07-01T17:16:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 5:16pm UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637/1 "2022-07-01T17:16:58Z")

</div>

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:

```julia
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)`:

```julia

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:

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

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 3, 2022, 1:46pm UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637/2 "2022-07-03T13:46:47Z")

</div>

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:

```julia
using JSON
JSON.parse("")

```

```julia
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()`.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 4, 2022, 7:33am UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637/3 "2022-07-04T07:33:26Z")

</div>

@jd-foster Thanks for the “vague guess”! 🙂  
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?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 4, 2022, 4:36pm UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637/4 "2022-07-04T16:36:11Z")

</div>

@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:

```julia
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:

```julia
template="nothing", xaxis_showgrid=false,

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 14, 2022, 8:59pm UTC](https://discourse.julialang.org/t/plotlyjs-savefig/83637/5 "2022-07-14T20:59:47Z")

</div>

Thanks [empet](https://discourse.julialang.org/u/empet)!  
Unfortunately, on my machine your suggestions does not help ☹  
Error massage is the same.
