6.2 Attributes

A custom plot can be created by using attributes. The attributes can be set through keyword arguments. A list of attributes for every plotting object can be viewed via:

  1. fig, ax, pltobj = scatterlines(1:10)
  2. pltobj.attributes
  1. Attributes with 15 entries:
  2. color => RGBA{Float32}(0.0,0.447059,0.698039,1.0)
  3. colormap => viridis
  4. colorrange => Automatic()
  5. cycle => [:color]
  6. inspectable => true
  7. linestyle => nothing
  8. linewidth => 1.5
  9. marker => circle
  10. markercolor => Automatic()
  11. markercolormap => viridis
  12. markercolorrange => Automatic()
  13. markersize => 12
  14. model => Float32[1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]
  15. strokecolor => black
  16. strokewidth => 0

Or as a Dict calling pltobj.attributes.attributes.

Asking for help in the REPL as ?lines or help(lines) for any given plotting function will show you their corresponding attributes plus a short description on how to use that specific function. For example, for lines:

  1. help(lines)
  1. lines(positions)
  2. lines(x, y)
  3. lines(x, y, z)
  4. Creates a connected line plot for each element in (x, y, z), (x, y) or
  5. positions.
  6. NaN values are displayed as gaps in the line.
  7. Attributes
  8. ============
  9. Specific
  10. ––––––––––
  11. cycle::Vector{Symbol} = [:color] sets which attributes to cycle
  12. when creating multiple plots.
  13. linestyle::Union{Nothing, Symbol, Vector} = nothing sets the
  14. pattern of the line (e.g. :solid, :dot, :dashdot)
  15. linewidth::Real = 1.5 sets the width of the line in pixel units.
  16. Generic
  17. –––––––––
  18. visible::Bool = true sets whether the plot will be rendered or
  19. not.
  20. overdraw::Bool = false sets whether the plot will draw over other
  21. plots. This specifically means ignoring depth checks in GL
  22. backends.
  23. transparency::Bool = false adjusts how the plot deals with
  24. transparency. In GLMakie transparency = true results in using
  25. Order Independent Transparency.
  26. fxaa::Bool = false adjusts whether the plot is rendered with fxaa
  27. (anti-aliasing). Note that line plots already use a different form
  28. of anti-aliasing.
  29. inspectable::Bool = true sets whether this plot should be seen by
  30. DataInspector.
  31. depth_shift::Float32 = 0f0 adjusts the depth value of a plot after
  32. all other transformations, i.e. in clip space, where 0 <= depth <=
  33. 1. This only applies to GLMakie and WGLMakie and can be used to
  34. adjust render order (like a tunable overdraw).
  35. model::Makie.Mat4f sets a model matrix for the plot. This replaces
  36. adjustments made with translate!, rotate! and scale!.
  37. color sets the color of the plot. It can be given as a named color
  38. Symbol or a Colors.Colorant. Transparency can be included either
  39. directly as an alpha value in the Colorant or as an additional
  40. float in a tuple (color, alpha). The color can also be set for
  41. each point in the line by passing a Vector of colors or be used to
  42. index the colormap by passing a Real number or Vector{<: Real}.
  43. colormap::Union{Symbol, Vector{<:Colorant}} = :viridis sets the
  44. colormap that is sampled for numeric colors.
  45. colorrange::Tuple{<:Real, <:Real} sets the values representing the
  46. start and end points of colormap.
  47. nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0) sets a
  48. replacement color for color = NaN.
  49. space::Symbol = :data sets the transformation space for line
  50. position. See Makie.spaces() for possible inputs.
  51. lines has the following function signatures:
  52. (Vector, Vector)
  53. (Vector, Vector, Vector)
  54. (Matrix)
  55. Available attributes for Lines are:
  56. color
  57. colormap
  58. colorrange
  59. cycle
  60. depth_shift
  61. diffuse
  62. inspectable
  63. linestyle
  64. linewidth
  65. nan_color
  66. overdraw
  67. shininess
  68. space
  69. specular
  70. ssao
  71. transparency
  72. visible

Not only the plot objects have attributes, also the Axis and Figure objects do. For example, for Figure, we have backgroundcolor, resolution, font and fontsize as well as the figure_padding which changes the amount of space around the figure content, see the grey area in Figure 5. It can take one number for all sides, or a tuple of four numbers for left, right, bottom and top.

Axis has a lot more, some of them are backgroundcolor, xgridcolor and title. For a full list just type help(Axis).

Hence, for our next plot we will call several attributes at once as follows:

  1. lines(1:10, (1:10).^2; color=:black, linewidth=2, linestyle=:dash,
  2. figure=(; figure_padding=5, resolution=(600, 400), font="sans",
  3. backgroundcolor=:grey90, fontsize=16),
  4. axis=(; xlabel="x", ylabel="x²", title="title",
  5. xgridstyle=:dash, ygridstyle=:dash))
  6. current_figure()

Figure 5: Custom plot.

Figure 5: Custom plot.

This example has already most of the attributes that most users will normally use. Probably, a legend will also be good to have. Which for more than one function will make more sense. So, let’s append another mutation plot object and add the corresponding legends by calling axislegend. This will collect all the labels you might have passed to your plotting functions and by default will be located in the right top position. For a different one, the position=:ct argument is called, where :ct means let’s put our label in the center and at the top, see Figure Figure 6:

  1. lines(1:10, (1:10).^2; label="x²", linewidth=2, linestyle=nothing,
  2. figure=(; figure_padding=5, resolution=(600, 400), font="sans",
  3. backgroundcolor=:grey90, fontsize=16),
  4. axis=(; xlabel="x", title="title", xgridstyle=:dash,
  5. ygridstyle=:dash))
  6. scatterlines!(1:10, (10:-1:1).^2; label="Reverse(x)²")
  7. axislegend("legend"; position=:ct)
  8. current_figure()

Figure 6: Custom plot legend.

Figure 6: Custom plot legend.

Other positions are also available by combining left(l), center(c), right(r) and bottom(b), center(c), top(t). For instance, for left top, use :lt.

However, having to write this much code just for two lines is cumbersome. So, if you plan on doing a lot of plots with the same general aesthetics, then setting a theme will be better. We can do this with set_theme!() as the following example illustrates.

Plotting the previous figure should take the new default settings defined by set_theme!(kwargs):

  1. set_theme!(; resolution=(600, 400),
  2. backgroundcolor=(:orange, 0.5), fontsize=16, font="sans",
  3. Axis=(backgroundcolor=:grey90, xgridstyle=:dash, ygridstyle=:dash),
  4. Legend=(bgcolor=(:red, 0.2), framecolor=:dodgerblue))
  5. lines(1:10, (1:10).^2; label="x²", linewidth=2, linestyle=nothing,
  6. axis=(; xlabel="x", title="title"))
  7. scatterlines!(1:10, (10:-1:1).^2; label="Reverse(x)²")
  8. axislegend("legend"; position=:ct)
  9. current_figure()
  10. set_theme!()

Figure 7: Set theme example.

Figure 7: Set theme example.

Note that the last line is set_theme!(), which will reset to the default settings of Makie. For more on themes please go to Section 6.3.

Before moving on into the next section, it’s worthwhile to see an example where an array of attributes is passed at once to a plotting function. For this example, we will use the scatter plotting function to do a bubble plot.

The data for this could be an array with 100 rows and 3 columns, which we generated at random from a normal distribution. Here, the first column could be the positions in the x axis, the second one the positions in y and the third one an intrinsic associated value for each point. The latter could be represented in a plot by a different color or with a different marker size. In a bubble plot we can do both.

  1. using Random: seed!
  2. seed!(28)
  3. xyvals = randn(100, 3)
  4. xyvals[1:5, :]
  1. 5×3 Matrix{Float64}:
  2. 0.550992 1.27614 -0.659886
  3. -1.06587 -0.0287242 0.175126
  4. -0.721591 -1.84423 0.121052
  5. 0.801169 0.862781 -0.221599
  6. -0.340826 0.0589894 -1.76359

Next, the corresponding plot can be seen in Figure 8:

  1. fig, ax, pltobj = scatter(xyvals[:, 1], xyvals[:, 2]; color=xyvals[:, 3],
  2. label="Bubbles", colormap=:plasma, markersize=15 * abs.(xyvals[:, 3]),
  3. figure=(; resolution=(600, 400)), axis=(; aspect=DataAspect()))
  4. limits!(-3, 3, -3, 3)
  5. Legend(fig[1, 2], ax, valign=:top)
  6. Colorbar(fig[1, 2], pltobj, height=Relative(3 / 4))
  7. fig

Figure 8: Bubble plot.

Figure 8: Bubble plot.

where we have decomposed the tuple FigureAxisPlot into fig, ax, pltobj, in order to be able to add a Legend and Colorbar outside of the plotted object. We will discuss layout options in more detail in Section 6.6.

We have done some basic but still interesting examples to show how to use Makie.jl and by now you might be wondering: what else can we do? What are all the possible plotting functions available in Makie.jl? To answer this question, a CHEAT SHEET is shown in Figure 9. These work especially well with CairoMakie.jl backend.

Figure 9: Plotting functions: CHEAT SHEET. Output given by CairoMakie.

Figure 9: Plotting functions: CHEAT SHEET. Output given by CairoMakie.

For completeness, in Figure 10, we show the corresponding functions CHEAT SHEET for GLMakie.jl, which supports mostly 3D plots. Those will be explained in detail in Section 6.7.

Figure 10: Plotting functions: CHEAT SHEET. Output given by GLMakie.

Figure 10: Plotting functions: CHEAT SHEET. Output given by GLMakie.

Now, that we have an idea of all the things we can do, let’s go back and continue with the basics. It’s time to learn how to change the general appearance of our plots.

6.2 Attributes - 图7 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso