GR.jl icon indicating copy to clipboard operation
GR.jl copied to clipboard

The color argument in plot doesn't work

Open photor opened this issue 4 years ago • 7 comments

I plotted many line segments in one figure, using the following code and hoping that all the segments were of the same color:

using GR

x = y = LinRange(-10,10,20) plot([0,0], [0,0], xlim=(-10,10), ylim=(-10,10), size=(640,640)) normalize = z -> z<0.1 ? 0.1 : z fx=y'./normalize.(x.^2 .+ y'.^2) fy=-x./normalize.(x.^2 .+ y'.^2) m=maximum(sqrt.(fx.^2 + fy.^2)) for i=eachindex(x), j=eachindex(y) oplot([x[i], x[i]+fx[i,j]/m], [y[j], y[j]+fy[i,j]/m], color=:blue) #color doesn't work end oplot([0,0], [0,0])

However, there are still lots of colors in the figure: vector

photor avatar Jun 19 '20 22:06 photor

BTW, how to plot arrows also in this figure? I am doing this because GR seems not to have native realization of plots for vector fields.

photor avatar Jun 20 '20 17:06 photor

Did you see this quiver Python example. Works similar in Julia:

using GR
x = LinRange(-2, 2, 21)
y = x
X, Y = meshgrid(x,y)
z = X .* exp.(-X.^2 - Y.^2)
contour(x, y, z)
u, v = GR.gradient(x, y, z)
quiver(x, y, u, v)
Screen Shot 2020-06-20 at 19 25 33

jheinen avatar Jun 20 '20 17:06 jheinen

Did you see this quiver Python example. Works similar in Julia:

using GR
x = LinRange(-2, 2, 21)
y = x
X, Y = meshgrid(x,y)
z = X .* exp.(-X.^2 - Y.^2)
contour(x, y, z)
u, v = GR.gradient(x, y, z)
quiver(x, y, u, v)
Screen Shot 2020-06-20 at 19 25 33

Thanks. Actually I tried this quiver plot, but my goal is a realization of stream plot for vector fields, which seems absent in GR (am I right?):

using GR

x = y = LinRange(-10,10,20) plot([0,0], [0,0], xlim=(-10,10), ylim=(-10,10), size=(640,640)) normalize = z -> z<0.1 ? 0.1 : z m=1/√0.1 fx(x,y)=y/normalize(x^2+y^2)/m fy(x,y)=-x/normalize(x^2+y^2)/m st=0.1; times=40 for i=eachindex(x), j=eachindex(y) plots_x = [x[i]] plots_y = [y[j]] for k=1:times px=plots_x[end] py=plots_y[end] push!(plots_x, px+stfx(px,py)) push!(plots_y, py+stfy(px,py)) end oplot(plots_x, plots_y) end oplot([0,0], [0,0])

The result is like: stream It would be perfect if the colors are the same (or can be controlled in some way) and arrows can be added.

photor avatar Jun 20 '20 21:06 photor

The color keyword argument is not valid for that function. Use a line specification string:

oplot(plots_x, plots_y, "b")  # e.g. "b" for blue, etc.

The specification strings can be defines as in matplotlib. They can be used in plot, oplot and other line plots.

heliosdrm avatar Jun 21 '20 11:06 heliosdrm

The color keyword argument is not valid for that function. Use a line specification string:

oplot(plots_x, plots_y, "b")  # e.g. "b" for blue, etc.

The specification strings can be defines as in matplotlib. They can be used in plot, oplot and other line plots.

It works, thanks! Is it possible to use rgba in plot or oplot?

photor avatar Jun 21 '20 14:06 photor

Not currenty in GR.jl. For that you might use: (a) GRUtils (cf. https://heliosdrm.github.io/GRUtils.jl/stable/color/), or (b) Plots.jl (https://docs.juliaplots.org/latest/colors/). GRUtils is simpler, with basically the same interface as GR, and does not add extra dependencies, but Plots gives you more flexibility if you want extra features.

heliosdrm avatar Jun 21 '20 15:06 heliosdrm

Not currenty in GR.jl. For that you might use: (a) GRUtils (cf. https://heliosdrm.github.io/GRUtils.jl/stable/color/), or (b) Plots.jl (https://docs.juliaplots.org/latest/colors/). GRUtils is simpler, with basically the same interface as GR, and does not add extra dependencies, but Plots gives you more flexibility if you want extra features.

Thanks for referring, will try GRUtils. Plots.jl with GR backend is too much slower than the original GR.

photor avatar Jun 21 '20 15:06 photor