Formatting xticks in plotlyJS

Hi. I have a long time series comprising 238 quarterly observations (1960.Q1-2019.Q2)). I want to produce a simple time series plot using PlotlyJS.jl to benefit from interactivity. However, I have a problem with the x-axis displaying the correct information. For example, if I define the quarterly observations simply as:

period4 = LinRange(1960,2019.25,238)

I get the plot below with the following code:

trace10 = scatter(;x = period4, y = quarter_data[:,2], name = "GDP", line_color = "Blue")
trace11 = scatter(;x = period4, y = quarter_data[:,3], name = "CONS", line_color = "Red")
trace12 = scatter(;x = period4, y = quarter_data[:,4], name = "INV", line_color = "Gray")

layout = Layout(;title = "Some Macroeconomic Aggregates: USA (1960-Q1--2019-Q2)",
        xaxis_title = "Quarterly obervations",
        xaxis_range = [1960, 2020],
        yaxis_title = "Bilions of US dollars",
        #yaxis_range=[-2, 2], 
        titlefont_size = 16)

p10 = plot([trace10 , trace11, trace12], layout)

The problem with this plot is: how do I get rid of the thousands-comma separator in the x-axis? Apart from this problem, everything else looks perfect in the plot. As a date, 2004.25 looks acceptable, but 2,004.25 looks awkward.

If instead, for the same data I use Dates.jl and MontlyDates.jl, the quarterly observations will be defined as:

using Dates
using MonthlyDates
period5 = QuarterlyDate("1960-Q1") : Quarter(1) : QuarterlyDate("2019-Q2")

and the following code will produce the plot below:

trace13 = scatter(;x = period5, y = quarter_data[:,6], name = "FFR", line_color = "Blue")
trace14 = scatter(;x = period5, y = quarter_data[:,7], name = "UN", line_color = "Red")

layout = Layout(;title = "Some Macroeconomic Aggregates: USA (1960-Q1--2019-Q2)",
        #xaxis: {
        #tickmode= "array", 
        #tickvals = [1, 80, 160, 240],
        #ticktext = ['1960-Q1', '1980-Q1', '2000-Q1', '2020-Q1']
        #      },
        xaxis_title = "Quarterly obervations",
        #xaxis_range = [1960, 2020],
        yaxis_title = "Percentage points",
        #yaxis_range=[-2, 2], 
        titlefont_size = 16)

p10 = plot([trace13 , trace14], layout)

The x-axis of this plot turns it useless. How do I format the x-ticks such that I can remove the mess with the dates from that plot?

I tried the docs in PlotylyJS and found nothing about formatting xticks. In Plotly.js I found useful information but, apparently, it cannot just be copy+past into PlotlyJS. Help will be appreciated. Thanks.

Hi,

If you really just wants to have the dates specified as 2004.25 for quarters, then you would only need to set the separators attribute of the layout to “.” (this sets the dot as decimal separators and nothing as the thousands separator) as follows:

layout = Layout(;title = "Some Macroeconomic Aggregates: USA (1960-Q1--2019-Q2)",
        xaxis_title = "Quarterly obervations",
        xaxis_range = [1960, 2020],
        separators = ".",
        yaxis_title = "Bilions of US dollars",
        #yaxis_range=[-2, 2], 
        titlefont_size = 16)

Overall though, you might want to have the x-axis inputs recognized as date formats from plotly (they currently aren’t even in the second example), as you could then exploit the date format specifier.
You can then modify the code you provided as second example as follows:

trace13 = scatter(;x = Date.(period5), y = quarter_data[:,6], name = "FFR", line_color = "Blue")
trace14 = scatter(;x = Date.(period5), y = quarter_data[:,7], name = "UN", line_color = "Red")

layout = Layout(;title = "Some Macroeconomic Aggregates: USA (1960-Q1--2019-Q2)",
        #xaxis: {
        #tickmode= "array", 
        #tickvals = [1, 80, 160, 240],
        #ticktext = ['1960-Q1', '1980-Q1', '2000-Q1', '2020-Q1']
        #      },
            xaxis = attr(
               title = "Quarterly obervations",
               tickformat = "%Y",
               hoverformat = "%Y-Q%q",
               tick0 = "1960/01/01",
               dtick = "M120",
        ),
        #xaxis_range = [1960, 2020],
        yaxis_title = "Percentage points",
        #yaxis_range=[-2, 2], 
        titlefont_size = 16)

p10 = plot([trace13 , trace14], layout)

Where what I did was simply transforming the input data in period5 into yyyy-mm-dd format for creating the trace using the julia Date() function (to allow plotly correctly identifying these as dates).
The other modification in the layout structure is specifying a few xaxis tick attributes.
For the plotly specific attributes for the xaxis, you can find more information in the relevant documentation page

1 Like

Hi @disberd, thank you very much.

You solved my problem and possibly the problem of those that use PlotlyJS and have to deal with quarterly or monthly data. In PlotlyJS the docs are quite limited, and the adaptation of code from Plotly.js into PlotlyJS proved to be not so easy for me (and I could not find any other issue related to this point in any forum).

I do want to use the x-axis formatted as data inputs. The point is that I did not know how to get an output as your second example provides. Thanks a lot.

1 Like

I also found myself wanting to use PlotlyJS due to the interactivity and also nice static HTML export but indeed I had to do a lot of research at the beginning to tweak the small details.
The work of the PlotlyJS creator has been amazing but he obviously can’t replicate the extensive plotlyjs documentation in his julia package docs.

In the future if you need any other tweaking, I found that the general reference documentation of plotlyjs is usually quite extensive and well done, and once you learn the synthax differences between vanilla JS plotlyjs and the PlotlyJS julia package, everything becomes quite straightforward! :slight_smile:

1 Like

Yes, PlotlyJS is an amazing package for the reasons you mention (I would add also its high display quality) and it’s a perfect tool for teaching. And now, with Pluto, it may achieve even more relevance in the Julia world. The bad side is that @sglyon seems to have no time to allocate to the package or to reply to some issues that just need some clarification. But I understand; I’m using it but pay nothing.

If you are interested in using Plotly with Pluto, I recently posted a procedure to make it work directly with just PlotlyBase (instead of PlotlyJS since the latter is not officially supported yet) and static HTML export from Pluto.

Good to know. Actually, a month ago I raised an issue about the importance of integrating PLotlyJS into Pluto #60389. @fonsp replied that they were working on it.

Tomorrow, I will have a go and test your approach, which is actually what we do really need in the connection between Pluto and PlotlyJS: Pluto window and the possibility of having a static HTML export.

@disberd I have just replied to the issue you mentioned above here.

Congrats. You came up with a simple way to integrate PlotlyJS within Pluto notebooks which is immaculate. Details of my tests can be found there.