How to manage overlapping yticks and legends in a heatmap plot in julia?

73 Views Asked by At

enter image description here

I'm trying to create a heatmap featuring yticks on both the left and right side of each row. While plotting, I've encountered an issue where the yticks on the right side overlap with the legend. Despite attempting commands like "right_margin," I haven't been successful in adjusting the position of the yticks or the legend—they only seem to add space to the right side of the legend.

Could you offer any advice on how to rearrange the yticks and legends in this plot? Below is the code I've been working with.

Thanks a lot!

using Plots
using Statistics
using DelimitedFiles
using StatsPlots
using DataFrames
using Plots.PlotMeasures
using CSV
using LaTeXStrings

function total_plot_compared_new()
        Catchments=["Feistritz", "Palten", "Gailtal", "Silbertal", "Defreggental", "Pitztal" ]
        Catchments_n=["Feistritztal", "Paltental *", "Gailtal *",  "Silbertal *", "Defreggental", "Pitztal" ]

        obj_title = [L"Obj_{tot}", L"E_{NSE,Q}",L"E_{NSE,log(Q)}", L"E_{VE,Q}", L"E_{NSE,FDC}", L"E_{RE,AC1}", L"E_{NSE,AC90}", L"E_{NSE,RC}", L"E_{AE,SC}"]
        s_calibrations_path = "/01 Model /Calibrations/"

        cal_performance_s = CSV.read("01 Model/04 Results/Calibration/Total/S_Calibration.csv", DataFrame, decimal='.', delim = ',', header=false)
        cal_performance_ns = CSV.read("01 Model/04 Results/Calibration/Total/NS_Calibration.csv", DataFrame, decimal='.', delim = ',', header=false)
        val_performance_s = CSV.read("01 Model/04 Results/Calibration/Total/S_Validation.csv", DataFrame, decimal='.', delim = ',', header=false)
        val_performance_ns = CSV.read("01 Model/04 Results/Calibration/Total/NS_Validation.csv", DataFrame, decimal='.', delim = ',', header=false)
        cal_performance_dif = zeros(6,9)
        val_performance_dif = zeros(6,9)

        for (c,cm) in enumerate(Catchments)

                for (o, ob) in enumerate(obj_title)
                    cal_performance_dif[c,o] = cal_performance_ns[c,o] - cal_performance_s[c,o]
                    val_performance_dif[c,o] = val_performance_ns[c,o] - val_performance_s[c,o]
                end
        end

        cal_performance_s = Matrix(cal_performance_s)
        cal_performance_ns = Matrix(cal_performance_ns)
        val_performance_s = Matrix(val_performance_s)
        val_performance_ns = Matrix(val_performance_ns)

        hm_s = Plots.plot()
        hm_c_s = Plots.heatmap(cal_performance_s, legend=false, c=:RdBu, clims=(-1,1), title="Calibration Sr,cal", titlefont=font(18), xmirror=true)
        for y in 1:1:6
            for x in 1:1:9
                t = round(cal_performance_s[y,x],digits=2)
                    annotate!(x,y,string(t), :white)
            end
        end
        xticks!([1:9;], ["","","","","","","","","",""], xtickfontsize=14)
        yticks!([1:6;], Catchments_n, ytickfont=font(14))
        display(hm_c_s)

        hm_v = Plots.plot()
        hm_v_s = Plots.heatmap(val_performance_s, c=:RdBu, clims=(-1,1), title="Evaluation Sr,cal", titlefont=font(18), ymirror=true)

        for y in 1:1:6
            for x in 1:1:9
                t = round(val_performance_s[y,x],digits=2)
                annotate!(x,y,string(t), :white)
            end
        end
        xticks!([1:9;], ["","","","","","","","",""], xtickfontsize=14)
        yticks!([1:6;], ["F","E","D","C","B","A"], ytickfontsize=14, tickfonthalign=:left)
        total_s = plot(hm_c_s, hm_v_s, layout=(1,2), size=(2500,1000))

        hm_ns = plot()
        hm_c_ns = heatmap(cal_performance_ns, c=:RdBu, clims=(-1,1), legend=false, title="Calibration Sr,clim,adapt", titlefont=font(18))
        for y in 1:1:6
            for x in 1:1:9
                t = round(cal_performance_ns[y,x],digits=2)
                    annotate!(x,y,string(t), :white)
            end
        end
        xticks!([1:9;], ["","","","","","","","",""])
        yticks!([1:6;], Catchments_n, ytickfont=font(14))

        hm_ns = plot()
        hm_v_ns = heatmap(val_performance_ns, c=:RdBu, clims=(-1,1), title="Evaluation Sr,clim,adapt", titlefont=font(18), ymirror=true, ytickfontsize=14)
        for y in 1:1:6
            for x in 1:1:9
                t = round(val_performance_ns[y,x],digits=2)
                    annotate!(x,y,string(t),:white)
            end
        end
        xticks!([1:9;], ["","","","","","","","",""])
        yticks!([1:6;], ["L","K","J","I","H","G"])
        total_ns = plot(hm_c_ns, hm_v_ns, layout=(1,2), size=(2500,1000))

        hm = Plots.plot()
        hm_c_d = heatmap(cal_performance_dif, c=:RdBu, clims=(-0.2,0.2),title="Calibration", legend=false,  titlefont=font(18), ytickfont=18)
        for y in 1:1:6
            for x in 1:1:9
                t = round(cal_performance_dif[y,x],digits=2)
                    annotate!(x,y,string(t), :black)
            end
        end
        xticks!([1:9;], obj_title, xrotation=45, xtickfont=font(14))
        yticks!([1:6;], Catchments_n, ytickfont=font(14))

        hm_v_d = heatmap(val_performance_dif, c=:RdBu, clims=(-0.2,0.2), title="Evaluation", yaxis=nothing, titlefont=font(18), legend_position=:outertopright)

        for y in 1:1:6
            for x in 1:1:9
                t = round(val_performance_dif[y,x],digits=2)
                annotate!(x,y,string(t), :black)
            end
        end
        xticks!([1:9;], obj_title, xrotation=45, xtickfont=font(14), ymirror=true)
        yticks!([1:6;], ["R","Q","P","O","N","M"], ytickfont=font(14))

        total_dif = Plots.plot(hm_c_d, hm_v_d, layout = grid(1,2, widths=[0.46, 0.54]), size=(2500,1100), bottom_margin=30px, top_margin=15px)

        l=@layout[a b{0.55w}; c d{0.55w};e f{0.55w}]
        total_both = Plots.plot(hm_c_s, hm_v_s, hm_c_ns, hm_v_ns, hm_c_d, hm_v_d, layout=l, size=(2500,3000), bottom_margin=10mm, top_margin=1mm)
    
        Plots.savefig("01 Model/04 Results/Calibration/Total/Total_compared_Cal_Val.png")
        
    return
end

enter image description here

0

There are 0 best solutions below