Usage of Kwargs in a plot function PyPlot

Let’s say I define a Function that plot some data with a defined attributes. I 'd like to call that function but if I wish specifying/change any attribute. I read about kwarg… keywork, but I’ve not figured out how to use.

using PyPlot

function Track(depth, g,s)
spax=gca()                                               
spax.plot(sp,md,color="blue")
spax.set_xlim([0,50]) 

grax=spax.twiny()        
grax.plot(gamma,md,color="red")
spax.set_xlim([100,500])
return spax, grax
end

Then I’d like to call the function but changing for example the color of spax axes.

Track(x,y,z, color=spax.set_color("green")) #how to change for example the color of spax from blue to green in the call?

how to do this?

You can add keyword arguments as such:

function Track(depth, g, s; spax_color="blue", grax_color="blue")
# ...
spax.plot(sp, md, color=spax_color)
# ...
end
Track(depth, g, s, spax_color="green")

If you want to pass arbitrary arguments to your PyPlot call you can do:

function Track(depth, g, s; spax_args=Dict(), grax_args=Dict())
# ...
spax.plot(sp, md; spax_args...)
# ...
end
Track(depth, g, s, spax_args=Dict(:color => "green"))

Finally, you can also do something like:

function Track(depth, g, s; kwargs...)
# ...
spax.plot(sp, md; kwargs...)
# ...
end
Track(depth, g, s, color="green")
3 Likes