2D Plots... some missing possibilities?

I’m continuing to explore Plots (mainly 2D for now). Some useful things are missing, others are perhaps only hard to find?

  • Figure title – has not been implemented yet. There are some workarounds, but these are somewhat clumsy. Currently, “title” gives the subplot title, as far as I know.
  • Specifying several columns in legend panel (like PyPlot can) – it does not always look good to have a long column…
  • Support for LaTeX math: PyPlot backend has good support, GR less so. GR LaTeX math looks ugly in the Atom/Juno editor, and in IJulia (perhaps better in saved files?). Also, the GR support of LaTeX is incomplete: it seems to work in title, xlabel, and ylabel, but not in labels.
  • If I try to use PyPlot as backend in Atom/Juno, I get an error message related to loading a C-library. GR, on the other hand, works (but with limited LaTeX support…)
  • Grid…:
    – How to specify color, linewidth, line style, etc of grid lines?
    – How to specify spatial gap between grid lines? E.g., for Bode plots, the standard is to use equidistant gap between gridlines for the integer exponents (major grid), and logarithmic gap within each of the major grid lines (minor grid). Is this possible (ControlSystems package seems to give equidistant grid lines, only), or do I have to use PyPlot for now to achieve this?

OK – I really appreciate all the work going into visualization tools. And I know there is hectic activity related to v. 0.7/v.1.0. So please do not read this as critique: I rather ask to find out what is currently possible (and what may be in planning…).

2 Likes

If integration with LaTeX documents is important for you, consider exploring

which is just a thin layer over the LaTeX package pgfplots, so it relies on the features and the documentation of the latter. It can also do everything else you asked for. But note that it is not integrated into Plots.jl, so you wont be able to switch backends so easily, and you really have to like LaTeX :wink:

it would be nice if you would turn each of these points into issues on the Plots.jl repo - that will make it easier to address each of them in turn, associate discussion and code suggestion to them etc. Just please first search Plots.jl first to see if some of them are already open issues, then comment on the issue instead of openting a new one.

Thanks!

2 Likes

We actually have the minor grids in ControlSystems.jl (at least if I understand you correctly). I’m not sure which backends show this properly. Image found below, and an example of how it can be done is here

The

Great: you understand me correctly wrt. “minor grid”. Is it possible to specify line type, line thickness, color, etc. for the grids (major, minor)?

This “minor grid” didn’t show up in my IJulia notebook… I’ll have to check what backend I used. I know that PyPlot can have this “minor grid”. I’ll check tomorrow.

OK – I’ve checked: neither of backends “pyplot” or “gr” give the “minor logarithmic grid” – unless there is a way to set it. Also, I notice your bodeplots are somewhat strange in the way it displays the numbers along the abscissa. Anyway, here is what I get using bodeplot:

Some years ago, I played around with Python/pyplot. The following example illustrates the possibility I’m looking for:
image

The following code was used to produce this:

The possibility to specify the grid color/thickness/line style is perhaps more for nostalgic reasons… I remember when, as a student, I had to buy a notebook of bode plot sheets – the color of the grid was typically some shade of green or orange…

Do consider ordering this into issues on Plots.
If you go to the repo, you’ll see that minor ticks were just implemented on master for gr() https://github.com/JuliaPlots/Plots.jl/pull/1596 (and you get them natively if you use plotlyjs()).
You can adjust the grid using the grid magical argument, e.g. grid = (:green, :dot, 2, 1.)

1 Like

Thanks for tip on grid.

I still don’t get logarithmic grid for GR and PyPlot… I tried to install PlotlyJS on my system (from IJulia); didn’t work.

My fault here, I didn’t test this properly on the last tagged version of ControlSystems. The plotting has been changed a lot since the last version of ControlSystems (as much of the rest of the package). For example we now use Plots recipes. The plot I showed is generated from the “master” branch of ControlSystems.jl.

The printing on the axis depends on how the backend interprets the strings “10^{-1}” and similar. We have a special case for the GR backend where they are encoded as LaTexString.

It should be noted that the minor grids are not printed if there are already too many major grids, and we don’t know how to have different properties on minor and major grids. If you have any good ideas, feel free to create a pull request onto the “autodiff” branch of ControlSystems.

OK… I’m trying to decode the command grid = (:green, :dot, 2, 1.). From the Plots documentation:

plot(y, line = (:steppre, :dot, :arrow, 0.5, 4, :red))

plot(y, seriestype = :steppre,
        linestyle = :dot,
        arrow = :arrow,
        linealpha = 0.5,
        linewidth = 4,
        linecolor = :red)

So… I’m guessing that:

plot(y, grid= (:green, :dot, 2, 1.))

could be spelt out as:

plot(y, gridlinestyle = :dot,
        gridalpha = 1.,
        gridlinewidth = 2,
        gridlinecolor = :green)

I have not been able to guess how to achieve logarithmic scaling of the grid with xcale=:log10, though…

Thus, with julia> pyplot(), the following code:

sys = tfg("2*(1-3*s)/(1+0.1*s)/(1+10*s)")
mag,phase,w = bode(sys);
#
plot(w,[20*log10.(mag[:][:][:]),phase[:][:][:]],xscale=:log10,
    linecolor=[:red :blue],layout=(2,1),linewidth=1.5,linealpha=0.9)
plot!(title=["Bode plot" ""],xlabel=["" "Frequency [rad/s]"])
plot!(ylabel=["Magnitude (dB)" "Phase (deg)"])
plot!(gridcolor=:green,gridlinestyle=:dot,gridlinewidth=1,
    gridalpha=0.8)

leads to:
image

Perhaps the nice, logarithmic “minor grid” as shown for pyplot in a previous message will appear in a later version of Plots?

Hm… my PyPlot code shown above (I did this ca. 2011-2012) automatically includes log10 minor grid, I guess. I assume that the statement plt.grid(True, "both", color="green") is interpreted as follows: include grid (True), use both major grid and minor grid ("both"), and "green" is obvious…

Have you checked out Plots master?

The minor grids is implemented in the ControlSystems package (but only on “master” branch). There you are also able to send in any extra commands that you would send to plots, such as gridlinestyle = :dot. Example

bodeplot(sys, gridstyle=:dot)

However, since it plots both the phase and magnitude, it is sometimes a bit ambiguous. There is therefore an alternative way to use the features we have, which is using the seriestype = :bodemag/:bodephase
Example:

Plots.plot(w, mag[:], seriestype = :bodemag, gridstyle = :dot)

Again, these currently only work on ControlSystems “master” branch, which you can get with
Pkg.checkout("ControlSystems", "master")

Edit: there is a function that computes the ticks (both major and minor) that you can use to customize you plots. Example

s = tf("s")
sys = -6*(s-0.33)/(s+10)/(s+0.1)
w = logspace(-1,1)
mag, phase, w = bode(sys, w)
ticks = ControlSystems.getLogTicks(w)
Plots.plot(w, mag[:], xticks=ticks, xscale=:log10)

it returns something like

([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], String["10^{-1}", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "10^{0}", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "\$ \$", "10^{1}"])