;********************************* ; based on bar_horz_13.ncl ; test script to have different labels colored the same as the bar chart ; ;********************************** begin ;******************************************************** ; create the data ;******************************************************** ;---Read file as arrays of strings for parsing later lines = asciiread("dummydata.txt",-1,"string") print(lines) nlines = dimsizes(lines) ;-------- Calculate number of models nmodels = nlines-2 ; The -2 is for the min/max/avg header x = ispan(0,nmodels,1) ;-------- Special labels for tickmarks, get name of models models = new(nmodels+1,string) ; Add one for the blank space b/w models do i=0,nmodels-1 models(i) = str_get_field(lines(i+2),1," ") end do ;-------- Parse out min/max/avg values and put in big "data" array data = new((/nmodels+1,3/),float) ; models x (min/max/avg) do j=0,nmodels-1 do k=0,2 data(j,k) = tofloat(str_get_field(lines(j+2),2+k," ")) end do end do ;******************************************************** ; create the plot ;******************************************************** wks_type = "pdf" wks = gsn_open_wks(wks_type,"horizontal_bar_chart_variation.pdf") ;----------- ressources --------------------------------------------------------- ;---Values for the X and Y axes xmin = min(data(:,0))-2 ; add small margins at xmax = max(data(:,1))+2 ; top and bottom ymin = min(x)-1 ymax = max(x) ;---resources for each bar chart res = True res@gsnMaximize = True ; maximize plots in frame res@gsnFrame = False res@gsnDraw = False res@vpWidthF = 0.3 res@vpHeightF = 0.8 res@trYMinF = ymin res@trYMaxF = ymax res@trXMinF = xmin res@trXMaxF = xmax res@tmYROn = True res@tmYLOn = False res@tmXTOn = False res@tmYROn = True res@tmYRLabelsOn = True res@tmYUseLeft = False res@tmYLLabelFontHeightF = 0.01 res@tmYRLabelFontHeightF = 0.006 res@tmXBLabelFontHeightF = 0.006 res@tmXBMajorLengthF = 0.011 res@tmXBMajorOutwardLengthF = 0.011 res@tmXBMinorLengthF = 0.008 res@tmXBMinorOutwardLengthF = 0.008 ;---Put model names on Y axis ii = ind(.not.ismissing(models)) res@tmYRMode = "Explicit" res@tmYRValues = x(ii) res@tmYRLabels = models(ii) res@tmYRLabelJust = "CenterCenter" res@tmYRLabelDeltaF = 3 res@tiMainString = "Title main string" res@tiMainFontHeightF = 0.01 ;---Turn on bar chart res@gsnXYBarChart = True res@gsnXYBarChartBarWidth = 0.2 ; Default is 1.0 res@gsnXRefLineColor = "transparent" ;res@gsnRightXRefLineColor = "red" ;res@gsnLeftXRefLineColor = "blue" colors = (/"SlateBlue","SkyBlue","blue","navy","green","orange","red","firebrick","gray","black","steelblue","darkgreen",\ "SlateBlue","SkyBlue","blue","navy","green","orange","red","firebrick","gray","black","steelblue","darkgreen"/) min_plot = new(nmodels,graphic) max_plot = new(nmodels,graphic) do i=0,nmodels-1 res@gsnXYBarChartColors = colors(i) ;<================= this works res@tmYRLabelFontColor = colors(i) ;<================= this doesn't work if(.not.ismissing(data(i,2))) then res@gsnXRefLine = data(i,2) ; avg value, indicates where the red/blue bars begin and end, necessary for horizontal plot if(i.eq.0) then ;---First time in loop, create base plot base_plot = gsn_csm_xy(wks,data(i,0),x(i),res) ; min value max_plot(i) = gsn_csm_xy(wks,data(i,1),x(i),res) ; max value overlay(base_plot,max_plot(i)) else min_plot(i) = gsn_csm_xy(wks,data(i,0),x(i),res) ; min value max_plot(i) = gsn_csm_xy(wks,data(i,1),x(i),res) ; max value overlay(base_plot,min_plot(i)) overlay(base_plot,max_plot(i)) end if end if end do draw(base_plot) ;---Drawing the base plot draws everything. frame(wks) ;===================================================================================== end