PlotlyJS: customizing subplot figure titles from facet categories

Hi everyone,

I am trying to adapt a piece of code from Plotly into PlotlyJS. This code allows editing the titles of individual subplots produced with facet_row and facet_col by changing the default format. I have tried hard, but somehow I failed to translate the code.

The MWE is as follows. With the code:

using PlotlyJS
using DataFrames
using CSV

df_tips =CSV.read("tips.csv" ,  delim=';' , DataFrame)
fig = plot(df_tips, x=:total_bill, y=:tip, facet_col=:sex, color=:smoker, mode="markers")

and with the tips.csv dataset (from here ), I can generate the plot below:

I need to remove the “facet_col” name: sex=, which appears in all subplots. In this simple example, this repetition is irrelevant, but in figures with many subplots, this particular characteristic turns out to be quite disturbing.

There is a discussion of this problem here, and on the Plotly website here there is an explanation of how it can be implemented:

import plotly.express as px
fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

I am missing how to adapt the third code line above to the context of PlotlyJS:

fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

Help will be very much appreciated. Thanks.

You can customise the label with e.g. labels=Dict(:sex => "V")

fig = plot(df_tips, x=:total_bill, y=:tip, facet_col=:sex, color=:smoker, mode="markers",
        labels=Dict(:sex => "V"))

The = remains, however, in this instance.

@jd-foster, that helps a little, but it looks awkward to see a useless = in all subplots. Thanks for helping.

Ok, try this one?:

fig = plot(df_tips, Layout(); x=:total_bill, y=:tip, facet_col=:sex, color=:smoker, mode="markers")

for sp in fig.plot.layout.annotations
        sp.text = split(sp.text,"=")[end]
end
display(fig)

@jd-foster, sorry, it still comes out exactly as my initial plot, in VSCode.

Did you get a new plot with display(fig)?

Yes, I did get a new plot, but it looks exactly equal to the one above.

I’m not sure why we have different results then. I’m using PlotlyJS v0.18.8 in VSCode.

I am on a Windows 10 machine, Julia 1.7.1, using PlotlyJS v0.18.8, while VSCode is 1.68.0.

I restarted VSCode to avoid some possible strange conflicts. Used exactly your code:

fig = plot(df_tips, Layout(); x=:total_bill, y=:tip, facet_col=:sex, color=:smoker, mode="markers")

    for sp in fig.plot.layout.annotations
        sp.text = split(sp.text,"=")[end]
    end

display(fig)

and my output is this:

Thank you very much for helping.

It looks like you are using a pop-up window instead of the VSCode Julia Plots panel?
For some reason, please do relayout!(fig) instead of display(fig) in this case.

1 Like

Congrats, you did it. It works by adding relayout!(fig) and calling the fig again at the end. Thanks a lot. This turnaround is extremely useful when dealing with large panel data sets.

For posterity, the code that works in VSCode, with an external plots window, is the solution provided by @jd-foster above:

fig = plot(df_tips, Layout(); x=:total_bill, y=:tip, facet_col=:sex, color=:smoker, mode="markers")

    for sp in fig.plot.layout.annotations
        sp.text = split(sp.text,"=")[end]
    end
relayout!(fig)
fig

with no more sex= in the plot:

2 Likes