4

Coming from Python I'm trying to reproduce this Seaborn plot in Julia using the Gadfly package. I've two questions:

  • How to annotate this heatmap with the actual values per cell without "duplicating" lines of code?
  • And how to modify the xticks to show all the year values from 1949 to 1960?

My code so far:

using DataFrames
using CSV
using Gadfly
using Compose
using ColorSchemes 

download("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/flights.csv", "flights.csv");
flights = DataFrame(CSV.File("flights.csv"))
flights_unstacked = unstack(flights, :month, :year, :passengers)

set_default_plot_size(16cm, 12cm)

plot(
    flights, 
    x=:year, 
    y=:month, 
    color=:passengers, 
    Geom.rectbin, 
    Scale.ContinuousColorScale(palette -> get(ColorSchemes.magma, palette)),
    Guide.xticks(ticks=[minimum(flights.year):maximum(flights.year);]), 
    Theme(background_color = "white"),

    Guide.annotation(compose(context(), text(fill(1949, 12), 1:12, string.(flights_unstacked[:, "1949"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1950, 12), 1:12, string.(flights_unstacked[:, "1950"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1951, 12), 1:12, string.(flights_unstacked[:, "1951"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1952, 12), 1:12, string.(flights_unstacked[:, "1952"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1953, 12), 1:12, string.(flights_unstacked[:, "1953"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1954, 12), 1:12, string.(flights_unstacked[:, "1954"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1955, 12), 1:12, string.(flights_unstacked[:, "1955"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1956, 12), 1:12, string.(flights_unstacked[:, "1956"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1957, 12), 1:12, string.(flights_unstacked[:, "1957"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1958, 12), 1:12, string.(flights_unstacked[:, "1958"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1959, 12), 1:12, string.(flights_unstacked[:, "1959"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    Guide.annotation(compose(context(), text(fill(1960, 12), 1:12, string.(flights_unstacked[:, "1960"]), [hcenter], [vcenter]), fontsize(7pt), stroke("white"))), 
    )

enter image description here

René
  • 4,594
  • 5
  • 23
  • 52

1 Answers1

2

The year xticks answer is to add

Guide.xticks(ticks=[minimum(flights.year):maximum(flights.year);]),

to the plot statement.

You then need a Guide.annotation() statement for the annotations. It needs some tuning to look the same as Seaborn's, but this does what you need:

Guide.annotation(
    compose(
        context(),
        text(
            flights.year,
            12:-1:1,
            string.(flights.passengers),
            [hcenter for x in flights.passengers],
        ),
        fontsize(2.5),
        stroke("white"),
    ),
Bill
  • 5,600
  • 15
  • 27
  • Thanks for the xticks solution. My main issue is how to do the annotation in an easy way, without "duplicating" Guide.annotation code lines. Just added this to the code to clarify the question). I hope there's a way to give an array with annotation values as an argument to the annotation. – René May 17 '22 at 11:56
  • Thanks, I did accept your answer. However, I think the 12:-1:1 part should be 1:12 to get all the annotations in the right places/cells (months in this plot are in a different order than in the Seaborn plot). – René May 18 '22 at 19:27
  • 1
    True, months need to be reversed for the plot to be identical. – Bill May 19 '22 at 04:29