如何给Gadfly热力图加注释?

4

我来自Python,正在尝试使用Gadfly软件包在Julia中复制这个Seaborn图。我有两个问题:

  • 如何注释此热图以显示每个单元格的实际值,而不会“重复”代码行?
  • 如何修改xticks以显示1949年到1960年的所有年份值?

我的代码:

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

1个回答

2
年份 xticks 的答案是添加:
Guide.xticks(ticks=[minimum(flights.year):maximum(flights.year);]),

对于绘图语句,您需要一个Guide.annotation()语句来进行注释。它需要一些调整,以使其外观与Seaborn相同,但这正是您所需的:

Guide.annotation(
    compose(
        context(),
        text(
            flights.year,
            12:-1:1,
            string.(flights.passengers),
            [hcenter for x in flights.passengers],
        ),
        fontsize(2.5),
        stroke("white"),
    ),

感谢提供xticks解决方案。我的主要问题是如何以简单的方式进行注释,而不需要“复制”Guide.annotation代码行。我已将此添加到代码中以澄清问题。我希望有一种方法可以将注释值的数组作为参数传递给注释。 - René
谢谢,我接受了你的答案。然而,我认为12:-1:1部分应该改为1:12,以便在正确的位置/单元格中获取所有注释(此图中的月份顺序与Seaborn图中的不同)。 - René
1
真的,月份需要反转才能使图形相同。 - Bill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接