Error using Gadfly package in Julia - julia

I am very new to Julia. I have tried a sample code posted in Julia site which uses Gadfly to demonstrate plotting. However, it gives me the below error. I believe that all the dependent packages were installed.
Code:
Pkg.add("Gadfly")
using Gadfly
draw(SVG("output.svg", 6inch, 3inch), plot([sin, cos], 0, 25))
Error I got is:
ERROR: PyError (:PyObject_Call) <type 'exceptions.ValueError'>
ValueError('third arg must be a format string',)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2987, in plot
ret = ax.plot(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 4137, in plot
for line in self._get_lines(*args, **kwargs):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 279, in _plot_args
raise ValueError('third arg must be a format string')
[inlined code] from /Users/mango/.julia/v0.4/PyCall/src/exception.jl:81
in _pycall at /Users/mango/.julia/v0.4/PyCall/src/PyCall.jl:546
in pycall at /Users/mango/.julia/v0.4/PyCall/src/PyCall.jl:568
in plot at /Users/mango/.julia/v0.4/PyPlot/src/PyPlot.jl:395
What is wrong with this sample code?

It indeed appears that the second comment is the issue, however at least in julia version 0.4 if you wish you can have both packages open, you just have to specify which one you are using. So if you have used the commands:
using PyPlot
using Gadfly
draw(SVG("output.svg", 6inch, 3inch), Gadfly.plot([sin, cos], 0, 25))
worked for me. This way you specify that you are using the Gadfly package rather than the PyPlot package for this specific plot

Related

Cannot plot with Gadfly when running script with `julia -i`

When running julia REPL from the command line and copy-pasting my script, the Gadfly plot command works as expected.
But, when running julia -i scriptname.jl plot no longer works and prints:
julia> DataFrame(CSV.File("file.csv"))
julia> plot(massdf, x=:date, y=:mass, group=:day, color=:day, Geom.line)
Plot(...)
julia>
Is there a setting needed to make it work?
You need to explicitly display the output of plot() in your script (which is done implicitly in a Read-Eval-Print-Loop but not when the whole script is executed, even with -i).
Use display(p::Plot), or draw(backend::Compose.Backend, p::Plot) :
df = DataFrame(CSV.File("file.csv"))
p = plot(df, x=:date, y=:mass, group=:day, color=:day, Geom.line)
display(p)
See also Gadfly Backends.

Pkg not defined

I tried following the official Julia docs on plotting, where the following code is proposed for plotting:
Pkg.add("PyPlot")
using PyPlot
x = range(0,stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x))
plot(x, y, color="red", linewidth=2.0, linestyle="--")
Julia v1.0.2 exits with the error
ERROR: LoadError: UndefVarError: Pkg not defined
My question is how to actually run the above code?
The problem is that the Pkg module is not loaded, because this seems to be deprecated. To fix this the following line has to be added before the first line
using Pkg
The code then works for me not from the command line interface, but within an interactive julia session and produces the following image:

failed to execute first example of Differential Algebraic Equations

I am using JuliaPro v0.6.0.1 and the JunoIDE
I tried to apply the DifferentialEquations.jl. In order to run a first example I added the package DifferentialEquations followed by the using statement. In a next step I copied the first example:
f(t,u) = 1.01*u
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8)
using Plots
plot(sol,linewidth=5,title="Solution to the linear ODE with a thick line",
xaxis="Time (t)",yaxis="u(t) (in μm)",label="My Thick Line!") # legend=false
plot!(sol.t, t->0.5*exp(1.01t),lw=3,ls=:dash,label="True Solution!")
Instead of getting the expected graphic I've got the message:
UndefVarError: ODEProblem not defined
Furthermore I've got the warnings right after the: using DifferentialEquations statement
WARNING: The call to compilecache failed to create a usable precompiled cache file for module StochasticDiffEq. Got:
WARNING: Module Iterators uuid did not match cache file.
> LoadError: Declaring __precompile__(true) is only allowed in module files being imported.
which I do not understand. Nevertheless the last warning deals with StochasticDiffEq whereas I'm applying an Ordinary Differential Equation problem.
Any help is appropiated
JuliaPro is incompatible with some packages including DifferentialEquations. I would avoid using JuliaPro and instead use a standard installation until JuliaPro is updated.

Julia Gadfly error: add_plot_element not found

Using Julia 0.3.10 with Juno as IDE and Gadfly latest one. Tried running a sample code, but got stuck with add_plot_element not defined error message. The last line throws the error. Running on Win8 64bit. I'm sure I'm missing something.
using Gadfly
xs = [0:0.1:pi]
k = layer(x=xs, y=sin(xs))
p = plot(x=xs, y=sin(xs))
add_plot_element(k, Guide.title("Now it has a title"))
First, add_plot_element is modifying, so you need the ! like:
add_plot_element!(k,Guide.title(...))
This function is also not exported from Gadfly, so you would really need to write:
Gadfly.add_plot_element!(k, Guide.title("Now it has a title"))
except add_plot_element! doesn't work on Gadfly layers! It does, however, work on plots. What should work:
Gadfly.add_plot_element!(p, Guide.title("Now it has a title"))
since the layer itself doesn't have Guide.Title elements, but the plot does.

Points3D Function in R

I have the scatterplot3d package installed in R. When I load it with library(scatterplot3d) or require(scatterplot3d) I am able to create a 3d scatter plot. However, when I try to use the points3d function I get the following error:
Error: could not find function "points3d"
I tried reinstalling the package to no avail (both inside R and as a tarball using R CMD INSTALL in the command line). I am running Xubuntu 12.10 kernel 3.8.7-030807-generic and R version 2.15.3 (2013-03-01).
Entering locate points3d in the command line gave me no results.
I also tried the par.mar default settings command as explained in the manual.
scatterplot3d does an interesting object-oriented twist on the usual R practice. The object returned from the function call includes the points3d function as built-in part of the object but it is not in the Global environment. It is intended that you add to the existing plot-object using that "attached" function that is not a free-living organism but rather a domesticated animal that only exists in the object corral, so you would use this as your syntax:
object$point3d(x,y,z)
I do "feel your pain" but can show you how to overcome the frustration, since I created a working example yesterday: Using scatterplot3d to plot a sphere
You need to intall the package plot3D in the usual way via
install.packages("plot3D")
Then you just need to import, generate the dataset and use the function points3D()
library(plot3D)
x = rnorm(100)
y = rnorm(100)
z = x + y + rnorm(100,0,1)
points3D(x, y, z, col = rainbow(1000))
This is the plot generated by the code above

Resources