如何注释 Gadfly 热图?
How to annotate a Gadfly heatmap?
来自 Python 我正在尝试使用 Gadfly 包在 Julia 中重现 this 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"))),
)
年份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"),
),
来自 Python 我正在尝试使用 Gadfly 包在 Julia 中重现 this 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"))),
)
年份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"),
),