[ncl-talk] emphasize every parts of a line with different colors according to its y value.
Alan Brammer
abrammer at albany.edu
Fri Dec 30 12:59:58 MST 2016
ok, so there are multiples ways to plot a line.
Here’s my method which is over complicated for a single line but has massive speed benefits if you want to plot multiple lines.
Similar to the previous replies, you can use the relatively undocumented gsSegments and gsColors resources to do pretty much what Rick describes but with better scaleability.
Attached function with hopefully sufficient comments to explain whats going on and includes an inline demo function at the bottom I put together a while back.
To run the demo either load the script and call colored_line_demo() or comment out lines 127/128 and then `ncl add_colored_line.ncl` will run it as the main script.
Note to devs, attached in this script is an extended version of get_color_index() to accept an array of "value". Could this be included in the main function, it's a trivial edit and retains backwards compatibility.
Alan.
> On 30 Dec 2016, at 14:21, Guido Cioni <guidocioni at gmail.com> wrote:
>
> I was going to reply with a similar recipe :)
>
> Basically, when drawing the line you have to split every segment and assign a color based on the average value of the y in that segment.
> This is just an hint, you will have to complete the code.
>
> Assuming you have x,y which are your data values:
>
> levels=fspan(min(y), max(y), 20) ; define the levels for the colors, you can change this but it will always depend on the number of segments
>
> cmap = read_colormap_file("amwg_blueyellowred") ; define the colormap that you want to use
>
> do i=0,dimsizes(y)-1 ; loop over the segments
> res at xyLineColors=GetFillColor(levels,cmap,avg((/y(i),y(i+1)/)))
> gsn_polyline(wks,plot,(/x(i),x(i+1)/),(/y(i),y(i+1)/),lres)
> end do
>
> Guido Cioni
> http://guidocioni.altervista <http://guidocioni.altervista/>.org
>
>> On 30 Dec 2016, at 17:35, Rick Brownrigg <brownrig at ucar.edu <mailto:brownrig at ucar.edu>> wrote:
>>
>> Hi,
>>
>> I don't know of any way of doing this other than to draw the line piecewise. What about something like:
>>
>> datadims = dimsizes(data) ; assuming your data is 2xN
>> numlinesegs = datadims(1) - 1
>> linesegs = new((/ numlinesegs/), graphic)
>> do i=1,numlinesegs
>> color = ....appropriate logic to determine the color here...
>> res at gsLineColor = color
>> linesegs(i) = gsn_add_polyline(wks, plot, data(0, i-1:i), data(1, i-1:i), res)
>> end do
>>
>> This is not particularly efficient, and there may be better ways of doing this, but that should work.
>>
>> HTH...
>> Rick
>>
>>
>> On Fri, Dec 30, 2016 at 12:48 AM, grace <313695096 at qq.com <mailto:313695096 at qq.com>> wrote:
>> Hi:
>> All,I am trying to draw a line that emphasize every parts of it with different colors.
>> I have found the example xy_12.ncl which highlight the wanted part by split the data.
>> But I want to draw a line which have different colors according to its y numberical value,just like the plot below.
>> <0356412F at 5DA70B25.65116658.png>
>> If I split the data ,that would be a huge work.
>> Have anybody drawed plot like this ?Any information will be appreciated.
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161230/26bf20a5/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: add_colored_line.ncl
Type: application/octet-stream
Size: 4912 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161230/26bf20a5/attachment.obj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161230/26bf20a5/attachment-0001.html
More information about the ncl-talk
mailing list