[julia-users] Re: Converting from ColorTypes to Tuple
Another quick solution is to just create an array/tuple that PyPlot will recognize as an RGB array/tuple: myColors = distinguishable_colors(N) PyPlot_myColors = [[red(i), green(i), blue(i)] for i in myColors] You can also save yourself the time from needing to call the new color scheme for every plot by redefining the color cycle: ax[:set_color_cycle](PyPlot_myColors) Gabriel Gellner wrote > I use the following as a utility to have PyPlot.jl/PyCall.jl automatically > convert RGB types into tuples > > function PyObject(t::Color) > trgb = convert(RGB, t) > ctup = map(float, (red(trgb), green(trgb), blue(trgb))) > o = PyObject(ctup) > return o > end > > I'm sure it can be tweaked to be more general. But it works so far when I > am doing quick and dirty plotting :) Good luck! > > On Thursday, July 7, 2016 at 6:09:41 PM UTC-7, Tim Holy wrote: > >> On Thursday, July 7, 2016 4:41:10 PM CDT Islam Badreldin wrote: >> > Maybe >> > this means PyPlot.jl needs to add better support for ColorTypes? >> >> That sounds like a very reasonable solution. I don't really know PyPlot >> at >> all, so I don't have any advice to offer, but given how well you seem to >> understand things already it seems that matters are in excellent hands >> :-). >> >> Best, >> --Tim >> >> -- View this message in context: http://julia-programming-language.2336112.n4.nabble.com/Converting-from-ColorTypes-to-Tuple-tp43674p44391.html Sent from the Julia Users mailing list archive at Nabble.com.
Re: [julia-users] Re: Converting from ColorTypes to Tuple
I use the following as a utility to have PyPlot.jl/PyCall.jl automatically convert RGB types into tuples function PyObject(t::Color) trgb = convert(RGB, t) ctup = map(float, (red(trgb), green(trgb), blue(trgb))) o = PyObject(ctup) return o end I'm sure it can be tweaked to be more general. But it works so far when I am doing quick and dirty plotting :) Good luck! On Thursday, July 7, 2016 at 6:09:41 PM UTC-7, Tim Holy wrote: > On Thursday, July 7, 2016 4:41:10 PM CDT Islam Badreldin wrote: > > Maybe > > this means PyPlot.jl needs to add better support for ColorTypes? > > That sounds like a very reasonable solution. I don't really know PyPlot at > all, so I don't have any advice to offer, but given how well you seem to > understand things already it seems that matters are in excellent hands > :-). > > Best, > --Tim > >
Re: [julia-users] Re: Converting from ColorTypes to Tuple
On Thursday, July 7, 2016 4:41:10 PM CDT Islam Badreldin wrote: > Maybe > this means PyPlot.jl needs to add better support for ColorTypes? That sounds like a very reasonable solution. I don't really know PyPlot at all, so I don't have any advice to offer, but given how well you seem to understand things already it seems that matters are in excellent hands :-). Best, --Tim
Re: [julia-users] Re: Converting from ColorTypes to Tuple
Thanks Tim for the clarification. Yes, you're right. Maybe I created the post basically after figuring out how to do it. But the more fundamental issue here is that I simply wanted to pass the color obtained from ColorBrewer directly to PyPlot and expected it to work out of the box. I was surprised that wasn't the case when I got an error message, which prompted me to dig a little further and do the manual conversion. Maybe this means PyPlot.jl needs to add better support for ColorTypes? -Islam On Tuesday, July 5, 2016 at 1:46:18 PM UTC-4, Tim Holy wrote: > Well, you've just written that convenience function yourself :-). It's not > more complicated than that. > > The reason there isn't an exported function is because the general case is > not > quite as simple as it may seem. For example, BGR and RGB both mean "RGB > colors," but with different internal storage order. Which do you care about, > storage order or "meaning"? When you access a value as `red(col)`, you're > always getting the red channel no matter how it's stored, whereas > `comp1(col)` > might give you the red channel or might give you the blue channel. So > "converting to a tuple" might mean different things to different people ("red > first" or "component 1 first"?), which is why there's no general > function---it's > so simple to write, you might as well write it yourself, because you know > what > you want it to mean. > > When you're dealing with arrays, you can always use `reinterpret` to force an > interpretation. > > Best, > --Tim > > On Tuesday, July 5, 2016 9:59:00 AM CDT Islam Badreldin wrote: > > Forgot to add `using ColorTypes` to the top of the minimal example. > > > > -Islam > > > > On Tuesday, July 5, 2016 at 12:55:37 PM UTC-4, Islam Badreldin wrote: > > > Hello, > > > > > > I was experimenting with plotting using PyPlot and I wanted to create line > > > plots with custom colors. I wanted to use a color palette from > > > ColorBrewer.jl, which returns the colors as an array of ColorTypes colors. > > > I wanted to convert the returned colors to a Tuple, e.g. > > > (0.894,0.102,0.11), in order to pass it to PyPlot. The following snippet > > > works for me, > > > > > > > > > using PyPlot > > > > > > using ColorBrewer > > > > > > myc=palette("Set1",9) > > > > > > for i in 1:9 plot(rand(100),c=(comp1(myc[i]),comp2(myc[i]),comp3(myc[i]))) > > > end > > > > > > > > > My question is, is there a convenience function that can directly give me > > > the desired tuple, without having to manually construct it using > > > (comp1,comp2,comp3)? > > > > > > Thanks, > > > Islam
Re: [julia-users] Re: Converting from ColorTypes to Tuple
Well, you've just written that convenience function yourself :-). It's not more complicated than that. The reason there isn't an exported function is because the general case is not quite as simple as it may seem. For example, BGR and RGB both mean "RGB colors," but with different internal storage order. Which do you care about, storage order or "meaning"? When you access a value as `red(col)`, you're always getting the red channel no matter how it's stored, whereas `comp1(col)` might give you the red channel or might give you the blue channel. So "converting to a tuple" might mean different things to different people ("red first" or "component 1 first"?), which is why there's no general function---it's so simple to write, you might as well write it yourself, because you know what you want it to mean. When you're dealing with arrays, you can always use `reinterpret` to force an interpretation. Best, --Tim On Tuesday, July 5, 2016 9:59:00 AM CDT Islam Badreldin wrote: > Forgot to add `using ColorTypes` to the top of the minimal example. > > -Islam > > On Tuesday, July 5, 2016 at 12:55:37 PM UTC-4, Islam Badreldin wrote: > > Hello, > > > > I was experimenting with plotting using PyPlot and I wanted to create line > > plots with custom colors. I wanted to use a color palette from > > ColorBrewer.jl, which returns the colors as an array of ColorTypes colors. > > I wanted to convert the returned colors to a Tuple, e.g. > > (0.894,0.102,0.11), in order to pass it to PyPlot. The following snippet > > works for me, > > > > > > using PyPlot > > > > using ColorBrewer > > > > myc=palette("Set1",9) > > > > for i in 1:9 plot(rand(100),c=(comp1(myc[i]),comp2(myc[i]),comp3(myc[i]))) > > end > > > > > > My question is, is there a convenience function that can directly give me > > the desired tuple, without having to manually construct it using > > (comp1,comp2,comp3)? > > > > Thanks, > > Islam
[julia-users] Re: Converting from ColorTypes to Tuple
Forgot to add `using ColorTypes` to the top of the minimal example. -Islam On Tuesday, July 5, 2016 at 12:55:37 PM UTC-4, Islam Badreldin wrote: > Hello, > > I was experimenting with plotting using PyPlot and I wanted to create line > plots with custom colors. I wanted to use a color palette from > ColorBrewer.jl, which returns the colors as an array of ColorTypes colors. > I wanted to convert the returned colors to a Tuple, e.g. > (0.894,0.102,0.11), in order to pass it to PyPlot. The following snippet > works for me, > > > using PyPlot > > using ColorBrewer > > myc=palette("Set1",9) > > for i in 1:9 plot(rand(100),c=(comp1(myc[i]),comp2(myc[i]),comp3(myc[i]))) > end > > > My question is, is there a convenience function that can directly give me > the desired tuple, without having to manually construct it using > (comp1,comp2,comp3)? > > Thanks, > Islam >