|
| 1 | +module SignalTablesInterface_PyPlot |
| 2 | + |
| 3 | +# License for this file: MIT (expat) |
| 4 | +# Copyright 2017-2022, DLR Institute of System Dynamics and Control |
| 5 | + |
| 6 | +# ToDo: |
| 7 | +# MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently |
| 8 | +# reuses the earlier instance. In a future version, a new instance will always be created and returned. |
| 9 | +# Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. |
| 10 | +# |
| 11 | +# Description how to get rid of the warning: |
| 12 | +# https://stackoverflow.com/questions/46933824/matplotlib-adding-an-axes-using-the-same-arguments-as-a-previous-axes# |
| 13 | +# |
| 14 | +# ax1 = subplot(..) |
| 15 | +# plot(..) |
| 16 | +# subplot(ax1) # plot into this previously defined subplot |
| 17 | +# plot(..) |
| 18 | +# However, SignalTables.plot(..) has no internal state and has currently now way to pass the ax1 definition to the next SignalTables.plot(..) call. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +# It seems that rcParams settings has only an effect, when set on PyPlot in Main |
| 23 | +import SignalTables |
| 24 | +import Measurements |
| 25 | +import MonteCarloMeasurements |
| 26 | + |
| 27 | +# Determine whether pmean, pmaximum, pminimum is available (MonteCarlMeasurements, version >= 1.0) |
| 28 | +const pfunctionsDefined = isdefined(MonteCarloMeasurements, :pmean) |
| 29 | + |
| 30 | +using Unitful |
| 31 | + |
| 32 | +import PyCall |
| 33 | +import PyPlot |
| 34 | + |
| 35 | +export plot, showFigure, saveFigure, closeFigure, closeAllFigures |
| 36 | + |
| 37 | + |
| 38 | +set_matplotlib_rcParams!(args...) = |
| 39 | + merge!(PyCall.PyDict(PyPlot.matplotlib."rcParams"), Dict(args...)) |
| 40 | + |
| 41 | + |
| 42 | +include("$(SignalTables.path)/src/AbstractLinePlotInterface.jl") |
| 43 | + |
| 44 | + |
| 45 | +function plotOneSignal(xsig, ysig, ysigType, label, MonteCarloAsArea) |
| 46 | + xsig2 = ustrip.(xsig) |
| 47 | + ysig2 = ustrip.(ysig) |
| 48 | + if typeof(ysig2[1]) <: Measurements.Measurement |
| 49 | + # Plot mean value signal |
| 50 | + xsig_mean = Measurements.value.(xsig2) |
| 51 | + ysig_mean = Measurements.value.(ysig2) |
| 52 | + curve = PyPlot.plot(xsig_mean, ysig_mean, label=label) |
| 53 | + |
| 54 | + # Plot area of uncertainty around mean value signal (use the same color, but transparent) |
| 55 | + color = PyPlot.matplotlib.lines.Line2D.get_color(curve[1]) |
| 56 | + rgba = PyPlot.matplotlib.colors.to_rgba(color) |
| 57 | + rgba2 = (rgba[1], rgba[2], rgba[3], 0.2) |
| 58 | + ysig_u = Measurements.uncertainty.(ysig2) |
| 59 | + ysig_max = ysig_mean + ysig_u |
| 60 | + ysig_min = ysig_mean - ysig_u |
| 61 | + PyPlot.fill_between(xsig_mean, ysig_min, ysig_max, color=rgba2) |
| 62 | + |
| 63 | + elseif typeof(ysig2[1]) <: MonteCarloMeasurements.StaticParticles || |
| 64 | + typeof(ysig2[1]) <: MonteCarloMeasurements.Particles |
| 65 | + # Plot mean value signal |
| 66 | + if pfunctionsDefined |
| 67 | + # MonteCarlMeasurements, version >= 1.0 |
| 68 | + xsig_mean = MonteCarloMeasurements.pmean.(xsig2) |
| 69 | + ysig_mean = MonteCarloMeasurements.pmean.(ysig2) |
| 70 | + else |
| 71 | + # MonteCarloMeasurements, version < 1.0 |
| 72 | + xsig_mean = MonteCarloMeasurements.mean.(xsig2) |
| 73 | + ysig_mean = MonteCarloMeasurements.mean.(ysig2) |
| 74 | + end |
| 75 | + xsig_mean = ustrip.(xsig_mean) |
| 76 | + ysig_mean = ustrip.(ysig_mean) |
| 77 | + curve = PyPlot.plot(xsig_mean, ysig_mean, label=label) |
| 78 | + color = PyPlot.matplotlib.lines.Line2D.get_color(curve[1]) |
| 79 | + rgba = PyPlot.matplotlib.colors.to_rgba(color) |
| 80 | + |
| 81 | + if MonteCarloAsArea |
| 82 | + # Plot area of uncertainty around mean value signal (use the same color, but transparent) |
| 83 | + rgba2 = (rgba[1], rgba[2], rgba[3], 0.2) |
| 84 | + if pfunctionsDefined |
| 85 | + # MonteCarlMeasurements, version >= 1.0 |
| 86 | + ysig_max = MonteCarloMeasurements.pmaximum.(ysig2) |
| 87 | + ysig_min = MonteCarloMeasurements.pminimum.(ysig2) |
| 88 | + else |
| 89 | + # MonteCarloMeasurements, version < 1.0 |
| 90 | + ysig_max = MonteCarloMeasurements.maximum.(ysig2) |
| 91 | + ysig_min = MonteCarloMeasurements.minimum.(ysig2) |
| 92 | + end |
| 93 | + ysig_max = ustrip.(ysig_max) |
| 94 | + ysig_min = ustrip.(ysig_min) |
| 95 | + PyPlot.fill_between(xsig_mean, ysig_min, ysig_max, color=rgba2) |
| 96 | + else |
| 97 | + # Plot all particle signals (use the same color, but transparent) |
| 98 | + rgba2 = (rgba[1], rgba[2], rgba[3], 0.1) |
| 99 | + value = ysig[1].particles |
| 100 | + ysig3 = zeros(eltype(value), length(xsig)) |
| 101 | + for j in 1:length(value) |
| 102 | + for i in eachindex(ysig) |
| 103 | + ysig3[i] = ysig[i].particles[j] |
| 104 | + end |
| 105 | + ysig3 = ustrip.(ysig3) |
| 106 | + PyPlot.plot(xsig, ysig3, color=rgba2) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + else |
| 111 | + if typeof(xsig2[1]) <: Measurements.Measurement |
| 112 | + xsig2 = Measurements.value.(xsig2) |
| 113 | + elseif typeof(xsig2[1]) <: MonteCarloMeasurements.StaticParticles || |
| 114 | + typeof(xsig2[1]) <: MonteCarloMeasurements.Particles |
| 115 | + if pfunctionsDefined |
| 116 | + # MonteCarlMeasurements, version >= 1.0 |
| 117 | + xsig2 = MonteCarloMeasurements.pmean.(xsig2) |
| 118 | + else |
| 119 | + # MonteCarlMeasurements, version < 1.0 |
| 120 | + xsig2 = MonteCarloMeasurements.mean.(xsig2) |
| 121 | + end |
| 122 | + xsig2 = ustrip.(xsig2) |
| 123 | + end |
| 124 | + if ysigType == SignalTables.Continuous |
| 125 | + PyPlot.plot(xsig2, ysig2, label=label) |
| 126 | + else # SignalTables.Clocked |
| 127 | + PyPlot.plot(xsig2, ysig2, ".", label=label) |
| 128 | + end |
| 129 | + end |
| 130 | +end |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +""" |
| 135 | + addPlot(names, result, grid, xLabel, xAxis, prefix, reuse, maxLegend, MonteCarloAsArea, figure, i, j, nsubFigures) |
| 136 | +
|
| 137 | +Add the time series of one name (if names is one symbol/string) or with |
| 138 | +several names (if names is a tuple of symbols/strings) to the current diagram |
| 139 | +""" |
| 140 | +function addPlot(collectionOfNames::Tuple, result, grid::Bool, xLabel::Bool, xAxis, prefix::AbstractString, reuse::Bool, maxLegend::Integer, |
| 141 | + MonteCarloAsArea::Bool, figure::Int, i::Int, j::Int, nsubFigures::Int) |
| 142 | + xsigLegend = "" |
| 143 | + nLegend = 0 |
| 144 | + |
| 145 | + for name in collectionOfNames |
| 146 | + name2 = string(name) |
| 147 | + (xsig, xsigLegend, ysig, ysigLegend, ysigType) = SignalTables.getPlotSignal(result, name2; xsigName = xAxis) |
| 148 | + if !isnothing(xsig) |
| 149 | + nLegend = nLegend + length(ysigLegend) |
| 150 | + if ndims(ysig) == 1 |
| 151 | + plotOneSignal(xsig, ysig, ysigType, prefix*ysigLegend[1], MonteCarloAsArea) |
| 152 | + else |
| 153 | + for i = 1:size(ysig,2) |
| 154 | + plotOneSignal(xsig, ysig[:,i], ysigType, prefix*ysigLegend[i], MonteCarloAsArea) |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + PyPlot.grid(grid) |
| 161 | + if nLegend <= maxLegend |
| 162 | + PyPlot.legend() |
| 163 | + elseif nsubFigures == 1 |
| 164 | + @info "plot(..): No legend in figure $figure, since curve number (= $nLegend) > maxLegend (= $maxLegend)\nCan be fixed by plot(..., maxLegend=$nLegend)" |
| 165 | + else |
| 166 | + @info "plot(..): No legend in subfigure ($i,$j) of figure $figure, since curve number (= $nLegend) > maxLegend (= $maxLegend)\nCan be fixed by plot(..., maxLegend=$nLegend)" |
| 167 | + end |
| 168 | + |
| 169 | + if xLabel && !reuse && xsigLegend !== nothing |
| 170 | + PyPlot.xlabel(xsigLegend) |
| 171 | + end |
| 172 | +end |
| 173 | + |
| 174 | +addPlot(name::AbstractString, args...) = addPlot((name,) , args...) |
| 175 | +addPlot(name::Symbol , args...) = addPlot((string(name),), args...) |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +#--------------------------- Plot function |
| 180 | +function plot(result, names::AbstractMatrix; heading::AbstractString="", grid::Bool=true, xAxis=nothing, |
| 181 | + figure::Int=1, prefix::AbstractString="", reuse::Bool=false, maxLegend::Integer=10, |
| 182 | + minXaxisTickLabels::Bool=false, MonteCarloAsArea=false) |
| 183 | + |
| 184 | + set_matplotlib_rcParams!("axes.formatter.limits" => [-3,4], |
| 185 | + "font.size" => 8.0, |
| 186 | + "lines.linewidth" => 1.0, |
| 187 | + "grid.linewidth" => 0.5, |
| 188 | + "axes.grid" => true, |
| 189 | + "axes.titlesize" => "medium", |
| 190 | + "figure.titlesize" => "medium") |
| 191 | + |
| 192 | + PyPlot.pygui(true) # Use separate plot windows (no inline plots) |
| 193 | + |
| 194 | + |
| 195 | + if isnothing(result) |
| 196 | + @info "The call of ModiaPlot.plot(result, ...) is ignored, since the first argument is nothing." |
| 197 | + return |
| 198 | + end |
| 199 | + xAxis2 = isnothing(xAxis) ? xAxis : string(xAxis) |
| 200 | + PyPlot.figure(figure) |
| 201 | + if !reuse |
| 202 | + PyPlot.clf() |
| 203 | + end |
| 204 | + heading2 = SignalTables.getHeading(result, heading) |
| 205 | + (nrow, ncol) = size(names) |
| 206 | + |
| 207 | + # Add signals |
| 208 | + k = 1 |
| 209 | + for i = 1:nrow |
| 210 | + xLabel = i == nrow |
| 211 | + for j = 1:ncol |
| 212 | + # "reuse" gives a warning |
| 213 | + # MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. |
| 214 | + # One can gid rid of it by the sequence |
| 215 | + # ax1 = subplot(..) |
| 216 | + # plot(..) |
| 217 | + # subplot(ax1) # plot into this previously defined subplot |
| 218 | + # plot(..) |
| 219 | + # However, SignalTables.plot(..) has no internal state and has currently now way to pass the ax1 definition to the next SignalTables.plot(..) call. |
| 220 | + ax = PyPlot.subplot(nrow, ncol, k) |
| 221 | + if minXaxisTickLabels && !xLabel |
| 222 | + # Remove xaxis tick labels, if not the last row |
| 223 | + ax.set_xticklabels([]) |
| 224 | + end |
| 225 | + addPlot(names[i,j], result, grid, xLabel, xAxis2, prefix, reuse, maxLegend, MonteCarloAsArea, figure, i, j, nrow*ncol) |
| 226 | + k = k + 1 |
| 227 | + if ncol == 1 && i == 1 && heading2 != "" && !reuse |
| 228 | + PyPlot.title(heading2) |
| 229 | + end |
| 230 | + end |
| 231 | + end |
| 232 | + |
| 233 | + # Add overall heading in case of a matrix of diagrams (ncol > 1) |
| 234 | + if ncol > 1 && heading2 != "" && !reuse |
| 235 | + PyPlot.suptitle(heading2) |
| 236 | + end |
| 237 | +end |
| 238 | + |
| 239 | +showFigure(figure::Int) = nothing |
| 240 | + |
| 241 | +function saveFigure(figureNumber::Int, fileName)::Nothing |
| 242 | + fullFileName = joinpath(pwd(), fileName) |
| 243 | + println("... save plot in file: \"$fullFileName\"") |
| 244 | + PyPlot.figure(figureNumber) |
| 245 | + PyPlot.savefig(fileName) |
| 246 | + return nothing |
| 247 | +end |
| 248 | + |
| 249 | + |
| 250 | +closeFigure(figure::Int) = PyPlot.close(figure) |
| 251 | + |
| 252 | +closeAllFigures() = PyPlot.close("all") |
| 253 | + |
| 254 | + |
| 255 | +end |
0 commit comments